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

вам необходимо установить массив принимаемых файлов. Есть примеры для SO и т. Д. –

+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. Может не работать для ОП. Я пробовал это на моем хосте, работающем 5.3, и это не сработало. Пришлось использовать 'getimagesize'. –

+0

@Fred на странице, связанной с ответом, '(PHP 4> = 4.3.0, PHP 5)' – Musa

Смежные вопросы