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>
你的函數'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
我已經在我的服務器上測試過你的代碼,它的工作完美無瑕。添加'ini_set('display_errors',1);'和error_porting(E_ALL);'檢查錯誤。 –
我同意@feeela。 Tuga顯然沒有測試任何特別有用的輸入。嘗試下次更健壯。 –