2015-06-03 5 views
7

Я использую Laravel 4.2 и хочу настроить область, в которой создается zip-файл для загрузки пользователем.Создание Zip и Force Загрузка - Laravel

Я получаю ошибку

Файл "myzip.zip не существует"

Мой текущий код:

// 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); 

Может кто-нибудь помочь мне, Как почему я получаю ошибку не существует?

Спасибо!

+0

Если вы проверяете каталог через FTP, это почтовый создан или нет? –

+0

Вы нашли решение этой проблемы? Я столкнулся с той же проблемой. –

ответ

3

Из Laravel документации: http://laravel.com/docs/4.2/responses#special-responses

Создание файла Скачать Response

 
return Response::download($pathToFile); 

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

Может быть, вы должны избегать, чтобы повторить второй

$zipFileName
в последней строке кода.

+2

В 5.2 это будет 'return response() -> download ($ pathToFile, $ name, $ headers);' –

0

Вы можете «создать zip из нескольких файлов и загрузить на PHP» ниже.

Чтобы создать почтовый индекс нескольких файлов и загрузки с PHP имеет некоторые особенности ниже:

  • Это создаст почтовый файл с помощью ZipArchive Library.
  • Перед созданием файла он будет проверять наличие файла или нет.
  • Если файл существует, он удалит файл.
  • Если файл не существует, он будет создавать
  • zip-файл с несколькими переданными для него.
  • Когда zip готов, он будет автоматически загружен.

    //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; 
    
Смежные вопросы