文件类型检查JPG,JPEG,PNG

时间:2013-05-28 21:51:32

标签: php mysql mysqli

我创建了一个收集客户数据的表单。此数据将发送到MySQL数据库,图像将移动到我服务器上的所需文件夹。

我想要完成的只是允许上传图像。当我将标准设置为GIF时效果很好,但是当我尝试JPG / JPEG / PNG时,它无效。

-

<?php

//Connection String
$con=mysqli_connect("localhost","username","password","database");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//Write to the database
$sql="INSERT INTO Main_Details (driver_ID, driver_image_num, first_name, last_name, dob, license_number, license_image_num, vehicle_badge_image_num, display_image_num, vehicle_number, mobile_number, service_provider, preferred_language, fathers_name, residential_address, city, pin_code, state, work_start, work_stop, preferred_localities, auto_union, badge_number, introduced_by, introducer_tel, license_expiration, rc_book_expiration, fc_expiration, insurance_expiration, insurance_provider, insurance_policy_num, meter_expiration, permit_expiration, emission_expiration, date_signed, form_scanned_by, form_scan_date, form_scan_image_front_num, form_scan_image_back_num, agent_name, agent_id, submitted_to, submitted_on, your_name, todays_date, notes)
VALUES
('$_POST[driver_ID]','$_POST[driver_image_num]','$_POST[first_name]','$_POST[last_name]','$_POST[dob]','$_POST[license_number]','$_POST[license_image_num]','$_POST[vehicle_badge_image_num]','$_POST[display_image_num]','$_POST[vehicle_number]','$_POST[mobile_number]','$_POST[service_provider]','$_POST[preferred_language]','$_POST[fathers_name]','$_POST[residential_address]','$_POST[city]','$_POST[pin_code]','$_POST[state]','$_POST[work_start]','$_POST[work_stop]','$_POST[preferred_localities]','$_POST[auto_union]','$_POST[badge_number]','$_POST[introduced_by]','$_POST[introducer_tel]','$_POST[license_expiration]','$_POST[rc_book_expiration]','$_POST[fc_expiration]','$_POST[insurance_expiration]','$_POST[insurance_provider]','$_POST[insurance_policy_num]','$_POST[meter_expiration]','$_POST[permit_expiration]','$_POST[emission_expiration]','$_POST[date_signed]','$_POST[form_scanned_by]','$_POST[form_scan_date]','$_POST[form_scan_image_front_num]','$_POST[form_scan_image_back_num]','$_POST[agent_name]','$_POST[agent_id]','$_POST[submitted_to]','$_POST[submitted_on]','$_POST[your_name]','$_POST[todays_date]','$_POST[notes]')";


//Error Statement
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }

mysqli_close($con);

//Start customer photograph upload

if ( 6097152 < filesize( $file['upload_picture'] ) ) {

    // File to big.
    echo "Too large a file. Unable to upload.<br>";
    $ok = 2;
}



}

//The problematic part

if (!($uploaded_type=="image/GIF")) {
 echo "You may only upload GIF files.<br> Your file type is";


 $ok=2;
 } 

if ( $ok == 1 ) {

$target = "images/";
$extension = explode(".", $_FILES['upload_picture']['name']);
$extension = $extension[count($extension)-1];
$target = "images/";
$target = $target . $_POST['driver_ID'] . "_P." . $extension;

//Writes the customer photograph to the server
if(move_uploaded_file($_FILES['upload_picture']['tmp_name'], $target))
{

//Tells you if its all ok
echo "Driver's Picture uploaded successfully.<br>";
}}
else {

//Error Statement
echo "Unable to upload the Driver's Picture.";
}





//Start customer license upload
$target = "images/";
$extension = explode(".", $_FILES['upload_license']['name']);
$extension = $extension[count($extension)-1];
$target = "images/";
$target = $target . $_POST['driver_ID'] . "_L." . $extension;

//Writes the license picture to the server
if(move_uploaded_file($_FILES['upload_license']['tmp_name'], $target))
{

//Tells you if its all ok
echo "Driver License uploaded successfully as.<br>";
}
else {

//Error Statement
echo "Unable to upload the Driver's License.<br>";
}


//Start form front upload
$target = "images/";
$extension = explode(".", $_FILES['upload_form_front']['name']);
$extension = $extension[count($extension)-1];
$target = "images/";
$target = $target . $_POST['driver_ID'] . "_FF." . $extension;
//Writes the customer photograph to the server
if(move_uploaded_file($_FILES['upload_form_front']['tmp_name'], $target))
{

//Tells you if its all ok
echo "Front scan of the Form uploaded successfully.<br>";
}
else {

//Error Statement
echo "Unable to upload the Front scan of the Form.<br>";
}


//Start customer form back upload
$target = "images/";
$extension = explode(".", $_FILES['upload_form_back']['name']);
$extension = $extension[count($extension)-1];
$target = "images/";
$target = $target . $_POST['driver_ID'] . "_FB." . $extension;
//Writes the customer photograph to the server
if(move_uploaded_file($_FILES['upload_form_back']['tmp_name'], $target))
{

//Tells you if its all ok
echo "Back scan of the Form uploaded successfully.<br>";
}
else {

//Error Statement
echo "Unable to upload the Back scan of the Form.<br>";
}



echo "All details updated.";
?>

如何使其适用于JPG / JPEG / PNG图像?

1 个答案:

答案 0 :(得分:2)

您应该打开文件并查看其中包含的内容,而不是尝试按扩展名检测文件类型。

$allowed_types = array(IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG);
if (in_array(exif_imagetype($_FILES["upload_picture"]["tmp_name"]), $allowed_types)){
    //is either jpeg, png or gif
}

http://php.net/manual/en/function.exif-imagetype.php