简体   繁体   中英

Symfony2's HttpFoundation/File/File.php cannot find temporary file when uploading a document (in MAMP)

I'm using MAMP on my local mac to test a Symfony2 application. I'm trying to upload a document, but keep running into a system 500 error : 'The file "/Applications/MAMP/tmp/php/[...]" does not exist.'

This is either an issue with my Symfony2 config or MAMP config - I'm not sure which. The script itself succeeds in uploading a file:

$req = Request::createFromGlobals();                
$uploadedFile = $req->files->get($file);
$file = time().'_'.str_replace(' ','_',$uploadedFile->getClientOriginalName());     

if (!is_dir($this->uploads_dir))
{
    mkdir($this->uploads_dir);
}

if ($uploadedFile->move($this->uploads_dir, $file))
{
    $this->session->set('van.uploaded_file', $this->uploads_dir.$file);
}
else
{
   $this->error[] = 'upload.failed';
}

$this->uploads_dir is simply '/uploads/' and indeed each time I run the script (despite the 500 errors) my files are loaded into the /uploads directory.

I'm not sure that it matters, but the script above is run from a service class (rather than from a controller.)

I have checked the /Applications/MAMP/tmp/php/ directory and there are no temporary upload files there. (But that may be because they have been moved to my /uploads folder!)

I've set the permissions for the /Applications/MAMP/tmp/php/ directory to 777 and still no luck.

I commented out the upload_tmp_dir in my php.ini file but that just triggers the same error for a different default directory on my computer:

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).

; upload_tmp_dir = /Applications/MAMP/tmp/php

Here is the exact error from Symfony's error log:

CRITICAL - Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception
\FileNotFoundException: "The file "/Applications/MAMP/tmp/php/phpYew2rT" does not exist" at 
/Applications/MAMP/htdocs/tdp2013/Symfony/vendor/symfony/symfony/src/Symfony/Component
/HttpFoundation/File/File.php line 41

Any other ideas?

UPDATE:

I modified the script to upload the document using move_uploaded_file instead of Symfony's File component. I received the same error - meaning this is an issue with my MAMP configuration, not Symfony.

public function upload($file_name)
{
    $this->session->remove('van.uploaded_file');

    if (!is_dir($this->uploads_dir))
    {
        mkdir($this->uploads_dir);
    }

    $file = $this->uploads_dir.time().'_'.str_replace(' ','_',$_FILES[$file_name]["name"]);
    move_uploaded_file($_FILES[$file_name]["tmp_name"], $file);

    if (file_exists($file))
    {
        $this->session->set('van.uploaded_file', $file);
    }
    else
    {
        $this->error[] = 'upload.failed';
    }
    return $this;
}

Here are my php.ini settings:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).

upload_tmp_dir = /Applications/MAMP/tmp/php

; Maximum allowed size for uploaded files.
upload_max_filesize = 32M

What size is the file? I have had similar errors in the past and this was the cause.

I do not know about mamp but PHP, in general, has a 2MB file upload limit by default. In order to increase it you will need to modify 2 settings in your php.ini

post_max_size = 16MB
upload_max_filesize = 16MB

Of course you can put any value in stead of 16. If you are unable or unwilling to modify php.ini you may be able to change the values in php code as explained here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM