2010-08-18 70 views
0

被複制目前我正在試圖創建一個內容上傳系統,雖然有錯誤正在從網頁製作時,我選中相應的文件夾中的內容爲空郵編文件夾的內容不

$chapterZip = new ZipArchive(); 

if ($chapterZip->open($_FILES['chapterUpload']['name'])) 
{ 
    for($i = 0; $i < $chapterZip->numFiles; $i++) 
    { 
    $pictureName = $chapterZip->getNameIndex($i); 
    $fileOpened = $chapterZip->getStream($pictureName); 
    if(!$fileOpened) exit("failed\n"); 
    while (!feof($fileOpened)) { 
     $contents = fread($fileOpened, 8192); 
     // do some stuff 
     if(copy($contents,"Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/".$pictureName."")) 
     { 
      if(chmod("Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/".$pictureName."", 0664)) 
      { 
       $errmsg0.= "File successfully copied<br/>"; 
      } 
      else 
      { 
       $errmsg0.= "Error: failed to chmod file<br/>"; 
      } 
     } 
     else 
     { 
      $errmsg0.= "Error: failed to copy file<br/>"; 
     } 
    } 
    fclose($fileOpened); 
    } 

}

這個問題的任何幫助,將不勝感激

+0

的代碼非常難以閱讀。你能更詳細地解釋你在這裏做什麼嗎? – 2010-08-18 08:05:27

+0

你得到一個錯誤或不? 此外,你混合閱讀字節($內容)與文件複製,我懷疑它可以工作。 省略一半的代碼也無濟於事。 – PhiLho 2010-08-18 08:15:58

+0

@Pekka 代碼從站點讀取上傳的zip文件,打開文件,並通過內容將循環複製到在類中早先創建的文件夾中。 @PhiLho 我沒有得到任何錯誤,現在我已經在問題中包含了整個方法,只是認爲它與我所問的問題有點不相干。 – dbomb101 2010-08-18 08:42:01

回答

1

我看着它進一步發現一個相當簡單的方法來提取文件,在PHP在線手冊

$zip = new ZipArchive; 
$res = $zip->open('test.zip'); 
if ($res === TRUE) { 
    echo 'ok'; 
    $zip->extractTo('test'); 
    $zip->close(); 
} else { 
    echo 'failed, code:' . $res; 
} 
0

我不知道如果這能幫助你,但我昨天經過一些信息,我發現尋找遞歸拉上寫這個劇本。

function zip($source, $destination) 
    { 
     if (file_exists($source) === true) 
     { 
      $zip = new ZipArchive(); 

      if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) 
      { 
       $source = realpath($source); 

       if (is_dir($source) === true) 
       { 
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); 

        foreach ($files as $file) 
        { 
         $file = realpath($file); 

         if (is_dir($file) === true) 
         { 
          $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); 
         } 

         else if (is_file($file) === true) 
         { 
          $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); 
         } 
        } 
       } 

       else if (is_file($source) === true) 
       { 
        $zip->addFromString(basename($source), file_get_contents($source)); 
       } 
      } 

      return $zip->close(); 
     } 
    } 

發現它在一些網站上,我不記得在哪裏。適應它一點點。

用法:zip('mydir /','myZipFile.zip');

BR, 保羅Peelen

+0

'if(is_true($ foo)=== true){$ isIsTrueTrue = true; $ isIsIsTrueTrueBoolean = is_boolean($ isIsTrueTrue)=== true; }' – janmoesen 2010-08-18 11:28:26