2013-05-28 84 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

相關問題