isset() Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

isset() Problem

Post by Jim_Bo »

Hi,

In the following code once I introduced if (isset($_POST['photo_filename'])) { } on the photo_filename (file field) if I add a picture that part of the script still isnt exicuted .. Been a file field is a differend function needed?

code:

Code: Select all

$item = $_REQUEST['item'];
$desc = $_REQUEST['desc'];
$code = $_REQUEST['dode'];
$price = $_REQUEST['price'];
$stock = $_REQUEST['stock'];
$link = $_REQUEST['link'];

	$sql = "INSERT INTO items (item, desc, price, code, picture, link, stock) VALUES ('$item', '$desc', '$price', '$code', '0', '$link', '$stock')";
	$result = mysql_query($sql);

if (isset($_POST['photo_filename'])) {

$images_dir = 'images/products';

$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif');
        
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'gif');

$photos_uploaded = $_FILES['photo_filename'];
$photoFileName = $_FILES['photo_filename'];

if (!array_key_exists($photos_uploaded['type'], $known_photo_types)) {
	require 'preupload.php';
	echo '<center><font color="red"><b>File is not a photo!</b></font></center><br>';
	return;
 }                    
									
	$new_id = mysql_insert_id();
	$filetype = $photos_uploaded['type'];
    $extention = $known_photo_types[$filetype];
    $filename = $new_id.".".$extention;

	$sql = "UPDATE items SET picture='$filename' WHERE itemId='$new_id'";
	$result = mysql_query($sql);
								
	copy($photos_uploaded['tmp_name'], $images_dir."/".$filename);
                             
	$size = GetImageSize( $images_dir."/".$filename );

	$Config_tbwidth_wide = 80; // width of wide image
	$Config_tbheight_wide = 80; // height of wide image

	$Config_tbwidth_tall = 80; // width of tall image
	$Config_tbheight_tall = 80; // height of tall image

if ($size[0] > $size[1]) {
	$thumbnail_width = $Config_tbwidth_wide;
	$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);

if ($thumbnail_height > $Config_tbheight_wide) {
	$thumbnail_height = $Config_tbheight_wide;
	$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
}
        
}else{
		
	$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
	$thumbnail_height = $Config_tbheight_tall;

if ($thumbnail_width > $Config_tbwidth_tall) {
	$thumbnail_width = $Config_tbwidth_tall;
	$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
 }
}
	$function_suffix = $gd_function_suffix[$filetype];
	$function_to_read = "ImageCreateFrom".$function_suffix;
	$function_to_write = "Image".$function_suffix;

	$source_handle = $function_to_read ( $images_dir."/".$filename );

if ($source_handle) {

	$destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );

	ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
}
	$function_to_write( $destination_handle, $images_dir."/tb_".$filename, 100 );
	ImageDestroy($destination_handle );

if ($size[0] > '100') {
	$Config_width_wide = 500; // width of wide image
	$Config_height_wide = 475; // height of wide image

	$Config_width_tall = 475; // width of tall image
	$Config_height_tall = 500; // height of tall image

if($size[0] > $size[1]) {
	$image_width = $Config_width_wide;
	$image_height = (int)($Config_width_wide * $size[1] / $size[0]);

if ($image_height > $Config_height_wide) {
	$image_height = $Config_height_wide;
	$image_width = (int)($Config_height_wide * $size[0] / $size[1]);
}
}else{
	$image_width = (int)($Config_height_tall * $size[0] / $size[1]);
	$image_height = $Config_height_tall;

if ($image_width > $Config_width_tall) {
	$image_width = $Config_width_tall;
	$image_height = (int)($Config_width_tall * $size[1] / $size[0]);
 }
}
	$function_suffix = $gd_function_suffix[$filetype];
	$function_to_read = "ImageCreateFrom".$function_suffix;
	$function_to_write = "Image".$function_suffix;

	$source_handle = $function_to_read ( $images_dir."/".$filename );

if ($source_handle) {

	$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

	ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}
	$function_to_write( $destination_handle, $images_dir."/".$filename, 90 );
	ImageDestroy($destination_handle );
 }
}
Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you should use is_uploaded_file() anyway to ensure that there's a file there.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Thanks .. Still if I comment out

if (is_uploaded_file($_POST['photo_filename'])) { }

The rest of the script exicutes fine, if I uncomment it the picture side of the script fails?

I thought that was the way to acheive it, I also tried !=NULL


Cheers
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to use the $_FILES[] array with is_uploaded_file().

see the example on the link I posted above.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

do you mean

if (is_uploaded_file($_FILES['photo_filename'])) { }

Have tried that too, photo_filename is the field name for the file field in the form ..


Cheers
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

if (is_uploaded_file($_FILES['photo_filename']['tmp_name'])) { }

also make sure you have the enctype set correctly on the form...
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

:oops: damn that was writen in the top of the manual page is_uploaded_file(), and I read it :oops:


Thanks that works
Post Reply