我这个问题已经持续了几个小时,我似乎无法弄清楚它会是什么。
我目前有一个管理面板脚本,允许管理员在主网站上发布博客,我有一行利用str_replace();
foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
{
//replace in body
$Body = str_replace("[" . $index + 1 . "]", "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);
目前,在撰写博客文章时,我允许上传图片,当脚本处理每个图片时,它会用[1] [2](它的图像索引+ 1)替换标签,例如实际的img标签。
我让脚本输出解析后的Body并返回了
This is body content [<img src="/images/gallery/asdasdasdasd/windows.png">
正如您所看到的,图像在图像标记开始之前有一个打开的括号,原因很奇怪。
由于某种原因,我无法找到解决方案。这有没有发生在其他人身上?
谢谢, 雅各布
正如Dave评论说问题可能在代码的另一部分内,整个POST过程如下:
if(isset($_POST["create"]))
{
$Title = trim($_POST["title"]);
$Body = trim($_POST["body"]);
$seoTitle = seo($Title);
if($Title == "" || $Body == "")
{
$Template->assign_var("error", "Dude, fill in all of the fields.");
}
else
{
if(!empty($_FILES["images"]))
{
$allowed_types = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg", "image/x-png", "image/png");
$allowed_ext = array("jpg", "jpeg", "gif", "png");
$photos = array();
if(!is_dir(ROOT_DIR . "../images/gallery/" . $seoTitle))
mkdir(ROOT_DIR . "../images/gallery/" . $seoTitle);
foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
{
$filedata = explode(".", $_FILES["images"]["name"][$index]);
$ext = end($filedata);
if(in_array($ext, $allowed_ext) && in_array($_FILES["images"]["type"][$index], $allowed_types))
{
move_uploaded_file($tmpName, ROOT_DIR . "../images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index]);
//replace photo in body
$Body = str_replace("[" . $index + 1 . "]", "<img class=\"blog_photo\" src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);
$photos[] = $_FILES["images"]["name"][$index];
}
}
$Template->assign_var("error", $Body);
}
}
}
答案 0 :(得分:0)
使用它来找出问题所在并比较输出
print_r($_FILES["images"]["tmp_name"]);
echo "<br />";
foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
{
//replace in body
$Body = str_replace("[" . $index + 1 . "]", "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);
echo "[" . $index + 1 . "]" . "<br />";
echo "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />";
}
答案 1 :(得分:0)
显然,问题确实很奇怪,认为是因为在执行$ index + 1时没有使用括号。
所以将代码更改为以下内容:
$Body = str_replace("[" . ($index + 1) . "]", "<img class=\"blog_photo\" src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);
确实会奏效。