什么';在目录| PHP中查找文件的最佳方法是什么
我需要使用php读取目录中的所有文件。所以我需要这样的回报什么';在目录| PHP中查找文件的最佳方法是什么,php,arrays,directory,Php,Arrays,Directory,我需要使用php读取目录中的所有文件。所以我需要这样的回报 array( 'content_type' => 'application/zip', 'suggested_name' => 'this_is_example.zip', 'protected_path' => 'download/this_is_example.zip' ), 我的文件如下所示: $download_list = array();
array(
'content_type' => 'application/zip',
'suggested_name' => 'this_is_example.zip',
'protected_path' => 'download/this_is_example.zip'
),
我的文件如下所示:
$download_list = array();
if(is_array($PROTECTED_DOWNLOADS)) {
foreach ($PROTECTED_DOWNLOADS as $key => $download) {
// Create a new key
$new = uniqid('key',TRUE);
// get download link and file size
$download_link = "http://" . $_SERVER['HTTP_HOST'] . DOWNLOAD_PATH . "?key=" . $new . "&i=" . $key;
$filesize = human_filesize(filesize($download['protected_path']), 2);
$filename = ($download['suggested_name']);
// Add to the download list
$download_list[] = array(
'download_link' => $download_link,
'filename' => $filename,
'filesize' => $filesize
);
/*
* Create a protected directory to store keys in
*/
if(!is_dir('history')) {
mkdir('keys');
$file = fopen('history/.htaccess','w');
fwrite($file,"Generator");
fclose($file);
}
/*
* Write the key key to the keys list
*/
$file = fopen('history/keys','a');
fwrite($file,"{$new}\n");
fclose($file);
}
}
$PROTECTED_DOWNLOADS = array(
array(
'content_type' => 'application/png',
'suggested_name' => 'example.png',
'protected_path' => 'download/example.png'
),
array(
'content_type' => 'application/txt',
'suggested_name' => 'this_is_example.txt',
'protected_path' => 'download/this_is_example.txt'
),
array(
'content_type' => 'application/zip',
'suggested_name' => 'this_is_example.zip',
'protected_path' => 'download/this_is_example.zip'
),
array(
'content_type' => 'application/rar',
'suggested_name' => 'this_is_example.rar',
'protected_path' => 'download/this_is_example.rar'
),
array(
'content_type' => 'application/docx',
'suggested_name' => 'this_is_example.docx',
'protected_path' => 'download/this_is_example.docx'
)
);
现在一切正常,但在运行之前我必须自己输入这些文件。如何发布目录中的所有文件?
现在我有这样一个:
$download_list = array();
if(is_array($PROTECTED_DOWNLOADS)) {
foreach ($PROTECTED_DOWNLOADS as $key => $download) {
// Create a new key
$new = uniqid('key',TRUE);
// get download link and file size
$download_link = "http://" . $_SERVER['HTTP_HOST'] . DOWNLOAD_PATH . "?key=" . $new . "&i=" . $key;
$filesize = human_filesize(filesize($download['protected_path']), 2);
$filename = ($download['suggested_name']);
// Add to the download list
$download_list[] = array(
'download_link' => $download_link,
'filename' => $filename,
'filesize' => $filesize
);
/*
* Create a protected directory to store keys in
*/
if(!is_dir('history')) {
mkdir('keys');
$file = fopen('history/.htaccess','w');
fwrite($file,"Generator");
fclose($file);
}
/*
* Write the key key to the keys list
*/
$file = fopen('history/keys','a');
fwrite($file,"{$new}\n");
fclose($file);
}
}
$PROTECTED_DOWNLOADS = array(
array(
'content_type' => 'application/png',
'suggested_name' => 'example.png',
'protected_path' => 'download/example.png'
),
array(
'content_type' => 'application/txt',
'suggested_name' => 'this_is_example.txt',
'protected_path' => 'download/this_is_example.txt'
),
array(
'content_type' => 'application/zip',
'suggested_name' => 'this_is_example.zip',
'protected_path' => 'download/this_is_example.zip'
),
array(
'content_type' => 'application/rar',
'suggested_name' => 'this_is_example.rar',
'protected_path' => 'download/this_is_example.rar'
),
array(
'content_type' => 'application/docx',
'suggested_name' => 'this_is_example.docx',
'protected_path' => 'download/this_is_example.docx'
)
);
我尝试了glob()、scandir()和readdir(),但没有得到想要的结果。谢谢 未测试,但请尝试这样做
<?php
$directory = 'download';
$PROTECTED_DOWNLOADS = [];
foreach (glob($directory."/*") as $filename) {
$PROTECTED_DOWNLOADS[]['content_type'] = mime_content_type($filename);
$PROTECTED_DOWNLOADS[]['suggested_name'] = $filename;
$PROTECTED_DOWNLOADS[]['protected_path'] = "$directory/$filename";
}
print '<pre>';
print_r($PROTECTED_DOWNLOADS);
print '</pre>';
?>
未测试,但请尝试这样做
<?php
$directory = 'download';
$PROTECTED_DOWNLOADS = [];
foreach (glob($directory."/*") as $filename) {
$PROTECTED_DOWNLOADS[]['content_type'] = mime_content_type($filename);
$PROTECTED_DOWNLOADS[]['suggested_name'] = $filename;
$PROTECTED_DOWNLOADS[]['protected_path'] = "$directory/$filename";
}
print '<pre>';
print_r($PROTECTED_DOWNLOADS);
print '</pre>';
?>
你已经走了多远?添加到目前为止的代码,人们可能会调整它以提供帮助。您有多少进展?添加代码到目前为止,人们可以调整代码以提供帮助。如果只需要具有特定文件结尾的文件,可以使用glob(“下载/*.{txt,png,zip,doc,docx,tar}”,glob_BRACE)
。GLOB_支架
很重要。当然,也可以从数组动态构建文件结尾表达式。如果只需要具有特定文件结尾的文件,可以使用glob(“下载/.{txt,png,zip,doc,docx,tar}”,glob_BRACE)
。GLOB_支架
很重要。当然,也可以从数组动态构建文件结尾表达式。