Home  >  Q&A  >  body text

php - laravel File::delete fails to delete files

After the file was uploaded successfully, I downloaded it to the local computer, read the content and performed a series of database operations, and then deleted the file, but the deletion failed. And if you delete it immediately next time you come in, the deletion will be successful.

     // \File::delete(storage_path(self::SAVE_FILE_NAME)); // 如果写在这里下次再进来删除文件则会成功
    // 下载文件到本地
    $success = $this->downloadUserFile($data['user_info']);
    if ($success) {
        $file = new \SplFileObject(storage_path(self::SAVE_FILE_NAME));
        $file->setFlags(\SplFileObject::READ_CSV);
        foreach($file as $index => $row) {
            list($mobile, $userName) = $row;
            // 数据库操作
        }
        // 处理完成后删除(无法成功)
         \File::delete(storage_path(self::SAVE_FILE_NAME));
    }
    
曾经蜡笔没有小新曾经蜡笔没有小新2706 days ago819

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-24 11:35:41

    I know the reason. After the file is processed, it needs to be closed before it can be deleted successfully! So set $file=null before deletion

    reply
    0
  • Cancelreply