可能重复:
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类型等。处理上传的文件时请注意这一事实。 –