开发者

error handling returns blank

开发者 https://www.devze.com 2022-12-10 16:53 出处:网络
I have the following snippet from my code: switch ($extention) { case \"gif\": $src = @imagecreatefromgif($uploadedfile); break;

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 开发者_如何学Cscript terminates but does not return error. Anyone know why?


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)


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.


The @-operator suppresses errors, remove them.


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号