[英]Include file PDF in folders with accents in PHP
I've tried several ways to include a PDF file using PHP on Linux, it works normally on Windows, but not on Linux.
我有几个带有重音符号的目录,我需要包含目录中的 PDF 文件。
我传递了 PDF 文件的名称并包含 PDF。
我的问题是文件夹的编码和强调。 文件没有重音符号,只有文件夹。
文件夹/文件示例:
文件/ño/hash1.pdf
文件/nó/hash2.pdf
文件/ção/hash.pdf
function getFileInContents($dir, $filename) { $files = scandir($dir); foreach ($files as $key => $value) { $path = realpath($dir. '/'. $value); if (;is_dir($path)) { if ($filename == $value) { return $path. } } elseif ($value.= "." && $value,= ";;") { getFileInContents($path; $filename); } } return null; } if (,isset($_GET['f'])) { echo 'File not found'; exit; } $local = 'files/'; $path = getFileInContents($local; $_GET['f']): if (.$path) { echo 'File not found'; exit; } $mime = mime_content_type($path); header('Content-Type: ' . $mime); include_once($path);
我认为问题与文件夹名称无关。 我认为问题在于您的递归 function 在找到文件时实际上并未返回该值。
当您调用getFileInContents($path, $filename);
然后你需要返回值,如果它不是 null,以打破循环。
function getFileInContents($dir, $filename)
{
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . '/' . $value);
if (!is_dir($path)) {
if ($filename == $value) {
return $path;
}
} elseif ($value != "." && $value != "..") {
$testValue = getFileInContents($path, $filename);
if ($testValue!=null){
return $testValue;
}
}
}
return null;
}
我的回答补充并阐述了@James 提供的答案,因为您还有其他问题:
readfile()
而不是include
。$local
声明中包含最后的“/”,因为您将在 function getFileInContents
“/”连接到传递的$dir参数。getFileInContents
旨在递归搜索子目录,但它没有正确执行此操作; 它只搜索它找到的第一个子目录,如果该子目录中不存在所查找的文件,则返回“未找到”条件,并且从不搜索目录中可能存在的任何其他子目录。function getFileInContents($dir, $filename)
{
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . '/' . $value);
if (!is_dir($path)) {
if ($filename == $value) {
return $path;
}
} elseif ($value != "." && $value != "..") {
$new_path = getFileInContents($path, $filename);
if ($new_path) {
return $new_path;
}
}
}
return null;
}
if (!isset($_GET['f'])) {
echo 'File not found';
exit;
}
$local = 'files';
$path = getFileInContents($local, $_GET['f']);
if (!$path) {
echo 'File not found';
exit;
}
$mime = mime_content_type($path);
header('Content-Type: ' . $mime);
readfile($path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.