TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-26-2008, 08:20 AM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
oscargodson is on a distinguished road
Default Exec(zip) in PHP not working as expected

I have used this function before and I use the exec function quite frequently, but for some reason I can't get this to work right. No matter what the path is (as long as the path returns true) it always zips the "deploys" directory. Here is my directory structure real quick:

Code:
deploy
|-css
|-deploys
||--deploy_*
|||---project_name.zip
||||----project_name
|||---project_name
||||----(list files)
|-files
|-images
|-includes
|-js
The * above is the current date+a rand() number between 1-1000.

Here is how it works so you get the idea:
- Person clicks deploy
- the files are generated in a temp location in the root (deploy) directory
- the folder is copied via exec(cp)
- folder is removed from the temp location
- The folder then makes a copy of its self inside its self with the user's project name.
- Now it should zip THIS, the project_name directory.

However, it zips the deploys directory everytime no matter what the path is. Here is a snippet of the processing code I'm talking about:

PHP Code:
$sys_folder 'deploys'//What folder all the deploys are in

$deploy_download 'deploy_'.date('smd').rand(1,999); //What the folders will be named

$deploy_dir $sys_folder.'/'.$deploy_download//directory where deploy, deploys


if($session !== '' && $session !== 'hd82Dha1LL' && $session !== '1c3dD8320C'){ //Check for default and blank values.
    
$project_name $session//Project name is what they named it.
    
$safe_name str_replace(' ''_'$project_name); //System safe filename.
}
else {
    
$project_name "deploy"//Default project name
    
$safe_name "deploy";
}


        
exec('cp -r '.$deploy_download.' '.$deploy_dir.'');  //Copy the temp directory with contents to the new location
        
        
remove_dir($deploy_download); //Remove the temp folder and files
        
        
exec('cp -r '.$sys_folder.'/'.$deploy_download.' '.$deploy_dir.'/'.$safe_name.''); //Make a duplicate of the newly copied file, but rename it with their project name.
        
        
remove_dir($deploy_dir.'/'.$safe_name.'/'.$safe_name); //Kill the infinite looping of folders
        
        
exec("zip -r $deploy_dir/$safe_name $deploy_dir/$safe_name"); 
That's just some snippets from the code that are relevant for this problem.

You might seem some code that is repeating a variable like $sys_folder.'/'.$deploy_download and it's only there because I have been messing with it a lot.

Any ideas at all why it would be doing this on a Mac OS X (UNIX) system? It will be uploaded to a Linux box when it's finished, so I'm hoping I can get it working cross platform.

Thanks in advance!
oscargodson is offline  
Reply With Quote
Old 09-26-2008, 11:57 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

hmm, I dunno tbh I'll have to do some further research.
try this to see what its actually doing:
PHP Code:
exec("zip -vr $deploy_dir/$safe_name $deploy_dir/$safe_name >> log.txt"); 
then read log.txt.

Also (off topic slightly) would using 'mv' instead of copying and deleting dirs help?
Code:
mv oldname newname
can be used to move and rename folders/files
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 09-26-2008, 03:49 PM   #3 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
oscargodson is on a distinguished road
Default

Here is my log (different files each time, but same basic thing.)

Code:
  adding: deploys/deploy_120926888/deploy/	(in=0) (out=0) (stored 0%)
  adding: deploys/deploy_120926888/deploy/css/	(in=0) (out=0) (stored 0%)
  adding: deploys/deploy_120926888/deploy/css/style.css	(in=508) (out=302) (deflated 41%)
  adding: deploys/deploy_120926888/deploy/index.html	(in=486) (out=322) (deflated 34%)
  adding: deploys/deploy_120926888/deploy/js/	(in=0) (out=0) (stored 0%)
  adding: deploys/deploy_120926888/deploy/js/jquery-1.2.6.pack.js	(in=31033) (out=15638) (deflated 50%)
total bytes=32027, compressed=16262 -> 49% savings
Also, I can't know since I am at work, but I will try the mv function instead. Sounds much easier and cleaner. Didn't even think of that. I was using the rename function, but I was getting errors.
oscargodson is offline  
Reply With Quote
Old 09-28-2008, 12:00 PM   #4 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
oscargodson is on a distinguished road
Default

FINALLY! So many hours and I finally figured it out.

Alright here is the explanation in case anyone ever needs to zip a file with the exec() function.

Since zip -r recursively zips files (zips all files and directories inside the CURRENT directory) you need to find out how you are trying to zip the files.

a. Zipping the files with your script in the SAME directory as the file/directory you are trying to zip. E.g. Zipping gallery with a script inside of gallery would zip gallery + all the files and directories inside of gallery.
This would be no problem.

b. (this is what I was trying to do.) Zip your files from another location like a parent directory. E.g. Try to zip gallery from /home/tmp/gallery would zip home, tmp, gallery and all of their subdirectories. Not what you wanted probably.

So, how do you do these both?

a. exec("zip -r myzip mydirectory")
b. exec("cd home/tmp; zip -r myzip mydirectory")

In b, we had to change the directory to be a sibling of the directory we wanted to zip, THEN zip directory. Technically, without doing that it's zipping the "./" and "../" thats why you get all the files above it as well.

Hope this helps someone in the future :)

Oh by the way, my fix was to make the:
PHP Code:
exec("zip -r $deploy_dir/$safe_name $deploy_dir/$safe_name"); 
to:

PHP Code:
exec("cd $deploy_dir; zip -r $safe_name $safe_name"); 
oscargodson is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:18 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design