表單必須用 enctype="multipart/form-data"
代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>
</head>
<body>
<form action="./upload_process.php" method="post" enctype="multipart/form-data">
<div>
<input type="file" name="user_file" />
</div>
<div>
<input type="submit" />
</div>
</form>
</body>
</html>
儲存檔案時,檢查檔案類型,本來想使用$_FILES["user_file"]["type"],結果發現在各個瀏覽器下不一致。
只能使用檔案名稱檢查。
$_FILES["user_file"]["type"]
firefox下:application/zip 或 application/octet-stream
ie下:application/x-zip-compressed
chrome下:空
<?php
var_dump($_FILES["user_file"]);
$tmp = explode('.',$_FILES["user_file"]["name"]);
$last = count($tmp) -1;
if($tmp[$last]!='zip')
{
echo 'only allow zip';
exit;
}
$new_name_and_path = './files/'.md5($_FILES["sns_file"]["tmp_name"]).'.zip';
move_uploaded_file($_FILES["user_file"]["tmp_name"],$new_name_and_path);
?>