0

我正在尝试将以前上传到 tmp 目录的图像文件移动到永久位置。我以本教程为起点:

maxoffsky.com/code-blog/uploading-files-in-laravel-4/

我已经修改了代码,因为我之前上传了文件并稍后通过表单提供的 url 访问它们。下面的代码显示了我在做什么:

$form_element = "image_1"; // hidden form element containing the tmp location
$tmp_path = $form_data[$form_element]; // gets the tmp url from hidden element
$file = fopen($tmp_path, 'r'); // opens the file

$destinationPath = 'images/adverts/'.$advert->id; // specifies a new folder
$filename = $file->getClientOriginalName(); // retrieves the name of the file
$upload_success = $file->move($destinationPath, $filename); // moves the file to its new location

我遇到的问题是,在教程中,他们使用代码:

$file = Input::file('file'); // Laravel syntax for $_POST['file']

这会从 html 表单本身检索文件。从那里开始功能$file->getClientOriginalName()$file->move()正常工作。

但是,在我的表单中,由于我的表单没有提供实际文件,只是一个链接,我试图访问该文件并执行相同的操作,但是我收到此错误:

Call to a member function getClientOriginalName() on a non-object

我不认为fopen()返回的类型与$_POST['file']它不工作的类型相同。

我怎样才能使我的代码工作?

非常感谢

4

1 回答 1

0

我有同样的问题,只需在数组中的 Form::open 中添加 enctype:

enctype='multipart/form-data'

你的问题就解决了!我已经忘记了它,然后当我看到它时,我开始更加努力地敲打它。

PS。你有一点误解。Input::file('file') 它不是 $_POST['file'] 的语法,而是 $_FILES['file'] 的语法

于 2014-06-19T04:34:26.737 回答