2010-10-22 30 views
43

Tôi đang cố gắng sử dụng php để tạo tệp zip (được thực hiện từ trang này - http://davidwalsh.name/create-zip-php), tuy nhiên bên trong tệp zip là tất cả tên thư mục chính tệp đó.php tạo zips mà không cần đường dẫn đến tệp bên trong zip

Có thể chỉ có tệp bên trong mã zip trừ tất cả các thư mục không?

Dưới đây là mã của tôi:

function create_zip($files = array(), $destination = '', $overwrite = true) { 

    if(file_exists($destination) && !$overwrite) { return false; }; 
    $valid_files = array(); 
    if(is_array($files)) { 
     foreach($files as $file) { 
      if(file_exists($file)) { 
       $valid_files[] = $file; 
      }; 
     }; 
    }; 
    if(count($valid_files)) { 
     $zip = new ZipArchive(); 
     if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
      return false; 
     }; 
     foreach($valid_files as $file) { 
      $zip->addFile($file,$file); 
     }; 
     $zip->close(); 
     return file_exists($destination); 
    } else { 
     return false; 
    }; 

}; 


$files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg'); 

$result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/138_files.zip'); 
+1

thử '-D' switch (chữ hoa!) – drudge

Trả lời

107

Vấn đề ở đây là $zip->addFile đang được thông qua hai thông số tương tự.

Theo the documentation:

bool ZipArchive :: addFile (string $ filename [, chuỗi $ localname])

filename
Đường dẫn đến tập tin thêm.

tên địa phương
tên địa phương bên trong kho lưu trữ ZIP.

Điều này có nghĩa là tham số đầu tiên là đường dẫn đến tệp thực trong hệ thống tệp và thứ hai là đường dẫn tệp & mà tệp sẽ có trong lưu trữ.

Khi bạn cung cấp tham số thứ hai, bạn sẽ muốn tách đường dẫn khỏi tham số khi thêm nó vào lưu trữ zip. Ví dụ, trên hệ thống Unix-based này sẽ như thế nào:

$new_filename = substr($file,strrpos($file,'/') + 1); 
$zip->addFile($file,$new_filename); 
+0

cảm ơn rất nhiều :) – SoulieBaby

+0

Strange rằng vấn đề giống hệt nhau này đã nảy sinh hai lần trong hai ngày ... http://stackoverflow.com/questions/3988496/how-to-add-a-txt-file-and-create-a-zip-in-php/3989210#3989210 –

+0

Câu hỏi phổ biến của LOL? lạ. – SoulieBaby

33

Tôi nghĩ rằng một lựa chọn tốt hơn sẽ là:

$zip->addFile($file,basename($file)); 

Mà chỉ đơn giản là chiết xuất tên tập tin từ đường dẫn.

+1

Câu trả lời hoàn hảo - hoạt động như một sự quyến rũ! – Hexodus

+0

Không biết có một hàm được xây dựng trong PHP [basename] (http://php.net/manual/en/function.basename.php) –

0

Đây chỉ là một phương pháp mà tôi thấy rằng làm việc cho tôi

$zipname = 'file.zip'; 
$zip = new ZipArchive(); 
$tmp_file = tempnam('.',''); 
$zip->open($tmp_file, ZipArchive::CREATE); 
$download_file = file_get_contents($file); 
$zip->addFromString(basename($file),$download_file); 
$zip->close(); 
header('Content-disposition: attachment; filename='.$zipname); 
header('Content-type: application/zip'); 
readfile($tmp_file); 
Các vấn đề liên quan