我有一个上传脚本,就像上传文件的魅力一样。我还在脚本中设置了上传大小限制。但我想在我的脚本中设置文件类型限制,但我很难实现它。谁能帮我这个?我想只允许JPG,JPEG,PNG,GIF,PDF和DOC文件。我怎样才能做到这一点?请帮忙。
这是我的上传脚本。
<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("project101test",$conn) or die(mysql_error());
if(isset($_POST['submit'])!=""){
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
$limit_size=512000; // Define file size limit in Bytes.
$size_in_kb=1024; // File size in KB
$divide=$limit_size/$size_in_kb; // Dividing both the variables to get the size in KB.
if($size > $limit_size){
echo "Your file size is over limit<BR>";
echo "<BR>File size limit = $divide KB";
}
else {
move_uploaded_file($temp,"admin/files/".$name);
$insert=mysql_query("insert into upload(name, fname, phone, email, message)values('$name','$_POST[fname]','$_POST[phone]','$_POST[email]','$_POST[message]')");
}
if($insert){
echo "File Uploaded";
}
else{
die(mysql_error());
}
}
?>
PHP专家请帮助我。
答案 0 :(得分:1)
尝试使用文件扩展名
检查条件$extension = substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.'));
$extension = strtolower($extension);
if( $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" ||$extension == ".png" )
{
// File upload code
}