exec() -- fileupload question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

exec() -- fileupload question

Post by neophyte »

I'm trying to upload an archived file and then unzip. I can get the exec() command to execute on the file after the file upload script full executes through a different .php file. But for some reason it doesn't work when the uploading is still happening...
Does anyone know a solution to this problem?

Here's my code:

Code: Select all

<?php
$source = $_FILES['image_directory']['tmp_name'];
			$target = $storage_dir.$_FILES['image_directory']['name'];
			move_uploaded_file($source, $target) or die("Could not copy $source");
			if(file_exists($storage_dir.$_FILES['image_directory']['name']))
			{
				switch ($ext)
				{
				case ".zip":
				exec("unzip ".$storage_dir.$_FILES['image_directory']['name']);
				//echo "unzip ".$storage_dir.$_FILES['image_directory']['name'];
				
				break;
				case ".tar" :
				
				exec("tar -xf ".$storage_dir.$_FILES['image_directory']['name']);
				echo "tar -xf ".$storage_dir.$_FILES['image_directory']['name'];
				break;
				case ".sit":
				
				exec("unzip ".$_FILES['image_directory']['name']);
				break;
				}
				$message = " DUDE YOU ROCK! FILE UPLOADED!!!!!!!!";
			}
		
?>
It's echoing out what you would expect it to. But the zipped or tarred file doesn't unzip. The path to the file is also correct.

Thanks
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Do you have a session started before the exec?
If so try doing a session_write_close(); before calling the exec(), a note on http://php.net/manual/en/function.exec.php suggests this.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i was always under the impression that the webserver did
not even execute the php script until all uploads were complete.
but maybe this is caused by php copying the file from tmp

have you tried unzipping the file before moving it,
just unzip it right from the tmp directory?

maybe move_uploaded_file continues in the background until its finished,
and php just keeps parsing before it finishes copying the file from tmp

try sleep(1) right after the move_uploaded_file call.
using sleep imo isnt a good solution,
but if it works, it at least tells us something about whats going on.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I don't have any sessions running during the script. I tried the sleep () function rehfeld but that didn't produce different results. But I'm sure the path is correct for the unzip command. I copied and pasted the echoed results into another file and used the exec command and was successfully able to unzip and rm the file. I switched out move_uploaded_file for copy() but that didn't do anything. Same problem script uploads the file but doesn't move it. My php.ini file doesn't name a tmp directory for the uploaded files. If I did unzip it there where would it the unzipped file end up at. So in the end I dropped the move_uploaded_file and copy() and elected for this:

Code: Select all

<?php
   exec("unzip  ". $_FILES['image_directory']['tmp_name']." -d ".$storage_dir );
		
?>
It worked. So, problem solved.

Thanks again
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I guess the only question that remains is how do I return the name of the directory that is unzipped/tarred? How will I know what it is? Does anyone know how to do that with exec() or with shell commands?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Okay well here's how I did it. I sent the output from the shell into $archive_output. I looked at the array and determined the index to search for the string to the unzipped file. Then I can use $match to make further changes to it-- like changing owner. Anybody know of a better way?

Code: Select all

<?php
exec("unzip ". $_FILES['image_directory']['tmp_name']." -d ".$storage_dir, $archive_output);
preg_match("/(\/home\/user_name\/www\/directory\/\w+)/", $archive_output[1], $match);
exec("chown -R ".$user." ".$match[0]);
?>
Post Reply