我使用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';' –