【发布时间】:2015-02-26 08:56:06
【问题描述】:
我有一个上传脚本,它就像上传文件的魅力一样。我还在脚本中设置了上传大小限制。但是我想在我的脚本中设置文件类型限制,但我在实现它时感到困惑。谁能帮我这个?我只想允许 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 专家请帮助我。
【问题讨论】:
-
创建一个可接受的 mime 类型数组,并检查您的
$type与in_array()数组中的值
标签: php html ajax post file-upload