简体   繁体   English

上传文档时(在MAMP中),Symfony2的HttpFoundation / File / File.php无法找到临时文件

[英]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. 我在本地Mac上使用MAMP来测试Symfony2应用程序。 I'm trying to upload a document, but keep running into a system 500 error : 'The file "/Applications/MAMP/tmp/php/[...]" does not exist.' 我正在尝试上载文档,但继续遇到系统500错误:“文件“ / Applications / MAMP / tmp / php / [...]”不存在。

This is either an issue with my Symfony2 config or MAMP config - I'm not sure which. 这是我的Symfony2配置或MAMP配置的问题-我不确定是哪一个。 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. $ this-> uploads_dir只是'/ uploads /',实际上,每次我运行脚本时(尽管有500个错误),我的文件都被加载到/ uploads目录中。

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. 我已经检查了/ Applications / MAMP / tmp / php /目录,并且那里没有临时上传文件。 (But that may be because they have been moved to my /uploads folder!) (但这可能是因为它们已移至我的/ uploads文件夹!)

I've set the permissions for the /Applications/MAMP/tmp/php/ directory to 777 and still no luck. 我已经将/ Applications / MAMP / tmp / php /目录的权限设置为777,仍然没有运气。

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: 我注释掉了我的php.ini文件中的upload_tmp_dir,但这只是为计算机上的其他默认目录触发了相同的错误:

; 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: 这是Symfony的错误日志中的确切错误:

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. 我修改了脚本,使用move_uploaded_file而不是Symfony的File组件上载文档。 I received the same error - meaning this is an issue with my MAMP configuration, not Symfony. 我收到了相同的错误-这是我的MAMP配置而不是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: 这是我的php.ini设置:

;;;;;;;;;;;;;;;;
; 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. 我不了解Mamp,但PHP默认情况下的文件上传限制为2MB。 In order to increase it you will need to modify 2 settings in your php.ini 为了增加它,您将需要在php.ini中修改2个设置

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 当然,你可以把任何价值在16代替如果您无法或不愿修改php.ini文件你可以改变PHP代码值作为解释这里

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

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