2011-08-21 56 views
0

我试图让用户提交zip文件给我。我也想重新命名它们,以免发生冲突。用php上传和重命名zip

此代码不适用于zip文件扩展名。我找不出原因;如果我将扩展名更改为图片或完美的作品。

<?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> 
+0

你的函数'getExtension()'有点奇怪,那么'this.is.a.testcase.zip'这样的文件名呢?更好地使用[finfo](http://php.net/manual/en/function.finfo-file.php)找出你正在处理的文件类型:'$ finfo = new finfo(); $ currentFileMimeType = $ finfo-> file($ fullPathAndFilename,FILEINFO_MIME_TYPE);' – feeela

+0

我已经在我的服务器上测试过你的代码,它的工作完美无瑕。添加'ini_set('display_errors',1);'和error_porting(E_ALL);'检查错误。 –

+0

我同意@feeela。 Tuga显然没有测试任何特别有用的输入。尝试下次更健壮。 –

回答