laravel上传图片
1.html页面
<form action="{
{url('file')}}" method="post" enctype="multipart/form-data">
<table border="1" align="center">
<tr>
<td>昵称</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>选择图片</td>
<td><input type="file" name="photo"/></td>
</tr>
<tr>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<td><input type="submit" value="提交"/></td>
<td></td>
</tr>
</table>
</form>
2.php页面
<?php
$file = Request::file('photo');
$name = Request::input('name');
$allowed_extensions = ["png", "jpg", "gif"];
if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
return ['error' => 'You may only upload png, jpg or gif.'];
}
$destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
$extension = $file->getClientOriginalExtension();
$fileName = str_random(10).'.'.$extension;
$file->move($destinationPath, $fileName);
$filePath = asset($destinationPath.$fileName);
$info=DB::insert('insert into photo(pname,photo) VALUES (?,?)',[$name,$filePath]);
if($info){
return Redirect('/show');
}else{
echo "no";
}
还没有评论,来说两句吧...