【发布时间】:2016-01-19 11:51:04
【问题描述】:
我目前正在努力让这个功能在我的测试服务器上运行。
这会在一个非常大的文件目录(嵌套文件夹中大约 4000 个文件)中查找并创建一个文件树。
我已经在我的本地机器上测试了这个脚本和类似的脚本,它工作正常,但我发现在实时服务器上,如果我取消注释注释掉的行,它会导致页面无限期挂起。没有错误输出,我似乎找不到任何日志。我已经用尽了服务器上的 php 内存,并增加了最大输入变量和处理时间等。
任何帮助将不胜感激。
function read_dir_content($parent_dir, $depth = 0, $rememberFiles = [], $tree = [], $first = 'first', $rand = 0){
$str_result = "<ul class=\"files {$first}\" id=\"collapse{$rand}\">";
if ($handle = opendir($parent_dir))
{
$array = [];
while (false !== ($file = readdir($handle)))
{
if(in_array($file, array('.', '..'))) continue; // ignore . and ..
if(substr($file, 0, 1) == '.') continue; // ignore invisible files
$array[] = $parent_dir . '/' . $file;
}
closedir($handle);
if (count($array)) {
// order the array here...
foreach($array as $path) {
$relPath = str_replace(Dataroom::basePath() . '/', '', $path);
$name = basename($path);
$checked = false;
if( is_dir($path) ){ // folders
$encoded = base64_encode($relPath . '/');
$newrand = mt_rand($rand+1, $rand+1000000); // set random number
$state = in_array(md5($encoded), $tree) ? 'true' : 'false';
$state2 = $state == 'true' ? 'collapse in' : 'collapse';
$folderIcon = $state == 'true' ? 'glyphicon-folder-open' : 'glyphicon-folder-close';
$checked = old('folders.'.$encoded) || in_array($encoded, $rememberFiles) ? 'checked="checked"' : '';
$str_result .= "<li class=\"folder text-primary\">";
$str_result .= "<input type=\"checkbox\" {$checked} id=\"{$encoded}\" name=\"files[{$encoded}]\"> "; // folder checkboxes will be ignored without a name attribute
$str_result .= "<span class=\"glyphicon {$folderIcon}\" aria-hidden=\"true\"></span> ";
$str_result .= '<a class="collapse'.$newrand.'" onclick="rememberTreeState(\''.$encoded.'\');" aria-expanded="'.$state.'" aria-controls="collapse'.$newrand.'" data-toggle="collapse" href="#collapse'.$newrand.'">'.$name.'<span class="caret"></span></a>'; // link
//$str_result .= '<a href="http://test"></a>'; // THIS LINE CAUSES PAGE TO HANG!!!
$str_result .= read_dir_content($path, $depth++, $rememberFiles, $tree, $state2, $newrand);
$str_result .= "</li>";
} else { // files
$encoded = base64_encode($relPath);
if (!empty(old() && old('files'))) {
if (old('files.'.$encoded)) {
$checked = 'checked="checked"'; // remember input from last errored form submission
}
} else {
if (in_array($encoded, $rememberFiles)) {
$checked = 'checked="checked"'; // remember files from group db records
}
}
$str_result .= "<li class=\"file text-muted\"><input type=\"checkbox\" {$checked} name=\"files[{$encoded}]\"> <span class=\"glyphicon glyphicon-file\" aria-hidden=\"true\"></span> {$name}</li>";
}
}
unset($path);
unset($handle);
}
}
$str_result .= "</ul>";
return $str_result;
}
似乎它刚刚达到某种内存限制并且一切都停止了,但我的服务器知识并不是那么好,所以我真的被卡住了。我确定语法没问题,所以它一定是别的东西。
【问题讨论】:
-
本地PHP版本和live版本有区别吗?
-
是的,本地是 5.5.10,现场是 5.6.17
-
我注意到如果我将 base64_encode 换成 md5 它可以工作,但我需要对我的 url 进行可逆加密(不需要安全)。
-
所以看起来是 base64_encoding 导致了这个问题。无论是因为一些非法字符还是因为字符串长度或内存,我都不确定。对 url 的短 url 安全可逆加密有什么建议吗?不需要安全。
-
这似乎与我要输出的字符串的长度有关,所以我打开了另一个问题
标签: php laravel memory foreach