简体   繁体   English

如何解决此 Laravel 问题 - “/tmp/phpY14gRo”文件不存在或不可读?

[英]How to resolve this Laravel issue - The “/tmp/phpY14gRo” file does not exist or is not readable?

I need to create PDF's thumbnail every time it gets loaded via POST method.每次通过 POST 方法加载 PDF 时,我都需要创建它的缩略图。 Once I upload the file inside Controller , it runs getThumb function that uses Imagick to create thumbnail.一旦我将文件上传到Controller中,它就会运行getThumb function ,它使用Imagick创建缩略图。 The problem is that everytime I do that, this request breaks and shows this error - The "/tmp/phpY14gRo" file does not exist or is not readable.问题是每次我这样做时,此请求都会中断并显示此错误- “/tmp/phpY14gRo”文件不存在或不可读。 . .

Imagick is properly installed. Imagick 已正确安装。 I use php-7.2-apache docker image.我使用php-7.2-apache docker 图像。

But if I run shell_excec script that does absolutely the same thing, it works!但是,如果我运行完全相同的shell_excec脚本,它就可以工作! That eliminates all suspicions of the wrong dependency installation这消除了对错误依赖项安装的所有怀疑

Here's the function from my controller:这是我的 controller 中的 function:


    public function createThumb($source, $target, $size = 256, $page = 1)
    {

        if (file_exists($source) && !is_dir($source)): // source path must be available and not be a directory
            if (mime_content_type($source) != 'application/pdf'):
                return FALSE;                // source is not a pdf file returns a failure
            endif;

            $sepa = '/';                // using '/' as file separation for nfs on linux.
            $target = dirname($source) . $sepa . $target;
            $size = intval($size);            // only use as integer, default is 256
            $page = intval($page);            // only use as integer, default is 1

            $page--;                    // default page 1, must be treated as 0 hereafter
            if ($page < 0) {
                $page = 0;
            }            // we cannot have negative values


//It breaks exactly right here

            $img = new Imagick($source . "[$page]"); // [0] = first page, [1] = second page


            $imH = $img->getImageHeight();
            $imW = $img->getImageWidth();
            if ($imH == 0) {
                $imH = 1;
            }            // if the pdf page has no height use 1 instead
            if ($imW == 0) {
                $imW = 1;
            }            // if the pdf page has no width use 1 instead

            $sizR = round($size * (min($imW, $imH) / max($imW, $imH))); // relative pixels of the shorter side

            $img->setImageColorspace(255);        // prevent image colors from inverting
            $img->setImageBackgroundColor('white');    // set background color and flatten
            $img = $img->flattenImages();            // prevents black zones on transparency in pdf
            $img->setimageformat('jpeg');

            if ($imH == $imW) {
                $img->thumbnailimage($size, $size);
            }    // square page
            if ($imH < $imW) {
                $img->thumbnailimage($size, $sizR);
            }    // landscape page orientation
            if ($imH > $imW) {
                $img->thumbnailimage($sizR, $size);
            }    // portrait page orientation

            if (!is_dir(dirname($target))) {
                mkdir(dirname($target), 0777, true);
            } // if not there make target directory

            $img->writeimage($target);
            $img->clear();
            $img->destroy();

            if (file_exists($target)) {
                return $target;
            } // return the path to the new file for further processing
        endif;

        return FALSE;    // the source file was not available, or Imagick didn't create a file, so returns a failure
    }

I thought that it was permission problems but found out that it's not.我以为是权限问题,但发现不是。

Update:更新:

If I initialize Imagick without parameters it won't throw errors and thus won't create thumbnail as it doesn't get file path.如果我在没有参数的情况下初始化Imagick ,它不会抛出错误,因此不会创建缩略图,因为它没有获取文件路径。 So, whenever I add file path and PHP starts searching for that file, and the error occurs.因此,每当我添加文件路径并且 PHP 开始搜索该文件时,就会发生错误。 Inside the log, I noticed that the InvalidArgumentException exception was thrown by a Symfony framework.在日志中,我注意到InvalidArgumentException异常是由Symfony框架引发的。

Here's an image of the error:这是错误的图像: 在此处输入图像描述

After debugging I found out that Imagick was not imported into the project.调试后我发现 Imagick 没有导入到项目中。 So, I just added use Imagick at the top of my Controller .所以,我只是在 Controller 的顶部添加了use Imagick Imagick

In my case i call $validator->fails() two times.就我而言,我两次调用$validator->fails()

In my case, the first call $validator->fails() in my Controller action, after checking deleted the file.就我而言,在检查删除文件后,在我的 Controller 操作中第一次调用$validator->fails() The second call could not find this file.第二次调用找不到这个文件。

暂无
暂无

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

相关问题 无法上传 Laravel 中的图像:““C:\xampp\tmp\php38A9.tmp“文件不存在或不可读。” - Unable to upload image in Laravel: “The ”C:\xampp\tmp\php38A9.tmp“ file does not exist or is not readable.” laravel 文件不存在或不可读 - laravel file does not exist or is not readable laravel 文件上传 - &quot;&quot; 文件不存在或不可读 - laravel file upload - The "" file does not exist or is not readable laravel 文件上传会引发错误“文件不存在或不可读”。 - laravel file upload throws an error “file does not exist or is not readable.” 当图像已上传到文件夹中时,如何解决 laravel 中的“文件不存在或不可读” - how to solve “file does not exist or is not readable” in laravel while the image have been uploaded in folder /tmp/laravel-excel 不存在 - /tmp/laravel-excel does not exist Symfony vichUploader tmp 文件不存在 - Symfony vichUploader tmp file does not exist 如果迁移文件不存在但迁移表中的条目可用,我如何解决迁移问题 - How i can resolve migration issue if migration file does not exist but entry available in migration table &quot;message&quot;: &quot;关键路径 \\&quot;file:///app/storage/oauth-private.key\\&quot; 在 Laravel 6 中不存在或不可读&quot; - "message": "Key path \"file:///app/storage/oauth-private.key\" does not exist or is not readable" in Laravel 6 Laravel生产:文件“ /home/forge/default/storage/app/geoip.mmdb”不存在或不可读 - Laravel production: The file “/home/forge/default/storage/app/geoip.mmdb” does not exist or is not readable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM