Program design ideas on how to avoid junk files when uploading files

Get into the habit of writing together! This is the 16th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the event details .

Let's talk about the general scenario first, which is a page where an article is added. The cover image of the article needs to be uploaded. Uploading a file is a separate function. After the file is uploaded successfully, the url address is returned. echo in the page. Then fill in the other information, and finally click Submit Database.

 

However, the traditional way of uploading files will generate some junk files. For example, if you upload files multiple times on the page, the link stored in my database will only be the link to the last picture you uploaded. Then the pictures you posted before are actually useless, and I don't collect pictures, so it's useless to ask for those things.

 

So how to avoid this problem?

 

I just went into the horns before, it's actually very simple:

When uploading a file, now the image file is uploaded to the cache directory, and the link is returned and echoed.

When clicking submit to write to the database, move the picture corresponding to the picture link written in the database to the official picture directory.

Then write a scheduled task to periodically clear the files in the cache directory.

This will avoid the generation of junk files~

 

Another thought before was:

Store the uploaded file link in a database table, write a timed task method to view the files uploaded the previous day, those files are not stored in the database table, delete them. Then clear the data in the data table

This method is actually not bad, but you need to add a database table to temporarily store the file information that has been stored in the database.

Finally, share the public upload file method I encapsulated in the laravel framework:

       /**
     * @name: 公共上传文件方法
     * @author: camellia
     * @date: 2021-02-18 
     * @param:	$file	object  文件流
     * @param:	$filepath	string  文件目录
     * @param:	$file_type	string  文件类型字符串(.png,.bmp,.mp4,.mp3)
     * @param:	$file_size	number  文件大小(字节数)
     */
    public function uploadPublic($file, $filepath, $file_type, $file_size)
    {
        try{
            // 文件存放公共目录
            $path = "./uploads/";
            if (!$file) 
            {
                $result['code'] = -1;
                $result['msg'] = '参数错误,缺少文件流信息!';
                return $result; //没有图片
            }
            //获取图片后缀(如abc.png,获取到的为png)
            $file_ext = $file->extension();

            // 允许上传图片类型数组
            $file_type_array = explode(',', $file_type);
            // 判断格式
            if (!in_array(strtolower('.' . $file_ext), $file_type_array)) 
            {
                $result['code'] = -2;
                $result['msg'] = '文件格式错误!';
                return $result; //格式不符
            }
            // 文件大小(这里取得的是字节数)
            $fileSize = $file->getSize();
            if ($fileSize >= $file_size) 
            {
                $result['code'] = -10;
                $result['msg'] = '文件大小超出限制!';
                return $result;
            }
            //文件重命名
            $new_name = date('Y-m-d-H-i-s', time()) . '_' . rand(1000000, 9999999) . '.' . $file->getClientOriginalExtension();
            // 文件保存目录
            $savepath = $path . $filepath;
            // 如果目录不存在,创建目录
            if (!is_dir($savepath)) 
            {
                $mkdir = "mkdir -p " . $savepath;
                exec($mkdir, $output, $status);
            }
            // 移动文件
            if ($file->move($savepath, $new_name)) 
            {
                $result['code'] = 1;
                $result['name'] = $new_name;
                $result['url'] =  DOMAIN.$filepath . $new_name;
                $result['path'] =  $path . $filepath . $new_name;
                $result['type'] =  $file->getClientOriginalExtension();
                $result['msg'] = '文件上传成功!';
                return $result;
            } 
            else 
            {
                $result['code'] = -3;
                $result['msg'] = '文件上传失败!';
                return $result; //移动失败
            }
        }
        catch(Exception $e)
        {
            $result['code'] = -10;
            $result['msg'] = '上传文件发生错误!';
            return $result; 
        }
        
    }
复制代码

There are actually these two methods that I think of. If you have good suggestions, please enter your comments below.

Welcome to the personal blog guanchao.site

Guess you like

Origin juejin.im/post/7087025245961322509