Why does php file deletion fail?

王林
Release: 2023-03-03 19:04:01
Original
2972 people have browsed it

The reason why php failed to delete the file: The file was still occupied by other threads or processes when it was deleted. Solution: First release the file object manually to avoid deletion failure due to being occupied by other threads or processes; then use the unlink() function to delete the file.

Why does php file deletion fail?

#Cause analysis:

The file is still occupied by other processes. So we'd better release the file object to avoid deletion failure due to being occupied by other threads or processes.

(Recommended tutorial: php graphic tutorial)

unlink() function deletes files.

If successful, this function returns TRUE. On failure, returns FALSE.

(Video tutorial recommendation: php video tutorial)

Code implementation:

public function upload()
{
    //获取上传文件
    $file = $this->request->file('file');
	
    if ($file) {
        // 移动文件到 uploads 目录下
        $info = $file->rule('date')->move(ROOT_PATH . 'public' . DS . 'uploads');
		
        if ($info) {//文件上传到服务器成功->接下来上传到OSS
            $filePath = ROOT_PATH . 'public' . DS . 'uploads/' . $info->getSaveName();
        	
            $oss = new Oss();
            $result = $oss->putObject($filePath); 
                  
            if ($result) { //上传到OSS成功                              
                unset($info);//解除图片的进程占用
                @unlink($filePath);
                return success(['avatar' => $result['fileSrc']], '头像更新成功');
            }
        }
    }
}
Copy after login

The above is the detailed content of Why does php file deletion fail?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template