2015-06-03 17 views
7

Tôi đang sử dụng Laravel 4.2 và đang tìm cách thiết lập khu vực nơi tệp zip được tạo để người dùng tải xuống.Tạo Zip và Force Tải xuống - Laravel

tôi tiếp tục nhận được lỗi

Các tập tin "myzip.zip không tồn tại"

mã hiện tại của tôi là:

// Here we choose the folder which will be used. 
      $dirName = public_path() . '/uploads/packs/'.$pack_id; 

      // Choose a name for the archive. 
      $zipFileName = 'myzip.zip'; 

      // Create "MyCoolName.zip" file in public directory of project. 
      $zip = new ZipArchive; 

      if ($zip->open(public_path() . '/' . $zipFileName, ZipArchive::CREATE) === true) 
      { 
       // Copy all the files from the folder and place them in the archive. 
       foreach (glob($dirName . '/*') as $fileName) 
       { 
        $file = basename($fileName); 
        $zip->addFile($fileName, $file); 
       } 

       $zip->close(); 

       $headers = array(
        'Content-Type' => 'application/octet-stream', 
       ); 

       // Download .zip file. 
       return Response::download(public_path() . '/' . $zipFileName, $zipFileName, $headers); 

thể bất cứ ai giúp tôi, Như để tại sao tôi nhận được lỗi không tồn tại?

Cảm ơn bạn!

+0

Nếu bạn kiểm tra thư mục của bạn thông qua ftp, là zip tạo hay không? –

+0

Bạn có tìm thấy giải pháp nào cho vấn đề này không? Tôi đang phải đối mặt với vấn đề tương tự. –

Trả lời

3

Từ tài liệu Laravel: http://laravel.com/docs/4.2/responses#special-responses

Tạo một tệp Tải ứng

 
return Response::download($pathToFile); 

return Response::download($pathToFile, $name, $headers); 

Có lẽ bạn nên tránh để lặp lại thứ hai

$zipFileName
ở dòng cuối cùng của bạn mã.

+2

Trong 5.2, đó sẽ là 'trả về trả về() -> tải xuống ($ pathToFile, $ name, $ headers);' –

0

Bạn có thể "tạo một tệp zip nhiều tệp và tải xuống bằng PHP" dưới đây.

Để tạo một zip của nhiều tập tin và tải về với PHP nó có một số tính năng dưới đây:

  • Nó sẽ tạo ra một file zip bằng cách sử dụng ZipArchive Thư viện.
  • Trước khi tạo tệp, tệp sẽ kiểm tra tệp tồn tại hay không.
  • Nếu tệp tồn tại thì tệp sẽ xóa tệp.
  • Nếu tệp không tồn tại thì nó sẽ tạo ra
  • một tệp zip có nhiều tệp được chuyển cho nó.
  • Khi zip đã sẵn sàng, nó sẽ được tự động tải xuống.

    //get path of files 
    $path = base_path(); 
    $file_path = $path."/../uploads/advatise/"; 
    //getting data from database 
    $row = $this->getRow($ids); 
    //if data exist 
    if($row) { 
        //multiple images in single attrubute 
        $images=$row->advertise_image; 
        $images=explode(",",$images); 
        //creating zip object 
        $zip = new ZipArchive(); 
        //creating file name 
        $DelFilePath="images".$row->id.".zip"; 
        //if file exists then to delete it 
        if(file_exists($file_path.$DelFilePath)) { 
         unlink ($file_path.$DelFilePath); 
        } 
        //if not exist then to create zip file 
        if ($zip->open($file_path.$DelFilePath, ZIPARCHIVE::CREATE) 
    != TRUE) { 
         die ("Could not open archive"); 
        } 
        //loop on the images/file to add in zip 
        foreach ($images as $key => $image) { 
         $zip->addFile($file_path.$image,$image); 
        } 
        // close and save archive 
        $zip->close(); 
        //opening zip and saving directly on client machine 
        header("Location:".Request::root()."/uploads/advatise/".$DelFilePath); 
    } 
    exit; 
    
Các vấn đề liên quan