PHP ::如何在读取后从服务器删除文件。 Unlink()在

之前执行。 第一次显示后,我正在尝试从服务器删除图片(.jpg)。 但是该文件在显示之前已被删除(unlink();)。我已经尝试过sleep()了,但这只会延迟加载,并且在显示之前删除了所有文件。     
已邀请:
您可以使用mod_rewrite将jpg请求重定向到脚本,该脚本将图像加载到内存中,删除文件,然后提供图像。 IMO,这是最简单,最简单的解决方案。下面的不安全示例... 示例.htaccess文件:
# Turn on URL rewriting
RewriteEngine On

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
index.php
<?php

$image_file = $_SERVER[\'PATH_INFO\'];

// clean data, strip current/parent directory to block transversal, etc...

if (file_exists(\'images/\' . $image_file))
{
    $image_data = file_get_contents(\'images/\' . $image_file);

    // determine image mimetype using phps mimetype functions

    unlink(\'images/\' . $image_file);

    header(\'Content-Type: image/jpeg\');

    echo $image_data;
}
    
将图像内容从文件复制到内存; 删除图像文件; 将原始内容从内存流式传输到输出。 恐怕我只能用模糊来做。     
这是因为所有PHP在呈现输出之前都已在堆栈中执行。您将需要设置一个功能来标记要在下一页加载时删除的文件。 或者,您可以设置一些AJAX以在文档加载后删除图片。     

要回复问题请先登录注册