user577732
user577732

Reputation: 4046

Upload and rename zip with php

I'm trying to allow users to submit zip files to me. I would also like to rename them so that there are no conflicts.

This code doesn't work with zip file extensions. I can't figure out why; if i change the extension to pictures or something it works perfectly.

<?php

define ("MAX_SIZE","1000000");

function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;

if(isset($_POST['Submit']))
{

$image=$_FILES['image']['name'];

if ($image)
{

$filename = stripslashes($_FILES['image']['name']);

$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != "zip"))
{

echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{

$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

$image_name=time().'.'.$extension;

$newname="./zips/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

if(isset($_POST['Submit']) && !$errors)
{
header( 'Location: uploads.html' ) ;
}
?>


<form name="newad" method="post" enctype="multipart/form-data" action="upload.php">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload Zip"></td></tr>
</table>
</form>

Upvotes: 0

Views: 552

Answers (1)

Brandon
Brandon

Reputation: 298

Try using move_uploaded_file instead of copy.

Upvotes: 1

Related Questions