如何使用php下载每个文件
我正在使用php函数获取文件列表。它将目录中的所有文件显示为php页面上的表 如何通过单击每个文件名使这些文件可下载 实际文件的扩展名为.tar.gz 谢谢大家! 我的功能:如何使用php下载每个文件,php,Php,我正在使用php函数获取文件列表。它将目录中的所有文件显示为php页面上的表 如何通过单击每个文件名使这些文件可下载 实际文件的扩展名为.tar.gz 谢谢大家! 我的功能: function getFileList($dir) { // array to hold return value $retval = array(); // add trailing slash if missing if(substr($dir, -1) != "/") $di
function getFileList($dir)
{
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$entry",
"type" => mime_content_type("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();
return $retval;
$dirlist = getFileList("backupfiles");
}
<?PHP
// output file list in HTML TABLE format
echo "<table id=\"ct\" border=\"1\">\n";
echo "<thead>\n";
echo "<tr id = \"ct_sort\"><th>Name</th><th>Type</th><th>Size</th>
<th>Added On</th></tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
foreach($dirlist as $file) {
echo "<tr>\n";
echo "<td>{$file['name']}</td>\n";
echo "<td>{$file['type']}</td>\n";
echo "<td>{$file['size']}</td>\n";
echo "<td>",date('r', $file['lastmod']),"</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n\n";
?>
我的代码:
function getFileList($dir)
{
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$entry",
"type" => mime_content_type("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();
return $retval;
$dirlist = getFileList("backupfiles");
}
<?PHP
// output file list in HTML TABLE format
echo "<table id=\"ct\" border=\"1\">\n";
echo "<thead>\n";
echo "<tr id = \"ct_sort\"><th>Name</th><th>Type</th><th>Size</th>
<th>Added On</th></tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
foreach($dirlist as $file) {
echo "<tr>\n";
echo "<td>{$file['name']}</td>\n";
echo "<td>{$file['type']}</td>\n";
echo "<td>{$file['size']}</td>\n";
echo "<td>",date('r', $file['lastmod']),"</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n\n";
?>
只需添加一个锚点\n“
,其中path\u to\u file
是保存数据库中文件路径的变量。请注意三个级别的引用。您可以使用以下方法:
echo "<td><a href='http://www.yoursite.com/folder/{$file['name']}'>{$file['name']}</a></td>\n";
echo“\n”;
而不是:
echo "<td>{$file['name']}</td>\n";
echo“{$file['name']}\n”;
注意:这是我最初评论的答案
在foreach中,您只需将echo“{$file['name']}\n”
更改为:
echo "<td><a href='{$file['url']}'>{$file['name']}</a></td>\n";
echo“\n”;
假设您的url位于$file['url']
中,您只是想回显到现有文件的超链接吗?是的……用户只需单击文件名,它就会被下载。在这种情况下,在foreach中,您只需执行类似回显“\n”
的操作,假设您的url位于$file['url']
这不起作用…我的文件路径不在数据库中…我在变量$dirlist=getFileList(“backupfiles”)中提到了路径
。添加代码后,当然只会显示一个带有超链接的文件。您的解决方案与David建议的类似,而且也是正确的。但是我会将David的答案标记为与他以前在评论中回答的一样正确。谢谢您的帮助。