JPG,JPEG,PNG的文件类型检查 [英] File Type Check for JPGs, JPEG, PNGs

查看:58
本文介绍了JPG,JPEG,PNG的文件类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a form that collects customer data. This data is sent to the MySQL database and the images move to the required folder on my server.

我要完成的工作只是允许上传图片.当我将标准设置为GIF时,效果很好,但是当我尝试JPG/JPEG/PNG时,效果不佳.

What I am trying to accomplish is only allowing images to be uploaded. When I have criteria set to GIF it works well but when I try JPG/JPEG/PNG it's not working.

-

<?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图像?

How can I make it work for JPG/JPEG/PNG images?

推荐答案

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

Instead of try to detect the file type by extension, you should open the file and see what's in it.

$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

这篇关于JPG,JPEG,PNG的文件类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆