使用PHP上传图片并达到脚本内存限制 [英] Uploading images with PHP and hitting the script memory limit

查看:330
本文介绍了使用PHP上传图片并达到脚本内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP脚本上传JPG图片,但图片不断导致我的脚本超时并死于出现此错误:

I am trying to upload a JPG image using a PHP script, but the image is keeps causing my script to time out and die giving this error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 
2136 bytes) in image.php on line 38

如果图片太大,或者这个错误是否正常失败,如何防止上传发生?

How can I prevent the upload from taking place if the image is too big, or have this error fail gracefully?

推荐答案

实际的问题不是初始文件大小,而是图像本身的尺寸(height * width * colorDepth),因为你没有访问这些信息之前文件已安全上传,您应该可以使用以Flash,Java或Silverlight编写的上传器,因为他们可以事先访问此信息。

The actual problem is not with the initial file size but with the dimensions of the image itself (height*width*colorDepth), since you don't have access to this information before the file has been uploaded for security reasons, you should probably use an uploader written in Flash, Java or Silverlight since they DO have access to this information beforehand.

上传后可以通过使用 getimagesize ,并使用返回的宽度,高度和位深度。

After the upload you can check if the image will exceed your memory limit by calculating the dimensions of the (uncompressed) image by using getimagesize and using the returned width, height and bit depth.

运气最好。

这篇关于使用PHP上传图片并达到脚本内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆