我想用PHP搜索一个目录中的多个文件,并开始通过HTTP下载它们

我想用PHP搜索一个目录中的多个文件,并开始通过HTTP下载它们,php,file,directory,transfer,Php,File,Directory,Transfer,首先,我是一个PHP高手 我有一个Apache服务器,它托管我的文件。我有一个设备,它只能指向一个PHP文件。我需要它做的是让我的PHP文件以我要下载的文件的名称读取,并将其指向存储的目录。目前,我有它指向一个文件,但我需要它能够指向多个。这在PHP中可能吗 以下是我目前掌握的情况: <?php $file_name = 'file.img'; $size = filesize($file_name); $file_url = 'http://192.168.0.5/' . $file_

首先,我是一个PHP高手

我有一个Apache服务器,它托管我的文件。我有一个设备,它只能指向一个PHP文件。我需要它做的是让我的PHP文件以我要下载的文件的名称读取,并将其指向存储的目录。目前,我有它指向一个文件,但我需要它能够指向多个。这在PHP中可能吗

以下是我目前掌握的情况:

<?php 
$file_name = 'file.img';
$size = filesize($file_name);
$file_url = 'http://192.168.0.5/' . $file_name;
header("Content-length: $size");
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);?>
下载目录是.php文件。我愿意听取关于如何做到这一点的其他建议

编辑2: 以下是准确的示例URL:

 myserver.com/cgi-bin/va/cmd?hdl+fullconfig.ini+altconfig

hdl是一个预定义的函数,指向下载目录,以便从服务器下载文件,因此您的意思的布局并不完全相同。

我很难理解您到底想做什么,但我猜您希望用户能够下载多个文件。如果这是正确的,那么有一种方法可以实现这一点:

您可以让PHP使用创建ZIP存档。要使其工作,必须在php.ini中加载扩展名php_zip.dll

$ZIP = new ZipArchive();

// Use the current time as the filename to prevent two users to use the same file
$ZIPName = microtime().".zip";

// Create a new ZIP file
$ZIP->open($ZIPName, ZIPARCHIVE::CREATE);

// Loop through all files - $Files needs to be an array with the names of the files you want to add, including the paths
// basename() will prevent the creation of folders inside the ZIP
foreach ($Files as $File) {
    $ZIP->addFile($File, basename($File));
}

// Close the archive
$ZIP->close();

// Send the archive to the browser
readfile($ZIPName);

我希望这就是您想要的。

谢谢您的回答,但这并不是我真正想要做的。例如,我想下载sample_file1和sample_file2,在我的命令/地址栏中指定文件名。我希望我的PHP文件能够从目录中发送文件,并带有适当的标题等。我上面发布的代码只是“file.img”的强制下载,但我希望它能够处理其他名称(以命令/地址栏作为输入)。希望这更有意义!您是否只想在URL中指定要下载的文件,如myscript.php?file=sample_file1,并向您发送sample_file1?然后可以使用$\u GET[“file”]读取URL参数。这就是你想要的吗?是的,听起来不错。我会好好玩玩,看看能不能把它弄到手。抱歉,如果我的问题看起来很愚蠢,我对PHP非常陌生!谢谢你的帮助!对不起,恐怕我帮不了你。我根本不明白你在干什么。我希望其他人甚至你能想出一个解决办法。
$ZIP = new ZipArchive();

// Use the current time as the filename to prevent two users to use the same file
$ZIPName = microtime().".zip";

// Create a new ZIP file
$ZIP->open($ZIPName, ZIPARCHIVE::CREATE);

// Loop through all files - $Files needs to be an array with the names of the files you want to add, including the paths
// basename() will prevent the creation of folders inside the ZIP
foreach ($Files as $File) {
    $ZIP->addFile($File, basename($File));
}

// Close the archive
$ZIP->close();

// Send the archive to the browser
readfile($ZIPName);