可能重複:
How can I only allow certain filetypes on upload in php?只允許jpg和.png文件只
我怎樣才能讓下面僅腳本允許.jpg圖像或.png圖像? 它根據會話用戶名匹配將圖像目錄上傳到mysql表。是否可以限制文件類型?因爲我只需要.jpg或.png
if ($_POST['submit']) {
//get file attributes
$Name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
error_reporting(E_ERROR | E_PARSE);
if ($Name) {
$location = "avatars/$Name";
move_uploaded_file($tmp_name, $location) or die('Failed to upload picture');
$query = mysql_query("UPDATE fgusers3 SET imagelocation='$location' WHERE name='$name'") or die("Your profile image has been uploaded!");
}}
echo "Upload your image below:
<form action='profilepic.php' method='POST' enctype='multipart/form-data'>
Profile Picture: <input type='file' name='myfile'> <input type='submit' name='submit' value='upload'>
</form>";
請注意有幾種方法來欺騙擴展,MIME類型等。處理上傳的文件時請注意這一事實。 –