2013-05-28 54 views
-2

我创建了一个收集客户数据的表单。这些数据被发送到MySQL数据库,图像移动到我的服务器上的所需文件夹。文件类型检查JPG,JPEG,PNG

我想要完成的只是允许上传图片。当我将标准设置为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图像的工作吗?

+0

你需要设置的接受文件的数组。有这样的例子等 –

+0

你如何尝试将其设置为GIF/JPG/PNG? – slugonamission

+2

请定义“不工作”。你期望发生什么,目前发生了什么,以及你遇到什么问题阻止你完成它? –

回答

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

+0

PHP手册只显示版本5。可能不适用于OP。我在运行5.3的主机上尝试了这一点,但它不起作用。必须使用'getimagesize'。 –

+0

@Fred在页面中链接的答案,'(PHP 4> = 4.3.0,PHP 5)' – Musa

相关问题