views:

47

answers:

4

I have the following snippet from my code:

switch ($extention)
{
 case "gif": $src = @imagecreatefromgif($uploadedfile); break;
 case "jpeg": $src = @imagecreatefromjpeg($uploadedfile);  break;
 case "png": $src = @imagecreatefrompng($uploadedfile); break;
 default: $src = @imagecreatefromjpeg($uploadedfile);  break;
}

if(!$src)
 die("Error: Could not upload image code:#011");

The script terminates but does not return error. Anyone know why?

A: 

The @-operator suppresses errors, remove them.

chelmertz
I know, used for if you handle the errors yourself (bottom 2 lines)
Phil Jackson
@Phil No, but I guess you figured that out by now :) A pointer, always debug without error supression and `error_reporting(E_ALL|E_STRICT)`.
chelmertz
+1  A: 

Where does it end? If that's your whole script, it's quite natural that you don't get any output (you're not outputting anything). Try putting echo-statements here and there do find the line where the script breaks.

EDIT after clarification in answers:

You can't catch the out-of-memory error in PHP, it is simply impossible (anything you would do would require more memory anyway). You can't even check if the memory would be available beforehand, since you cannot know how much memory a 10kb jpeg-image would require (it depends on the dimensions of the image, its color depth, etc.)

The only way I can think of handling such errors is making the operation outside of PHP (calling another script using exec() or doing it with imagemagick using exec() or similar)

soulmerge
Thank you i understand now
Phil Jackson
A: 
echo "bar";
switch ($extention)
{
    case "gif": $src = @imagecreatefromgif($uploadedfile); echo "foo"; break;
    case "jpeg": $src = @imagecreatefromjpeg($uploadedfile); echo "foo"; break; // best quality
    case "png": $src = @imagecreatefrompng($uploadedfile); echo "foo"; break; // no compression
    default: $src = @imagecreatefromjpeg($uploadedfile); echo "foo"; break; // best quality
}

echo "foo";
if(!$src)
    die("Error: Could not upload image code:#011");
echo "foo foo";

returns only "bar"

Phil Jackson
You should edit your question next time to provide the additional information (sop people don't have to look into the answers section to get the whole question.) What happens if you remove the `@`-operator?
soulmerge
how am i supposed to input readable code into here?
Phil Jackson
Just put it in, we'll do the formatting
soulmerge
A: 

As has been suggested temporarily remove the @ symbols to find out which imagecreatefrom... is breaking your script, once you've debugged it put them back in. Also because you've echoed foo after the imagecreatefrom... function that is breaking the process it'll never print out anyway.

EDIT AFTER COMMENTS:

Okay so if you want to handle the errors yourself just using @ won't work, it'll suppress the message but not the fact that a fatal error occurred. You'll need to set up an error handler, have a look here for information about this.

RMcLeod
it is the jpeg case
Phil Jackson
okay and what is the error that is being thrown?
RMcLeod
nothing thats my point. take away the @ symbol and returns Fatal error: Out of memory (allocated 31981568) (tried to allocate 13056 bytes) in /home/sites/snowyswebsolutions.co.uk/public_html/ACTfileUploader/uploader.php on line 90i want to supress that error and return my own but it returns blank
Phil Jackson
Thank you i understand now
Phil Jackson