我使用Symfony 2.3来保存表单POST上传的文件。move_uploaded_file()期望参数2是有效的路径,给定的对象
这是我在控制器中使用的代码:
$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 ([email protected]_uploaded_file($this->getPathname(), $target)) {
// etc...
的$目标”变量在这里创建:
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);
}
的$目标变量是一个File类,它有一个继承自SplFileInfo的__toString()方法:
/**
* 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方法不起作用。
如您所见,其中一个参数是一个对象,而不是一个字符串或不能转换为字符串(即该对象没有__toString()方法)。顺便说一句,我确实希望它只是你的开发机器。 777对于现场环境来说是个坏主意...... –
是的,错误确实表明第二个参数不是字符串。但我为此感到困惑。这对我来说绝对是一个字符串。 – BigJ
'public/files /';'需要尾部斜线。因为它被读作'public/filesbook.pdf';'没有它,而不是'public/files/book.pdf';' –