[英]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. I've tried several ways to include a PDF file using PHP on Linux, it works normally on Windows, but not on Linux.
I have several directories with accents and I need to include PDF files that are inside the directories.我有几个带有重音符号的目录,我需要包含目录中的 PDF 文件。
I pass the name of the PDF files and include the PDF.我传递了 PDF 文件的名称并包含 PDF。
My problem is with the encoding and accentuation of the folders.我的问题是文件夹的编码和强调。 The files doesn't have accents, only the folders.
文件没有重音符号,只有文件夹。
Examples of folders / files:文件夹/文件示例:
files/ño/hash1.pdf文件/ño/hash1.pdf
files/nó/hash2.pdf文件/nó/hash2.pdf
files/ção/hash.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);
I don't think the problem is anything to do with the folder names.我认为问题与文件夹名称无关。 I think the problem is that your recursive function is not actually returning the value when it finds the file.
我认为问题在于您的递归 function 在找到文件时实际上并未返回该值。
When you call getFileInContents($path, $filename);
当您调用
getFileInContents($path, $filename);
you then need to return the value, if it's not null, to break the loop.然后你需要返回值,如果它不是 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;
}
My answer adds on and expounds on the one offered up by @James, because you have additional issues:我的回答补充并阐述了@James 提供的答案,因为您还有其他问题:
readfile()
instead of include
.readfile()
而不是include
。$local
since you will be concatenating a '/' to the passed $dir argument in function getFileInContents
.$local
声明中包含最后的“/”,因为您将在 function getFileInContents
“/”连接到传递的$dir参数。getFileInContents
is intended to recursively search subdirectories, but it is not doing this correctly;getFileInContents
旨在递归搜索子目录,但它没有正确执行此操作; it only is searching the first subdirectory it finds and if the sought file is not present in that subdirectory it returns with a "not found" condition and never searches any other subdirectories that might be present in the directory.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.