...

View Full Version : $_FILES error problem



Digicoder
01-22-2008, 12:17 AM
I've been doing image work lately and normally I wouldn't like people re upload their images, due to these kinds of troubles.

At any rate, I've been using the following code to try and stop the files from being replaced, as the re-upload is optional, if some one doesn't enter the field.

if($_FILES['image_link']['error'] != UPLOAD_ERR_OK)
return;

Turns out that doesn't work.
if the field isn't entered then the error would normally be 4. I can't figure out why it isn't working.

Here is the rest of the actual image processing code.


function process_image()
{
$upload_dir = 'image/reviews/';
$filename = $_FILES['image_link']['tmp_name'];
$new_side = 75;


if($_FILES['image_link']['error'] != UPLOAD_ERR_OK)
return;

if(!is_uploaded_file($filename))
fatal_error('Sorry, you are obviously not trying to upload a picture.');

if (!extension_loaded('gd')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'gd.' . PHP_SHLIB_SUFFIX);
}

list($width, $height, $type) = getimagesize($filename);
$imagetype = image_type_to_mime_type($type);


switch($imagetype)
{
case "image/jpeg":
case "image/jpg":
$extension = '.jpg';
$newfile = $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;

$w = $width;
$h = $height;

if ($h > $w) {
$new_w = ($new_side / $h) * $w;
$new_h = $new_side;
} else {
$new_h = ($new_side / $w) * $h;
$new_w = $new_side;
}

$im2 = imagecreatetruecolor($new_w, $new_h);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
imagejpeg($im2, $newfile, 75);

return $newfile;

break;

case "image/gif":
$extension = '.gif';
$newfile = $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;

$w = $width;
$h = $height;

if ($h > $w) {
$new_w = ($new_side / $h) * $w;
$new_h = $new_side;
} else {
$new_h = ($new_side / $w) * $h;
$new_w = $new_side;
}

$im2 = imagecreatetruecolor($new_w, $new_h);
$image = imagecreatefromgif($filename);
imagecopyresampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
imagegif($im2, $newfile);

return $newfile;

break;

case "image/png":
$extension = '.png';
$newfile = $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;

$w = $width;
$h = $height;

if ($h > $w) {
$new_w = ($new_side / $h) * $w;
$new_h = $new_side;
} else {
$new_h = ($new_side / $w) * $h;
$new_w = $new_side;
}

$im2 = imagecreatetruecolor($new_w, $new_h);
$image = imagecreatefrompng($filename);
imagecopyresampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
imagepng($im2, $newfile, 0, PNG_NO_FILTER);

return $newfile;

break;

case "image/bmp":
fatal_error('Please, do NOT upload any .bmp (MS paint or equivalent) files. They can not be used in the server.');
break;
default:
fatal_error('Please take note, you have uploaded an invalid file. The site only accepts the following images. '."\n".
'.jpg/.jpeg, .gif, and .png type files. ','Attention');
break;
}

}


I get a feeling that maybe I should just stick to numbers, the constant may not be a constant with my host...
Thanks for the help guys.

oesxyl
01-22-2008, 12:40 AM
if($_FILES['image_link']['error'] != UPLOAD_ERR_OK)
return;


try this:


if($_FILES['image_link']['error'] !== UPLOAD_ERR_OK)
return;

best regards

Digicoder
01-22-2008, 12:42 AM
Nope didn't work.

Any other suggestions?

oesxyl
01-22-2008, 01:02 AM
Nope didn't work.

Any other suggestions?

1. try somewhere outside the function a:


print_r($_FILES);


to see the valus.

2. check if 'if' work:



if($_FILES['image_link']['error'] != UPLOAD_ERR_OK) {
print "This work";
return;
}


3. it's better to pass the value as argument to the function the using with file scope, so you can try

if consider:



$filename = $_FILES['image_link']['tmp_name'];
$okupload = $_FILES['image_link']['error'];


become:



function process_image($filename, $okupload)
{
$upload_dir = 'image/reviews/';
$new_side = 75;

if($okupload != UPLOAD_ERR_OK)
return;


or pass them as array.

4. is paranoic but you could check, :)



if(UPLOAD_ERR_OK == 4)
print "This work";


I don't have anything else in mind, :).

best regards

Digicoder
01-22-2008, 01:04 AM
Well, see that the problem I did check it that way.

the error returned 4.
I think that its the version of PHP really.
Don't know when the constants where added but the version my host is using is 5.1.4 I believe.

oesxyl
01-22-2008, 01:12 AM
Well, see that the problem I did check it that way.

the error returned 4.
I think that its the version of PHP really.
Don't know when the constants where added but the version my host is using is 5.1.4 I believe.

PHP 4.2.0:

http://www.php.net/manual/en/features.file-upload.errors.php

get info about php:

upload on server a file with:



<?php phpinfo(); ?>


best regards

Digicoder
01-22-2008, 01:13 AM
Gah, I feel stupid now.

I remembered that not to long about the way I have used some of the operators in my scripts stopped working correctly.

The problem I've been having all along was that


if($_FILES['image_link']['error'] == UPLOAD_ERR_OK)
{
//...


is what works.

Thanks for the help, I should probably take a nap or something and set my mind straight again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum