简体   繁体   English

试图在本地上传1M文件,我收到致命错误

[英]Trying to upload a 1M file locally and i get a Fatal Error

"Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 9828 bytes)". “致命错误:允许的内存大小为31457280字节耗尽(尝试分配9828字节)”。

This is the error i get but I am only trying to upload a 1mb image. 这是我得到的错误,但我只是尝试上传1mb图像。 I have increased the memory limit in php.ini and the execution time. 我增加了php.ini中的内存限制和执行时间。 I am trying this on a local MAMP server, on a Mac using firefox. 我在使用firefox的Mac上的本地MAMP服务器上尝试此操作。 This going to be for an online image gallery. 这将是一个在线图片库。 Any ideas? 有任何想法吗? Below is the code: 以下是代码:

    ini_set("memory_limit","30M");
    if(isset($_POST['submit'])){
      if (isset ($_FILES['new_image'])){
          $imagename = $_FILES['new_image']['name'];
          $source = $_FILES['new_image']['tmp_name'];
          $target = "images/".$imagename;
          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          //below here for the removed code


          $save = "thumbs/uploads/" . $imagepath; //This is the new file you saving
          $file = "images/" . $imagepath; //This is the original file
          $imagesize = getimagesize($file);

          list($width, $height) = $imagesize; 

          unset($imagesize);

          if($width>$height)
            {
                $modwidth = 150; 
                $diff = $width / $modwidth;
                $modheight = $height / $diff;
            }else{
                $modheight = 150; 
                $diff = $height / $modheight;
                $modwidth = $width / $diff;
            }
          $tn = imagecreatetruecolor($modwidth, $modheight); 
          $image = imagecreatefromjpeg($file); 
          $imagecopy = imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 

          imagedestroy($image);
          imagedestroy($im);
          imagedestroy($imagecopy);
          imagedestroy($source);

          $imagejpg = imagejpeg($tn, $save, 100); 
            imagedestroy($tn);
          imagedestroy($imagejpg);

EDIT 编辑

This has now been sorted out hopefully. 现在已经有希望了解这一点。 One of my colleagues had a solution all along but neglected to tell me! 我的一位同事一直都有解决方案,却忽略了告诉我!

You're likely loading the image to do some manipulation of it. 您可能正在加载图像以对其进行一些操作。 That causes the image data to be decompressed, which requires a lot of memory for big images (I think it's about 4 bytes per pixel). 这导致图像数据被解压缩,这需要大量的内存用于大图像(我认为每个像素大约4个字节)。

You can choose to either not process the image, or do your processing outside of PHP - for example by invoking ImageMagick or some other program. 您可以选择不处理图像,也不选择在PHP之外进行处理 - 例如通过调用ImageMagick或其他程序。 It depends a bit on what you're trying to accomplish. 这取决于你想要完成的事情。

It's nothing to do with the (file)size of the image you're uploading, the call that's breaking your memory limit is imagecreatetruecolor(). 它与您上传的图像的(文件)大小无关,打破内存限制的调用是imagecreatetruecolor()。

imagecreatetruecolor() will allocate an area of memory to store a true colour image in with no compression, and use 32 bits (4 bytes) per pixel. imagecreatetruecolor()将分配一个内存区域,以便在没有压缩的情况下存储真彩色图像,并且每个像素使用32位(4个字节)。

So for a 1024x768 pixel image, for example, imagecreatetruecolour() will use up 1024*768*4 = 3145728 bytes, or around 3MB. 因此,对于1024x768像素的图像,例如,imagecreatetruecolour()将耗尽1024 * 768 * 4 = 3145728字节,或大约3MB。

The problem is that this scales up quite dramatically. 问题是,这种情况非常显着。 A 3072x2034 (6 megapixel) image, by the same sort of calculation, needs around 24MB just to be loaded into memory - I would guess this is the sort of size you're dealing with. 一个3072x2034(600万像素)的图像,通过相同的计算,需要大约24MB才能加载到内存中 - 我猜这是你正在处理的那种大小。

The solution is to hand off the processing to something else like imagemagick or NetPBM that will run as a separate process and not count towards the PHP memory limit. 解决方案是将处理交给像imagemagick或NetPBM这样的其他东西,它将作为一个单独的进程运行,而不是计入PHP内存限制。

NetPBM is my personal favourite and would look something like: NetPBM是我个人的最爱,看起来像是:

anytopnm <file> | pnmscale -xysize <dimensions> | pnmtojpg  > <outfile> 

As mentioned here : 如前所述这里

don't forget the imagedestroy () function, or caching your thumbnails - they'll save you a LOT of work down the road. 不要忘记imagedestroy ()函数,或缓存你的缩略图 - 它们将为你节省大量的工作。

Did you restart apache after you increased the memory limit? 在增加内存限制后重启apache了吗? If yes, then increase a little more. 如果是,那么再增加一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:尝试将文件上传到 AWS 时未捕获 InvalidArgumentException - Fatal error: Uncaught InvalidArgumentException when trying to upload file to AWS 上传文件CakePHP致命错误 - Upload file CakePHP Fatal Error 尝试上传 .doc 文件,但我总是收到请求超时 - Trying to upload a .doc file but I'm always getting a Request Timeout 我正在尝试使用 AJAX 和 PHP 上传多张图片,但出现错误 - I'm trying to upload multiple images using AJAX and PHP but there is an error PHP Mailer:我在尝试通过表单上传文件并将其作为附件发送时收到错误“无法访问文件” - PHP Mailer: I'm getting an error "Could not access file" while trying to upload the file through form and sending it as a attachment 我正在尝试使一个函数(按钮)将图像上传到CodeIgniter(PHP)中的数据库。 但是我出错了 - I'm trying to make a function (button) to upload images to my database in CodeIgniter (PHP). But I'm getting an error 我正在尝试使用任何Web编程语言在我的PC中本地创建和存储文件 - I'm trying to creat and store a file locally in my pc using any web programing language PHP致命错误:我正在尝试在Symfony2应用程序中实现Form \\ AbstractType - PHP Fatal error: I'm trying to implement a Form\AbstractType in my Symfony2 application PHP致命错误:我尝试安装的脚本中无法访问空属性 - PHP Fatal error: Cannot access empty property in a script I'm trying to install 我正在尝试在magento的产品页面上显示面包屑,但这给了我一个致命错误 - I'm trying to display breadcrumbs on product page in magento but it gives me a fatal error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM