...

View Full Version : Please help - how do I check my image file ?



jeddi
11-02-2006, 02:46 PM
Hello all,

I have been using an upload script for a while and it has been ok
but I recently bought a new camera and my pictures wont upload :o

The camera is a Vivitar 3.2 Mega Pix. (pretty basic one)
It appears to produce jpg images.

The script rejects the image with this code:



if(!ereg("image",$_FILES['upLoad1']['type'])) {
$message1 = "The file you have selected for Picture 1 is not";
$message2 = "a recognised picture file - Please try a different file";
require_once ("a_picts_fm.php");
exit();
} // endif


Anyway, my question is : How can I check the file on my pc to see if it will get
rejected by this code :
if(!ereg("image",$_FILES['upLoad1']['type'])) { reject it } ?

Secondly:

Since the other reject reason is dealt by this bit of script:



if($_FILES['upLoad1']['tmp_name'] == "none") {
$message1 = "Picture file 1 did not successfully upload" ;
$message2 = "Check the file size. Must be less than 500K";
require_once ("a_picts_fm.php");
exit();
} // endif


where in my script does it specify a 500k limit ?
Why should a tmp_name = none mean that the file was over 500K ?

Can the limit be increased ? Actually maybe thats the problem ?:confused:

Many Thanks

PS the complete image upload script is below:
(Its a bit long - but thats because it processes 6 pictures)

Do you see any problem with it ?




<?php
/*
*
* Called by section_add.php s
*/

function makeThumbnail($source, $t_ht,$N_pix_n) {
$image_info = getImageSize($source) ;

// echo print_r($image_info);

switch ($image_info['mime']) {
case 'image/gif':
if (imagetypes() & IMG_GIF) { // not the same as IMAGETYPE
$o_im = imageCreateFromGIF($source) ;
} else {
$ermsg = 'GIF images are not supported<br />';
}
break;
case 'image/jpeg':
if (imagetypes() & IMG_JPG) {
exec("jhead -purejpg ".$source); // cleans up jpg headers
$o_im = imageCreateFromJPEG($source) ;
} else {
$ermsg = 'JPEG images are not supported<br />';
}
break;
case 'image/png':
if (imagetypes() & IMG_PNG) {
$o_im = imageCreateFromPNG($source) ;
} else {
$ermsg = 'PNG images are not supported<br />';
}
break;
case 'image/wbmp':
if (imagetypes() & IMG_WBMP) {
$o_im = imageCreateFromWBMP($source) ;
} else {
$ermsg = 'WBMP images are not supported<br />';
}
break;
default:
$ermsg = $image_info['mime'].' images are not supported<br />';
break;
} // end switch

IF (!isset($ermsg)) {
$o_wd = imagesx($o_im) ;
$o_ht = imagesy($o_im) ;
// thumbnail width = target * original width / original height
$t_wd = round($t_ht * $o_wd / $o_ht) ;

if(imageistruecolor($o_im)){
$N_image = @imagecreatetruecolor($t_wd, $t_ht) or die('Invalid resize dimmensions');
imageAlphaBlending($N_image, false); // this disabled so that saveAlpha can be used.
imageSaveAlpha($N_image, true);
}
else{
$N_image = @imagecreate($t_wd, $t_ht) or die('Invalid resize dimmensions');
if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source)))){
$trans = ImageColorAllocate($N_image, $trans['red'], $trans['green'], $trans['blue']);
imagefilledrectangle($N_image, 0, 0, $w - 1, $h - 1, $trans);
imagecolortransparent($N_image, $trans);
}
} // end else

// echo "<br> $t_wd, $t_ht, $o_wd, $o_ht";

imagecopyresampled($N_image, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);

// $destination = "/home/blinyky/public_html/images/$N_pix_n";

$destination = $_SERVER['DOCUMENT_ROOT']."/images/$N_pix_n";
// $destination = 'D\web\images'."\\".$N_pix_n;

// echo "<br>N_image:$N_image. - N_pix_n:$N_pix_n. - Destination: $destination.";

imageJPEG($N_image,$destination,90);
imageDestroy($o_im);
imageDestroy($N_image);

} // end IF
return (isset($ermsg)?$ermsg:NULL);
} // End of Function

require_once("my_functions.php");

$hgt = 120;
$today = date(U);
$pix1_y = $pix2_y = $pix3_y = $pix4_y = $pix5_y = $pix6_y = "n";

// Checking image in pix files.

$N_pix1 = safe_sql($_FILES['upLoad1']["name"]);
$N_pix2 = safe_sql($_FILES['upLoad2']["name"]);
$N_pix3 = safe_sql($_FILES['upLoad3']["name"]);
$N_pix4 = safe_sql($_FILES['upLoad4']["name"]);
$N_pix5 = safe_sql($_FILES['upLoad5']["name"]);
$N_pix6 = safe_sql($_FILES['upLoad6']["name"]);

$ad_ref = safe_sql($_POST["ad_ref"]);
$N_city = safe_sql($_POST["x_city"]);
$section = safe_sql($_POST["section"]);
$N_contact = safe_sql($_POST["x_opt_contact"]);
$N_comp = safe_sql($_POST["x_comp"]);
$N_agent = safe_sql($_POST["x_agent"]);
$N_pass = safe_sql($_POST["x_pass"]);
$N_email = safe_sql($_POST["x_email"]);
$client_status = safe_sql($_POST["client_status"]);
$N_count = safe_sql($_POST["x_count"]);

if($N_pix1 != ""){
if($_FILES['upLoad1']['tmp_name'] == "none") {
$message1 = "Picture file 1 did not successfully upload" ;
$message2 = "Check the file size. Must be less than 500K";
require_once ("a_picts_fm.php");
exit();
} // endif

if(!ereg("image",$_FILES['upLoad1']['type'])) {
$message1 = "The file you have selected for Picture 1 is not";
$message2 = "a recognised picture file - Please try a different file";
require_once ("a_picts_fm.php");
exit();
} // endif
$pix1_y = "y";
} // endif
else{
$N_pix1 = "none";
} // end else

// AND THEN REPEAT SIX TIMES

/*
* Everything seems ok - so we create the thumbnails from the temp_files.
*/

if($pix1_y == "y"){
$image = $_FILES['upLoad1']['tmp_name'];
$N_pix1 = time()."-".$N_pix1;
makeThumbnail($image,$hgt,$N_pix1);
} // endif


// AND THEN REPEAT SIX TIMES

/*
* Update the ADVERT WITH THE IMAGE NAMES
*/

// echo "adref: $ad_ref .";

require_once("Letter_yod.inc");
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to server.");

$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");


$sql = "UPDATE $section SET
image1 = '$N_pix1',
image2 = '$N_pix2',
image3 = '$N_pix3',
image4 = '$N_pix4',
image5 = '$N_pix5',
image6 = '$N_pix6'

WHERE ad_ref = '$ad_ref' ";

mysql_query($sql)
or die("could not execute $section UPDATE PICTURES query");

// echo " <br> client_status: $client_status:";

/*
* Update the client table - WE DO THIS BECAUSE THE WITH PICTS ADVERTS SKIPPED THIS.
*/

if($client_status == "c"){ // IF THEY ARE A CONFIRMED CLIENT
$N_count = $N_count + 1;

$sql = "UPDATE clients SET
contact = '$N_contact',
last_date = '$today',
count = '$N_count',
posted = '$section'

WHERE email = '$N_email' ";

mysql_query($sql)
or die("could not execute CLIENTS ($section) UPDATE query");

/*
* send THANK YOU email
*/
$prog_tp = "add";
$prog = $section."_add.php";
$go_prog = $section."_disp.php";
require_once("$go_prog");
exit();

} // endif

/*
* Add a New client to the table
*/

if($client_status == "N"){ // IF THEY ARE A NEW CLIENT
$mess1 = "";
$N_count = 1;


$sql = "INSERT INTO clients (confirm,comp,agent,city,count,contact,email,pass,create_date,last_date,posted)

VALUES ('$N_confirm','$N_comp','$N_agent','$N_city','$N_count','$N_contact','$N_email','$N_pass',
'$today','$today','$section')";

mysql_query($sql)
or die("could not execute CLIENT INSERT query - $section");

/*
* send ACTIVATION email
*/

require_once("b_email_act.php");

$prog_tp = "add";
$prog = $section."_add.php";
$go_prog = $section."_disp.php";
require_once("$go_prog");
exit();
} // endif
?>

CFMaBiSmAd
11-03-2006, 03:44 AM
For the "type" problem, you need to determine what $_FILES['upLoad1']['type'] contains. The failure message should be modified to echo what is in $_FILES['upLoad1']['type'].

There are two reasons why the 'tmp_name' would be "none" that are due to size -

The uploaded file exceeds the upload maximum size directive in php.ini - upload_max_filesize.

If your form has a hidden field with this, the uploaded file exceeds $_POST['MAX_FILE_SIZE'], the MAX_FILE_SIZE directive in the HTML form.

jeddi
11-03-2006, 04:03 PM
Thanks for your input.

I changed the MAX FILE to:

<input type="hidden" name ="MAX FILE SIZE" value="1000000">

and the error message now read:




echo "File type: ".$_FILES['upLoad1']['type']." <br>";

if($_FILES['upLoad1']['tmp_name'] == "none") {
$message1 = "Picture file 1 did not successfully upload" ;
$message2 = "Check the file size. Must be less than 1MB";
require_once ("a_picts_fm.php");
exit();
} // endif

if(!ereg("image",$_FILES['upLoad1']['type'])) {
$message1 = "The file you have selected for Picture 1 is not a recognised picture file.";
$message2 = "Type = ".$_FILES['upLoad1']['type'];
require_once ("a_picts_fm.php");
exit();
} // endif



Now the strange thing is - these " error if " s don't catch anything.

Everything appears ok but when the makeThumbnail() runs I get errors:


------------------------------------------
File type: image/jpeg

Warning: imagecreatefromjpeg(): '/tmp/phpZwctij' is not a valid JPEG file in /a_pic_updater.php on line 23

Warning: imagesx(): supplied argument is not a valid Image resource in /a_pic_updater.php on line 48

Warning: imagesy(): supplied argument is not a valid Image resource in /a_pic_updater.php on line 49

Warning: Division by zero in /a_pic_updater.php on line 51

Warning: imageistruecolor(): supplied argument is not a valid Image resource in /a_pic_updater.php on line 53
Invalid resize dimmensions

So the file type is the expected "image/jpeg" and as it is under a 1MB I would expect it to pass the to if statements - but why does this line fail ?
$o_im = imageCreateFromJPEG($source) ;

ALL of these are obviously due to an invalid image file.

This is the part of the function that gives the problem:


case 'image/jpeg':
if (imagetypes() & IMG_JPG) {
exec("jhead -purejpg ".$source); // cleans up jpg headers
$o_im = imageCreateFromJPEG($source) ;
} else {
$ermsg = 'JPEG images are not supported<br />';
}

The exec("jhead -purejpg ".$source); is supposed to clean up the file - but maybe it does NOT do the job ???

I have tried the function with this line in AND with it commented out - but I get the same errors.

I don't know where the name '/tmp/phpZwctij' comes from !
The file I upload is named: IMAG0009.JPG

Any sugestions on what is going wrong ?

Please help - as I have no idea how to check the file or what to do with it.

Any thoughts ??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum