我正在使用Symfony 2.3保存由表单POST上传的文件。
这是我在控制器中使用的代码:
$fileDir = '/home2/divine/Symfony/src/App/Bundle/Resources/public/files';
$form['my_file']->getData()->move($fileDir, 'book.pdf');
在水下,Symfony执行此代码来移动文件:
move_uploaded_file("/tmp/phpBM9kw8", "/home2/divine/Symfony/src/App/Bundle/Resources/public/files/book.pdf");
公共目录具有777权限。
这是我得到的错误:
"Could not move the file "/tmp/phpBM9kw8" to "/home2/divine/Symfony/src/App/Bundle/Resources/public/files/book.pdf"
(move_uploaded_file() expects parameter 2 to be valid path, object given)"
我正在使用PHP 5.3。
更新:
这是执行move_uploaded_file()的代码剪切:
// Class: Symfony\Component\HttpFoundation\File\UploadedFile
$target = $this->getTargetFile($directory, $name);
if (!@move_uploaded_file($this->getPathname(), $target)) {
// etc...
$ target“变量在这里创建:
protected function getTargetFile($directory, $name = null) {
// Some error handling here...
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
return new File($target, false);
}
$ target变量就是File类。 它有一个__toString()方法,继承自SplFileInfo:
/**
* Returns the path to the file as a string
* @link http://php.net/manual/en/splfileinfo.tostring.php
* @return string the path to the file.
* @since 5.1.2
*/
public function __toString () {}
但不知何故__toString方法无法正常工作。