• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2008
  • Last Modified:

Create zip file using php and exec()

hi there,

i'm trying to create a zip file on my server using php and the exec() command and here is the output i get :

Warning: ftp_get(test100401120201.zip) [function.ftp-get]: failed to open stream: Permission denied

my code is attached below, i'm on an ubuntu system

any ideas ?


$zipRemoteFileName = "/var/www/teleskiDev/tmp/teleski" . $date . ".zip";
$zipLocalFileName = "teleski" . $date . ".zip";			  
$zipCommand = "sudo -u root zip $zipRemoteFileName ";
foreach($files as $file) {
	$zipCommand .= $this->pwd . "/" . escapeshellcmd($file) . " ";
echo $zipCommand;

Open in new window

  • 7
  • 6
1 Solution
Permission denied.  Looks like the script is trying to work, but doesn't have the necessary permission.

Does apache have access to the folder in question?  Check the access permissions on the folder, assuming your page is running under apache the user that apache runs under needs to have write permissions to the folder in question.
terencepiresAuthor Commented:
ok, how can i do that ?
On the server where the php script is you need to make sure the access to this folder is high enough fro apache to write to.


Check the ownership and current permissions.

This article shows you how to set the permissions via your script.  Not sure about the 777 being necessary, but it may be.  If this is a public facing server, you may want to reset that

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

terencepiresAuthor Commented:
ok i tried that but it still doesn't work

maybe i have to put apache in the sudoers group ? that too i don't know how to do
terencepiresAuthor Commented:
ok, i tried to add the apache user to the sudoers group using visudo, like below

stopped / restarted server without effect...
terencepiresAuthor Commented:

# User alias specification
User_Alias      APACHE = www-data

# Cmnd alias specification
Cmnd_Alias      ZIP = /usr/bin/zip

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)

Open in new window

The sudoer files isn't really necessary as far as I know as they aren't going to be using sudo.

As a test, can you chmod 777  /var/www/teleskiDev/tmp/

This will set your tmp folder for world writeable access.  It will also be worth if you can do the following on the server that hosts the page.

tail -f /var/log/httpd/error.log

Watch that log file while you try the file and post whatever pops up if anything.
terencepiresAuthor Commented:
ok, here's what i get :

[Thu Apr 01 13:09:23 2010] [error] [client] File does not exist: /var/www/favicon.ico
      zip warning: name not matched: /home/terence/Rakefile
      zip warning: name not matched: /home/terence/backup.sql
      zip warning: name not matched: /home/terence/configuration.php
      zip warning: name not matched: /home/terence/photos.php
      zip warning: name not matched: /home/terence/teleski.zip
      zip warning: name not matched: /home/terence/test
      zip warning: name not matched: /home/terence/test.tite
      zip warning: name not matched: /home/terence/test.titi
      zip warning: name not matched: /home/terence/test.titz
      zip warning: name not matched: /home/terence/toto.zip

zip error: Nothing to do! (/var/www/teleskiDev/tmp/teleski100401130925.zip)

i then tried to do :
   cat /home/terence/test.titi

and it worked...
Can you check what the file sizes are on those files listed above.  

See the name not match highlight here.

Check the following as well to see if that applies to you if the file sizes are small.

terencepiresAuthor Commented:
- well the sizes are really small (a few kb's) and the command works when typing it from ssh

- i was using PHP's escapeshellcmd() so i tried without it, to get the same result...
It might still be a permissions problem, may the inability of the zip to fully access the files in question generates that error.  Have you tried the earlier suggestion of changing the permisssion on your tmp folder?  

All the files listed as name not matched, is that all of them?  What are the permisssions on that folder? If the same command works over ssh (I assume as yourself or root and not apache) then we are back to the permissions for apache.

Try changing the permissions on your home folder and see what happens.
terencepiresAuthor Commented:
solved !
thanks !

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now