所以我有一个名为/kv/的目录,我想要它,所以当您转到 blah.com/getkv.php 时,它会从该目录下载一个随机文件。如果可能的话,我希望它在下载文件后也将其删除。我们将不胜感激!
最佳答案
您可以按数字顺序存储文件,然后通过生成随机数随机选择一个文件。
$random = mt_rand(1,100); //The amount of files you have
下一步,一旦您选择了一个文件,您将设置下载 header
$file="/path/to/file/".$random.".png"; //file location
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
最后用
删除文件delete($file);
另一种选择随机文件的方法是索引一个文件夹,然后随机选择其中一个文件。
进行检查以确保文件存在也是明智的。
关于php - 从目录下载随机文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858860/