2010-08-22 69 views
1

我想複製一些從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 

儘管事實我已經chomed正在使用的所有文件夾,這裏是代碼

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

LOL它不是一個權限問題:)
解決方法:

while(($filename = readdir($dir_handle)) !== FALSE) { 
    if ($filename[0] == ".") continue; 

考慮使用現代glob()而不是古代opendir。

+2

DirectoryIterator類可能更多的是替代品.. http://php.net/manual/en/class.directoryiterator.php – 2010-08-22 13:13:39

1

在t排除...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"; 
      } 
     } 
     } 
    }