PHP代码在上传之前控制图像的大小,扩展名和格式

时间:2017-01-25 22:59:00

标签: javascript php

我需要该代码才能上传图片文件,当您上传文件扩展名(如.pdf)时,要拒绝该文件的代码

if((isset($_FILES['file'])) && ($_FILES['file']['name']!='"')) { echo 'isset'.$_FILES['file']['name'];
$folder = 'upload/';
$file = basename($_FILES['file']['name']);
$size_maxi = 2000000;
$size = filesize($_FILES['file']['tmp_name']);
$extensions = array('.png', '.gif', '.jpg', '.jpeg');
$extension = strrchr($_FILES['file']['name'], '.');
//Start the file type verification
//If the extension is not in table 
if(!in_array($extension, $extensions)){
$error = 'You must make use of file in the following forma type png, gif, jpg, jpeg'; }
elseif($size>$size_maxi)
{
$error = 'File size is above allowed limitations...';
}
//If no error detected, proceed to upload 
else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $file)) {
    echo "The file ". basename( $_FILES["file"]["name"]). " has been    uploaded.";

     //Writes the information to the database
 mysql_query("INSERT INTO picture (P_file,P_type,P_size)
 VALUES ('$file', '$extension','$size')"); 
 } else {
    echo "Sorry, there was an error uploading your file.";
 }
}

}

0 个答案:

没有答案