Skip to content

Bug #81016 imagick throwing out “Uncaught ImagickException: unable to open file” #9549

@mind-bending-forks

Description

@mind-bending-forks

Description

I'm running PHP 8.1.2 on Ubuntu 22.04 with the php-imagick extension installed.

Bug https://bugs.php.net/bug.php?id=81016 was suspended because of no feedback. I'm not the original reporter, but I appear to have the same issue and I have some feedback, so I'd effectively like to reopen it:

On 2021-06-11 @Danack postulated that:

One of you servers is configured to upload files with a user-ownership that works, and another is slightly misconfigured to have not give Imagick access to the file.

In fact, the original poster said

When I run against another test file it works ok.
If it is something about myfile.png that is causing this problem then improved error messages should be given

My interpretation of the original poster's bug report is that they observed this problem with some files, but not others, on the same server.

Certainly, in my case, this happens with some files but not others on the same server. I have not had this issue with two uploaded jpeg files, but I do have this exception thrown with an uploaded svg file.

In the case that is failing, the $_FILES entry for the uploaded file is:

$theFile = [
  'name' => 'some name.svg'
  , 'full_path' => 'some name.svg'
  , 'type' => 'image/svg+xml'
  , 'tmp_name' => '/tmp/phpaEBQOx'
  , 'error' => 0
  , 'size' => 33086
  ];

I don't find the file at "/tmp/phpaEBQOx". Instead, due to a quirk of systemd, I find it at /tmp/systemd-private-...-apache2.service-qabRRN/tmp/phpaEBQOx. (I've deliberately hidden part of the path.) When I check file permissions, I see:

$ ls -l /tmp/systemd-private-...-apache2.service-qabRRN/tmp/phpaEBQOx
-rw------- 1 www-data www-data 33086 Sep 15 16:28 /tmp/systemd-private-...-apache2.service-qabRRN/tmp/phpaEBQOx

This confirms that it is readable and writeable by the web server user and group.

On upload, when the following code is run:

try
{
  $imagickHandle = new \Imagick($theFile['tmp_name']);
}
catch ( \ImagickException $e )
{
  trigger_error("Failed to load Imagick: {$e->getMessage()}");
}

the following is reported in the error log:

PHP Notice: Failed to load Imagick: unable to open file `/tmp/magick-Yih2iBoUnU02Cx6aL0gNy27decidWSnO': No such file or directory @ error/constitute.c/ReadImage/614

So, why do I get this error for some files and not others? What is the file that imagick is referring to in the error message? Is it that imagick does not need to write then read an additional temporary file for some file types (jpeg), but does for others (svg)? Why, using a standard setup, is it having trouble reading and writing temporary files?

Uploading of SVG files used to work with the same setup but with PHP 7.0 on Ubuntu 16.04, so this appears to be some kind of regression, possibly due to changes in the way that temporary files are handled by the OS rather than due to a change in the PHP imagick extension itself. Not sure.

PHP Version

PHP 8.1.2

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions