我正在try 查看不同网络文件夹中的图像.一切正常,但一旦有一个文件夹的名称中有"&;",我就无法查看其内容.URL在"&;"之后停止,因此它找不到有问题的文件夹.
我已经研究了其他类似于这个问题的问题,但都不起作用(许多问题也可以使用strplace()来处理单个URL,在我的例子中,当我有数百个文件夹时,这是不起作用的)
Running on Windows Server 2022 with IIS.
我的代码是:
<?php
$selectedDir = isset($_GET["dir"]) ? $_GET["dir"] : "";
//echo $selectedDir;
echo '<h1> '.$selectedDir.' </h1>';
$dirs = "\\\\server\images\\moreimages/$selectedDir";
$dir = new DirectoryIterator($dirs);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
$directorypath = $selectedDir."/".$fileinfo->getFilename();
echo "<a href='?dir=".$directorypath."'>".$fileinfo->getFilename()."</a><br><br>";
}
elseif (stripos($fileinfo, '.jpg') !== false || stripos($fileinfo, '.png') !== false) {
?>
<?php echo '<a target="_blank" href="'."\Intraimages\VD_images/$selectedDir/".$fileinfo.'"/>'; ?>
<?php echo '<img src="'."\Intraimages\VD_images/$selectedDir/".$fileinfo.'"/> </a>'; ?>
<?php
}
}
?>
(For clarification the different paths if it's any help: "\\server\images\moreimages/" is the UNC path. "\Intraimages\VD_images/" is the Virtual Directory path in IIS.)个
我会尽量提前提供更多的信息.
文件夹名称为:35144 MAN T&A;B T2-L62
加入时间:清华2007年01月25日下午3:33
Php致命错误:未捕获意外值异常:DirectoryIterator::__construct(\server\images\moreimages//35000-35999/35144 MAN T):系统在C:\inetpub\wwwroot\IntraImages\index.php:38中找不到特定文件(代码:2
已try var_ump $selectedDir
,其中显示:
字符串(28)"%2F35000-35999%2F35144+MAN+T"
我网络浏览器中的URL显示:.../?dir=/35000-35999/35144 MAN T&;B T2-L62
What I've tried个
urlencode
:$selectedDir
将php.ini中的某些设置更改为%26:
Arg_分离器.out="&;"
Arg_分离器.input=";&;"
我需要在URL中插入"%26"而不是"&;"吗?如果是这样的话,我不知道是怎么回事.
EDIT个
我try 过:
str_replace("&","%26",$selectedDir);
但URL和$selectedDir
不包含"&;",因为它正好停在"&;"之前,所以我猜没有什么可替换的...?
SOLVED, this is my new code thanks to the all the help:个
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
$directorypath = http_build_query(array($selectedDir."/".$fileinfo->getFilename()));
$url = parse_url($directorypath);
$newurl = str_replace('0=','',$url['path']);
echo "<a href='?dir=".$newurl."'>".$fileinfo->getFilename()."</a><br><br>";
}
NOTE:这可能不是最好的解决方案,因为我用的是str_replace()
,但它只是证明了它适用于http_build_query
对于感兴趣的人,新的URL现在是:
.../?dir=%2F35000-35999%2F35144+MAN+T%26B+T2-L62
个