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.
#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']], '头像更新成功'); } } } }
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!