Laravel file upload operation

1. File upload

It is very simple to upload files in laravel. There is no need to introduce a third-party class library. The author treats the upload as a simple http request.
You can use use Illuminate\Http\Request;the filemethods or dynamic attributes provided by the instance to access the uploaded file, and filereturn one of the Illuminate\Http\UploadedFile;classes. Example, this class inherits from the class that provides methods for interacting with files in the PHP standard library SplFileInfo:

$file = $request -> file(‘photo’);
$file = $request -> photo;

You can use the hasFilemethod to determine whether the file exists in the request:

if ($request -> hasFile(‘photo’)) {
//
}

Verify whether the file is uploaded successfully.
Use the isValidmethod to determine whether there is an error in the upload process:

if ($request -> file(‘photo’) -> isValid()) {
//
}

For more methods, please visit:

http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

Ideas:
1. First judge whether the file is normal and existing
2. Obtain relevant information (optional)
3. Save the file (in fact, move the file to a new directory)

1.1 Modify the table structure, add the avatar field

Example:
Insert picture description here
Effect:
Insert picture description here

1.2 Create a form and route to add data

Add data form code:
Insert picture description here

1.3 Save path of uploaded file

Create a save path for the uploaded file (must be under the public file to ensure that the browser can access it.)
Insert picture description here
Controller code:

public function auto_verify(Request $request) {
    
    
        // 1、接收并验证数据是否符合规则
            $this -> validate($request, [
                // 此处是一个键值数组  "验证对字段名" => "规则1|规则2|规则3..."
                'name' => 'required|min:2|max:20|unique:member', // 姓名是必须对,最短2个字符,最长20个字符,在member表里名字是唯一的
                'age' => 'required|integer|min:1|max:200', // 必须的,整型,此处min和max表示最小为1,最大为200
                                                            //,因为我们这边定义了整型,laravel会自动判断min和max为数字
                'email' => 'required|email', // 验证邮箱
            ]);
        // 2、处理文件上传
            // 判断文件是否上传正常与存在
            if ($request -> hasFile('avatar') && $request -> file('avatar') -> isValid()) {
    
    
                // 对文件对重新命名
                $name = sha1(time().rand(100000,999999)). '.' .$request -> file('avatar') -> extension();
                dump($name);
                // 文件移动操作
                $request -> file('avatar') -> move('./statics/upload', $name);
                // 存文件地址
                $path = '/statics/upload/'.$name;
            }
        // 3、通过验证写入数据库
        $data = $request -> except(['_token', 'avatar']);
        $data['avatar'] = isset($path) ? $path : '';
        $result = Member::insert($data);
        
        // 4、告知用户操作是否成功
        return $result ? 'OK' : 'Failed';

    }

Insert picture description here
Insert picture description here
effect:
Insert picture description here
Insert picture description here

On the way of learning php, if you think this article is helpful to you, then please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/114319162