Jump to content

[SOLVED] upload -> rename -> add to zip?


B34ST

Recommended Posts

I am trying to make a script so that a user can upload a file, however I need to rename the file and add it to a zip. At the moment I am uploading the file to my server then renaming it, then adding it to a zip and then deleting the original uploaded file. is there a better way of just putting it straight in the zip with the new name? here is my code:

 

<?php

if (!$_POST) {
echo '<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
	<tr>
	<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
	<td>
	<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
	<tr>
	<td><strong>Single File Upload </strong></td>
	</tr>
	<tr>
	<tr>
<td>Name:</td>
<td><input type="text" size="16" name="name"></td>
</tr>
	<td colspan="2">Select file
	<input name="ufile" type="file" id="ufile" size="50" /></td>
	</tr>
	<tr>
	<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
	</tr>
	</table>
	</td>
	</form>
	</tr>
	</table>';
}
else {
$file_name = $HTTP_POST_FILES['ufile']['name'];

$new_file_name=$_POST['name'];

$path= "uploads/".$new_file_name;
if($ufile !=none)
{
	if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
	{
		echo "Successful<BR/>";
		echo "File Name :".$new_file_name."<BR/>";
		echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
		echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
		$zip = new ZipArchive();
		$filename = 'uploads/'.$new_file_name.'.zip';

		if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
			exit("cannot open <$filename>\n");
		}

		$zip->addFile($path);
		echo "numfiles: " . $zip->numFiles . "\n";
		$zip->close();
		unlink($path);
	}
	else
	{
		echo "Error";
	}
}
}
?>

 

Also when the zip is packed it is packing the file into an uploads directory instead of the root of the zip, how can I solve this?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/181517-solved-upload-rename-add-to-zip/
Share on other sites

I figured it out:

 

$file_name = $HTTP_POST_FILES['ufile']['name'];

$new_file_name=$_POST['name'];

if($ufile !=none)
{
		echo "Successful<BR/>";
		echo "File Name :".$new_file_name."<BR/>";
		echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
		echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
		$zip = new ZipArchive();
		$filename = 'uploads/'.$new_file_name.'.zip';

		if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
			exit("cannot open <$filename>\n");
		}

		$zip->addFile($file_name);
		$zip->renameName($file_name, $new_file_name);
		echo "numfiles: " . $zip->numFiles . "\n";
		$zip->close();
}		

 

Thanks anyway :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.