重命名文件权限

发布于 2024-09-15 15:30:49 字数 2561 浏览 11 评论 0原文

我正在尝试复制从目录外的 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";
                }
          }
        }
      }
    }                

I am trying to copy over some files pulled out from a zip folder, from out of the directory. I modified the code from this page

Extract Directory Inside Zip

for the most part it works, but when it is attempting the rename the directory files it pulls I get the following error

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 

despite the fact I have chomed all the folders being used, here is the code

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";
                }
          }
        }
      }
    }                

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-09-22 15:30:49

哈哈,这不是权限问题:)
解决方法:

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

考虑使用现代的 glob() 而不是古老的 opendir。

LOL it's not a permission problem :)
a workaround:

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

consider to use modern glob() instead of ancient opendir.

夏花。依旧 2024-09-22 15:30:49

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";
            }
         }
      }
    }

Exclude . and .. from the rename loop as they are aliases that refer to current folder and parent folder respectively (remember why you do cd .. to go to parent folder):

   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";
            }
         }
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文