8

I am working on cron jobs in laravel 5.2, when i try to call a controller function from schedule to create excel sheet getting Error. but runs fine in postman.

ZipArchive::close(): Failure to create temporary: No such file or directory' in /var/www/html/Expenses/vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007.php:398

Problem with temp file permission for zipArchive?

I am getting above error while save phpexcel sheet to directory(777).

$excel_path = 'SubmittedSheets/'.$userId.'-'.uniqid().'.xlsx';
$objWriter->save($excel_path);
151291
  • 3,308
  • 7
  • 48
  • 81
  • Does the folder that you have configured as a temp folder for PHP actually exist? If so, does your script have access to it? (Remembering that a cron job may be running as a different user that a web access) – Mark Baker May 08 '17 at 08:20
  • https://laracasts.com/discuss/channels/servers/ziparchiveclose-failure-to-create-temporary-file-no-such-file-or-directory – hassan May 08 '17 at 08:21
  • @hassan - I saw this solution but not clear. please explain if you know. – 151291 May 08 '17 at 08:28
  • @MarkBaker - I did not configure any temp folder. the path i given is PHPExcel. – 151291 May 08 '17 at 08:32
  • 2
    Configuration of a temp folder for PHP is nothing to do with PHPExcel; it's a part of your PHP/OS configuration. You can find what it's set to by using PHP's [sys_get_temp_dir()](http://www.php.net/manual/en/function.sys-get-temp-dir.php) function – Mark Baker May 08 '17 at 08:40
  • @MarkBaker - Problem with php zip extension or temp file permission? – 151291 May 08 '17 at 08:43
  • Check the value of that temp folder, and check permissions – Mark Baker May 08 '17 at 12:58
  • @MarkBaker - I do not know where phpexcel stores temp files, and aws linux tmp has 1777 permission. – 151291 May 08 '17 at 13:05
  • ZipArchive and PHPExcel should use sys_get_temp_dir() – Mark Baker May 08 '17 at 13:13
  • But permissions alone don't say much without knowing what accounts have privilege to access the folder, and what account is being used to run your php script – Mark Baker May 08 '17 at 13:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143696/discussion-between-151291-and-mark-baker). – 151291 May 08 '17 at 13:19
  • @MarkBaker - I added absolute path to save, then runs fine. – 151291 May 11 '17 at 07:20

3 Answers3

5

Need Absolute path to save excel file in AWS Ec2 Linux for PHPExcel.

$excel_path = '/var/www/html/MyProject/public/SubmittedSheets/'.$userId.'-'.uniqid().'.xlsx';
$objWriter->save($excel_path); 
151291
  • 3,308
  • 7
  • 48
  • 81
  • I have same error but solution is not working :( , even i used -- $objWriter->save('/var/www/yebesis/public_html/files/caglar.orhan/test.xlsx'); – caglaror Oct 05 '17 at 13:34
  • 1
    @caglaror - It happens due to low disk space too, check your AWS available space, hit `df -h` and `du -sh`. please let me know error message as well. – 151291 Oct 06 '17 at 06:51
  • No, it has a lot of space :( – caglaror Oct 09 '17 at 08:04
  • I am not on AWS Ec2 but I had same problem on Centos7 configured on Azure. sys_temp_dir was empty. I had to go in edit php.in to set tmp directory path to fix this error. – Arvind K. Jan 10 '19 at 06:05
3

I solved it on my Mac OS system by simply uncommenting the line

;sys_temp_dir = "/tmp"

in php.ini, i.e. changing it to

sys_temp_dir = "/tmp"

Directory where the temporary files should be placed. Defaults to the system default (see sys_get_temp_dir)

Not sure which directory it tried to use as default though, possibly /var/tmp, which my Homebrew PHP installation doesn't seem to have permission to write to.

Magnus
  • 17,157
  • 19
  • 104
  • 189
0

I've tried a lot of things and spend a lot of time. Solution for Ubuntu + Vesta + Nginx + Apache was in file:

:/home/admin/conf/web/your-domain.com.apache2.ssl.conf

set up tmp folder like in my example:

<Directory /home/admin/web/your-domain.com/public_html>
    AllowOverride All
    SSLRequireSSL
    Options +Includes -Indexes +ExecCGI
    php_admin_value open_basedir 
    /home/admin/web/your-domain.com/public_html:/tmp
    php_admin_value upload_tmp_dir /tmp
    php_admin_value session.save_path /tmp
</Directory>

example image

don't forget to restart the apache service

zhovtyj
  • 61
  • 1
  • 3