【发布时间】:2015-11-27 17:23:37
【问题描述】:
我编写了以下基本文件上传代码,在本地主机上完美运行。在实时版本中,Input::hasFile('profile_picture') 返回 false,因此 文件未移动到所需目录,而 $filename = Input::file('profile_picture')->getClientOriginalName(); 工作正常并接收文件名。
在移动文件之前删除 if hasFile 检查,移动函数抛出异常说,
Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException
File could not be uploaded: missing temporary directory.
我已确认:
- 大文件不是问题,我尝试上传 1.4kb 的文件,我的
index.php中有ini_set('memory_limit', '128M');。 - html表单没有缺少
'files' => true、csrf token等常见问题。
upload.blade.php 包含:
{{ Form::open(['url' => 'api/users', 'files' => true]) }}
File: {{ Form::file('profile_picture') }}
{{ Form::hidden('email', 'asimawan.62@gmail.com') }}
{{ Form::submit('send') }}
{{ Form::close() }}
生成的表单如下所示:
<form method="POST" action="http://localhost:8000/api/users" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="96OKRdiwnHlku0uMLYYpHPjAIKAiqoKRp372fUWc">
File: <input name="profile_picture" type="file">
<input name="email" type="hidden" value="asimawan.62@gmail.com">
<input type="submit" value="send">
</form>
routes.php 包含:
Route::get('upload', function() {
return View::make('upload');
});
Route::resource('api/users', 'UsersController');
UsersController::store()
public function store()
{
if ($this->user->isValid($data = Input::all())) {
$data = Input::all();
$filename = Input::file('profile_picture')->getClientOriginalName();
$data['profile_picture'] = $filename;
if (Input::hasFile('profile_picture')) {
Input::file('profile_picture')->move(public_path('pictures/profile'), $filename);
} else {
return "Input::hasFile('profile_picture') has returned false. Size = "
. Input::file('profile_picture')->getClientSize();
}
$this->user->fill($data);
$this->user->save();
return Response::json($this->user);
}
return Response::json($this->user->errors);
}
【问题讨论】:
标签: php file-upload laravel-4 cpanel web-hosting