laravel图片上传

html页面


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>表单title>
head>
<body>
<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="">
            <td><input type="submit" value="提交"/>td>
            <td>td>
        tr>
    table>
form>
body>
html>

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";
          }

完毕

你可能感兴趣的:(html,图片上传,laravel)