Php unlink () and cyrillic

I have a problem with deleting files through the unlink() function. When the file has a Cyrillic name, the function does not work.

[24-Jul-2012 00:33:35 UTC] PHP Warning: unlink (/ home / gtsvetan / public_html / menager .doc) [function.unlink]: There is no such file or directory in / home / gtsvetan / public _html / deleter .php on line 114

So how to delete a file when the name is cyrillized?

The code:

 $dir = is_array($dir) ? $dir : explode(',', $dir); foreach($dir as $dirv) { if(is_dir($dirv)) { $objects = scandir($dirv); foreach($objects as $object) { if($object != "." && $object != "..") { if(filetype($dirv."/".$object) == "dir") { $this->delete($dirv."/".$object); } else { unlink($dirv."/".$object); } } } reset($objects); rmdir($dirv); } else { unlink($dirv); } } 

Decision:

 public function delete($dir) { $dir = is_array($dir) ? $dir : explode(',', $dir); foreach($dir as $dirv) { if(is_dir($dirv)) { $d = @dir($dirv) or die(); while(false !== ($entry = $d->read())) { if($entry[0] == ".") { continue; } if(is_dir($dirv.$entry.'/')) { $this->delete($dirv.$entry.'/'); @rmdir($dirv.$entry); } elseif(is_readable($dirv.$entry)) { @unlink($dirv.$entry); } } $d->close(); } else { @unlink($dirv); } @rmdir($dirv); } } 

And here is the ajax.php that make the class instance :)

 case 'delete': $location = $_POST['location']; if(is_array($location)) { foreach($location as $v) { $loc[] = iconv('utf-8', 'cp1251', $v); } $pfm->delete($loc); } else { $location = iconv('utf-8', 'cp1251', $location); $pfm->delete($location); } break; 

It works great for me :)

+4
source share
4 answers

I would suggest renaming it first if it doesn't play well.

0
source

I found that disinfecting file names is always a good idea. I personally like the script name files themselves, not the users (esp if it's a downloaded file). create a cleanup function that converts cyrillic characters. take a look at convert_cyr_string :: http://php.net/manual/en/function.convert-cyr-string.php

Another idea, does renaming a file have the same problems as deleting them? if not, rename it to something like tobedeleted.ext, then undo it.

0
source

PHP's unlink will simply go to the appropriate system call. The file name will be passed to this function as is, since PHP strings are just opaque byte sequences. This means that the name must be in the encoding understood by the system call. In other words, it depends on your OS. You should also know what the current encoding of the file name is; it depends on where the input comes from.

If you know that a system call requires UTF-8 (which is true for Linux) and that the name is currently in ISO-8859-5, then the solution using iconv will look like

 unlink(iconv('iso-8859-5', 'utf-8', $dirv."/".$object)); 

Of course you can do the same with mb_convert_encoding . The same processing is also necessary for all other file system calls.

0
source

Hmm, I did it, it can come in handy.

  <?php function delete($link) { foreach($link as $u) { if(is_dir($u)) { delete(glob($u . DIRECTORY_SEPARATOR . "*")); rmdir($u); } else; unlink($u); } return; } delete(glob(__DIR__ . DIRECTORY_SEPARATOR . "*")); ?> 
0
source

All Articles