2013-07-03 81 views
0

因此,我正在爲字體做一個localhost網站,並且我設法讓用戶選擇多個文件到一個購物車中,如果它們來自同一個文件夾,例如C :/ fonts/a或C:/ fonts/b但是如果有來自a和b的文件,則無法下載PHP從多個文件夾中壓縮和下載文件

那麼可以這樣做嗎?像A和B下載選定的文件

下面是我的download.php代碼

  <?php 
      include 'dbfunctions.php'; 

      if (!$link) { 
       die(mysqli_error($link)); 
      } 
      $sql="SELECT id, Font_Name, Font_Family 
         FROM font"; 
      $result = mysqli_query($link, $sql) or die(mysqli_error($link)); 

      while($row = mysqli_fetch_array($result)){ 

      $Font_Family = $row['Font_Family']; 
      $a = file_get_contents('./a.txt'); 
      $file_dir = file_get_contents('./font_path.ini'); 

      $path = $file_dir.$Font_Family.$a; 

      $error = ""; //error holder 
      if(isset($_POST['createpdf'])) 
      { 
      $Font_Family = $_POST['Font_Family']; 
      } 
      $file_folder = $path;// folder to load files 
      if(extension_loaded('zip')) 
      { 
      // Checking ZIP extension is available 
      if(isset($_POST['files']) and count($_POST['files']) > 0) 
      { 

      // Checking files are selected 
      $zip = new ZipArchive(); // Load zip library 
      $zip_name = time().".zip"; // Zip name 
      if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE) 
      { 
      // Opening zip file to load files 
      $error .= "* Sorry ZIP creation failed at this time"; 
      } 
      foreach($_POST['files'] as $file) 
      { 
      $zip->addFile($file_folder.$file,pathinfo($file,PATHINFO_BASENAME)); 
      readfile($path); 

      } 
      $zip->close(); 
      if(file_exists($zip_name)) 
      { 
      // push to download the zip 
      header('Content-type: application/zip'); 
      header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
      readfile($zip_name); 
      // remove zip file is exists in temp path 
      unlink($zip_name); 
      } 

      } 
      else 
      $error .= "* Please select file to zip "; 
      } 
      else 
      $error .= "* You dont have ZIP extension"; 
      } 

      ?> 

fontpath.ini是c:\ Fonts \中
一個就是\自PHP給了我問題時,我只寫\
FONT_FAMILY是C內的不同的文件夾名稱:\字體\
這樣的路徑是像C:\字體(這裏取文件夾名)\

+0

php給了你什麼問題? – DevZer0

+0

他們只是對待'並關閉它。 – Ren

回答

0

你可以採取每個字體,目錄不分,並將其複製到臨時目錄中,說,就像,C:/fonts/temp/cartID/。然後你可以壓縮該字體的目錄(一些來自/ fonts/a和一些來自/ fonts/b)並提供下載。然後您需要垃圾收集,即刪除臨時cartid目錄。

只是我的2¢

+0

我該怎麼做?在互聯網上有沒有類似的例子?我無法真正發現。因爲這就是我的主管建議 – Ren

+0

我確定有。我不熟悉windows,但這裏有一個在php代碼中使用linux的例子: exec(「cp /fonts/a/font_name.ttf /fonts/temp/845/font_name.ttf」); exec(「cp /fonts/b/font2.ttf /fonts/temp/845/font2.ttf」); ...執行您的壓縮包並下載... exec(「rm/fonts/temp/845 /」); – dmayo

+0

也檢查此頁面:http://php.net/manual/en/function.copy.php因爲有一些Windows命令,_and_它是一個本地php函數啓動(我剛剛學到了一些新東西!)。 – dmayo

相關問題