我已经编写了以下基本文件上传代码,其中在本地主机上完美工作。在实时版本中,Input::hasFile('profile_picture')
返回false,因此文件没有移动到所需的目录,而$filename = Input::file('profile_picture')->getClientOriginalName();
工作正常并接收文件名。
在移动文件之前删除if hasFile检查, move函数抛出异常说: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令牌。
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:存储()
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函数来知道临时文件
的路径是什么ini_get('upload_tmp_dir');
然后检查文件夹权限