用php上传照片的最佳方式:允许67108864字节的内存耗尽


Best way to upload photos with php : Allowed memory size of 67108864 bytes exhausted

在我的网站上,用户可以上传自己的照片。php代码将它们存储在一个图像中,制作一个微型图像,重新命名它们并将它们存储在网站上。照片必须是2MB,但是一个用户最近告诉我,当他试图上传一张900KB的照片时,出现了致命错误"允许的内存大小为67108864字节已耗尽"(你可以猜到,这是一张又大又宽但压缩得很好的JPEG)。

在我的共享主机服务器上,我不能更改php.ini。上传代码目前看起来像这样:

            $source = imagecreatefromjpeg($value['tmp_name']);
            $width_source = imagesx($source);
            $height_source = imagesy($source);
            $max = max($width_source,$height_source);
            $ratio = $max/min($max,1000);
            $width_destination = $width_source/$ratio;
            $height_destination = $height_source/$ratio;
            $width_miniature = 100*$width_source/$height_source;
            $height_miniature = 100;
            $destination = imagecreatetruecolor($width_destination, $height_destination);
            $adress = '/home/photos/photo'.$id.'.jpg';
            imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur_destination, $hauteur_destination, $largeur_source, $hauteur_source);
            imagejpeg($destination, $adress);
            chmod($adresse, 0644);
            $miniature = imagecreatetruecolor($width_miniature, $height_miniature);
                                            etc etc to copy miniature

是否有其他方式/方法上传大照片或绕过php内存限制?

您可以使用ini_set方法动态更改ini设置

ini_set('memory_limit','128M');

如果你不能使用这个方法,它将返回false而不是设置值。