重命名文件权限
Posted
技术标签:
【中文标题】重命名文件权限【英文标题】:Rename file permission 【发布时间】:2010-08-22 12:24:12 【问题描述】:我正在尝试从目录中复制从 zip 文件夹中提取的一些文件。我从这个页面修改了代码
Extract Directory Inside Zip
在大多数情况下它可以工作,但是当它尝试重命名它提取的目录文件时,我收到以下错误
Warning: rename(.,Manga/Naruto/Hell/.) [function.rename]: Permission denied in C:\public_html\mangaUpload.php on line 124
Warning: rename(..,Manga/Naruto/Hell/..) [function.rename]: Permission denied in C:\public_html\mangaUpload.php on line 124
尽管我已经选择了所有正在使用的文件夹,但这里是代码
if(!file_exists("Manga"))
mkdir("Manga",0777);
chmod("Manga",0777);
if(!file_exists("Manga/".$_POST['mangaName']))
mkdir("Manga/".$_POST['mangaName'],0777);
chmod("Manga/".$_POST['mangaName'],0777);
if(!file_exists("Manga/".$_POST['mangaName']."/".$_POST['chapterName']))
mkdir("Manga/".$_POST['mangaName']."/".$_POST['chapterName'],0777);
chmod("Manga/".$_POST['mangaName']."/".$_POST['chapterName'],0777);
$pathname = "Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/";
$chapterZip = new ZipArchive();
if ($chapterZip->open($_FILES['chapterUpload']['tmp_name']) === true)
for($i = 0; $i < $chapterZip->numFiles; $i++)
$chapterZip->extractTo($pathname, array($chapterZip->getNameIndex($i)));
chmod($pathname.$chapterZip->getNameIndex($i),0777);
list($width, $height) = getimagesize($pathname.$chapterZip->getNameIndex($i));
$imageLocation= "INSERT INTO imageLocation (imageLocation,imageWidth,imageHeight,chapterID) VALUES ('"."Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/".$chapterZip->getNameIndex($i).
"',".$width.",".$height.",".$chapterID.")";
getQuery($imageLocation,$l);
$chapterZip->close();
$directories = glob($pathname.'*', GLOB_ONLYDIR);
if ($directories !== FALSE)
foreach($directories as $directory)
$dir_handle = opendir($directory);
while(($filename = readdir($dir_handle)) !== FALSE)
// Move all subdirectory contents to "Chapter Folder"
if (rename($filename, $pathname.basename($filename)) === FALSE)
$errmsg0.= "Error moving file ($filename) \n";
else
$errmsg0.="You have successfully uploaded a manga chapter";
【问题讨论】:
【参考方案1】:哈哈,这不是权限问题 :) 解决方法:
while(($filename = readdir($dir_handle)) !== FALSE)
if ($filename[0] == ".") continue;
考虑使用现代的glob() 而不是古老的opendir。
【讨论】:
DirectoryIterator 类可能更像是一个替代品。php.net/manual/en/class.directoryiterator.php【参考方案2】:从rename
循环中排除.
和..
,因为它们是分别引用当前文件夹和父文件夹的别名(请记住为什么要使用cd ..
转到父文件夹):
while(($filename = readdir($dir_handle)) !== FALSE)
if ($filename != '.' && $filename != '..')
// Move all subdirectory contents to "Chapter Folder"
if (rename($filename, $pathname.basename($filename)) === FALSE)
$errmsg0.= "Error moving file ($filename) \n";
else
$errmsg0.="You have successfully uploaded a manga chapter";
【讨论】:
以上是关于重命名文件权限的主要内容,如果未能解决你的问题,请参考以下文章