从站点目录的 url 下载文件
Download files from url of site directory
https://example.com/directory 中有两个 zip 文件和两个图像。 (由于 index.php 文件在此目录中可用,我们不知道这些文件的文件名)。
有什么方法可以使用PHP或Linux命令找到上述四个文件的名称吗?
您可以使用 PHPs scandir
方法。
试试这个:
<?php
$files = scandir('./');
print_r($files);
这应该打印当前目录中的所有文件。更多信息 here
您必须牢记:如果您的网络服务器不允许扫描目录,那么您将无法获取文件名。但是如果该目录在网络服务器中共享,您可以使用 wget
命令获取文件列表。例如:
wget -nv -r -np '*.*' https://example.com/directory/
<?php
$files=[];
$folder="/directory/directory/directory/";
if (is_dir($folder)){ // check whether exists or not
$allfiles = glob($folder.'/*.*'); // Read all filenames of $folder directory
foreach($allfiles as $imgFile) {
$files[]=$imgFile; // Putting the names of $folder directory to array one by one
}
}
echo "<pre>";
print_r($files); // printing the file name array to list aal names
echo "</pre>";
我认为您根本不了解 HTTP 协议。根据 HTTP 协议,它不知道任何 directory/sub-directory。你不能在 HTTP 协议中做这样的事情。
您想下载 http://example.com/directory 一些您不知道名称的文件。为此,您不能这样使用 PHP 。您必须依赖外部库,例如 wget 等或 FTP 协议。
<?php
$dir = 'directory';
$filesArr = scandir($dir);
$files = array();
for($i=2; $i<count($filesArr);++$i){
if($filesArr[$i]=="index.php"){ continue; }
$files[] = $filesArr[$i];
}
$zipname = 'file1.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
$zip->addFile($dir.'/'.$file, $dir.'/'.$file);
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
$zip->close();
exit;
?>
试试这个我在我的项目中使用下面的示例并且它对我来说工作正常,
<?php
$directory='directory';
$handler= opendir("$directory"."/");
$i=0;
while (false !== ($file = readdir($handler))) {
if ($file != '.' && $file !='..'){
echo $file.'<br>';
}
}
?>
假设您的网络服务器 example.com
上有一个目录,例如:/multimedia/
,您想要检索其内容,例如 mp3、jpg、png 等- 但 不是列表索引 也不是 tmp 索引文件,例如:index.html•C=D;O=D
:
wget -r -R "*.html*" -np -nd -l 1 example.com/multimedia/
其中:
-r # Recursive
-R "*.html*" # Reject tmp and listing html files
-np # Don't ascend to the parent directory
-nd # Don't create parent directories
-l 1 # Only one level deep (multimedia/ folder only)
要了解更多信息:运行 man wget
或 wget --help
或访问 gnu.org WGET
https://example.com/directory 中有两个 zip 文件和两个图像。 (由于 index.php 文件在此目录中可用,我们不知道这些文件的文件名)。
有什么方法可以使用PHP或Linux命令找到上述四个文件的名称吗?
您可以使用 PHPs scandir
方法。
试试这个:
<?php
$files = scandir('./');
print_r($files);
这应该打印当前目录中的所有文件。更多信息 here
您必须牢记:如果您的网络服务器不允许扫描目录,那么您将无法获取文件名。但是如果该目录在网络服务器中共享,您可以使用 wget
命令获取文件列表。例如:
wget -nv -r -np '*.*' https://example.com/directory/
<?php
$files=[];
$folder="/directory/directory/directory/";
if (is_dir($folder)){ // check whether exists or not
$allfiles = glob($folder.'/*.*'); // Read all filenames of $folder directory
foreach($allfiles as $imgFile) {
$files[]=$imgFile; // Putting the names of $folder directory to array one by one
}
}
echo "<pre>";
print_r($files); // printing the file name array to list aal names
echo "</pre>";
我认为您根本不了解 HTTP 协议。根据 HTTP 协议,它不知道任何 directory/sub-directory。你不能在 HTTP 协议中做这样的事情。
您想下载 http://example.com/directory 一些您不知道名称的文件。为此,您不能这样使用 PHP 。您必须依赖外部库,例如 wget 等或 FTP 协议。
<?php
$dir = 'directory';
$filesArr = scandir($dir);
$files = array();
for($i=2; $i<count($filesArr);++$i){
if($filesArr[$i]=="index.php"){ continue; }
$files[] = $filesArr[$i];
}
$zipname = 'file1.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
$zip->addFile($dir.'/'.$file, $dir.'/'.$file);
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
$zip->close();
exit;
?>
试试这个我在我的项目中使用下面的示例并且它对我来说工作正常,
<?php
$directory='directory';
$handler= opendir("$directory"."/");
$i=0;
while (false !== ($file = readdir($handler))) {
if ($file != '.' && $file !='..'){
echo $file.'<br>';
}
}
?>
假设您的网络服务器 example.com
上有一个目录,例如:/multimedia/
,您想要检索其内容,例如 mp3、jpg、png 等- 但 不是列表索引 也不是 tmp 索引文件,例如:index.html•C=D;O=D
:
wget -r -R "*.html*" -np -nd -l 1 example.com/multimedia/
其中:
-r # Recursive
-R "*.html*" # Reject tmp and listing html files
-np # Don't ascend to the parent directory
-nd # Don't create parent directories
-l 1 # Only one level deep (multimedia/ folder only)
要了解更多信息:运行 man wget
或 wget --help
或访问 gnu.org WGET