2

I'm on a Mac and I'm trying to create an image in the PHP tmp directory, and then move it to its definitive location.

This is the code I'm using:

public function download()
{
    // Get the remote file extension
    $remoteImageExtension = explode('.', $this->getRemoteUri()->getPath());
    $remoteImageExtension = array_pop($remoteImageExtension);
    $fs = new Filesystem();

    $tempImage = tempnam(sys_get_temp_dir(), 'image.') . '.' . $remoteImageExtension;

    /** @todo: Refact: Pass this as constructor parameter so it will be possible to unit test it */
    $client = new Client();

    // Get and save file
    $client->get($this->getRemoteUri(), ['save_to' => $tempImage]);

    $tempImage = new File($tempImage);

    try {
        if ($fs->exists($tempImage))
        {
            die('Temporary file exists');
            $fs->copy($tempImage, $this->getDownloadRootDir() . $this->getName() . '.' . $remoteImageExtension);
        } else {
            die('Temporary file doesn\'t exist');
        }
    } catch (\Exception $e)
    {
        die($e->getMessage());
    }
    // die(print_r($tempImage));
    // Move the file to its definitive location
    //die($this->getDownloadRootDir() . ' | ' . $this->getName());
    /*try {
        $tempImage->move(
            $this->getDownloadRootDir(),
            $this->getName() . '.' . $remoteImageExtension
        );
    } catch (FileException $e)
    {
        echo $e->getMessage();
    }*/

    // set the path property to the filename where you've saved the file
    $this->path = $this->getName();
}

As you can see I've put some die() in the code to print some information during the execution.

Doing this, I know the temporary file is correctly created, but it isn't moved to its new location.

I've read around that it could be a problem of permissions on destination folder, but, changing them with returns me an error about an illegal user (I'm on a Mac!):

$ chown -R www-data /Users/Aerendir/Documents/JooServer/_Projects/path/to/symfony_project/web/images/path/to/destination_folder
chown: www-data: illegal user name

I have tried both the Filesystem::copy() and the File::move() methods (as you can see from the source code provided), but the file isn't moved.

Any ideas about how to solve this?

UPDATE Trying to know which is my current Apache user I see it is correctly set to my main system user (Aerendir).

Community
  • 1
  • 1
Aerendir
  • 6,152
  • 9
  • 55
  • 108

0 Answers0