简体   繁体   English

李找不到图片

[英]li cannot find image

I have an ul with a li that cannot find a .PNG image associated with the list item 'li' in the code here: 我有一个带li的ul,在这里的代码中找不到与列表项“ li”相关的.PNG图像:

$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';
echo 'pngFilename is "', $pngFilename, '" -- that was the .png image filename.';


// within a 'ul' is this li item that displays an image (the ul code is simplified 
// to only show the item that (needs to but doesn't!) display a .PNG image.)
echo '<ul>';
echo '<li>';
echo '<a href="http://localhost/myProj/just_an.htm">';
echo '<img src="', $pngFilename, '"';            
echo 'alt="http://localhost/myProj/the_other.png"';

// NOTE -- I left out the close /> to the img statement when I copied my code here but 
// it was in fact in my source code.
echo '/>'; 

echo '</a>';
echo '</li>';
echo '</ul>';

Before I wrote the above php code, I tested just the raw html and it was fine -- my .PNG file called C:/xampp/htdocs/myProj/just_a.png was displayed correctly. 在编写上面的php代码之前,我只测试了原始html,这很好-我的.PNG文件名为C:/xampp/htdocs/myProj/just_a.png正确显示了。

But when I switched to php server-side generation of the html, the C:/xampp/htdocs/myProj/just_a.png image does not appear, only the 'cant find it' default small image appears that looks like a piece of paper torn horizontally. 但是,当我切换到html的php服务器端生成时,不会出现C:/xampp/htdocs/myProj/just_a.png图像,仅出现“无法找到”默认的小图像,看起来像一张纸水平撕裂。

Any ideas? 有任何想法吗? The .png file exists and so does the directory and the html correctly displayed the image, but when I put the html into php 'echo' calls I must be screwing something up, just not sure what. .png文件存在,目录和html也正确显示了图像,但是当我将html放入php'echo'调用中时,我必须搞砸了,只是不确定是什么。

To make sure I have the correct path and filename you'll see I echo it out at the top of the code. 为确保我具有正确的路径和文件名,您将在代码顶部看到将其回显的信息。
The php variable $pngFilename is displayed when I do 'View Source' in the browser as: 当我在浏览器中执行“查看源代码”时,将显示php变量$ pngFilename,显示为:

pngFilename is "C:/xampp/htdocs/myProj/just_a.png"  -- that was the .png image filename.

The only other thing to mention is that the 'alt' link, the 'alt="http://localhost/myProj/the_other.png"' -- this link (not the image) shows as blue underlined link text. 唯一要提及的是“ alt”链接,即“ alt =“ http://localhost/myProj/the_other.png””-此链接(不是图像)显示为带蓝色下划线的链接文本。

Why did this work in my html but breaks when I use the 'echo' in php? 为什么这在我的html中起作用,但是当我在php中使用“ echo”时却中断了? After all, the 'echo' simply sends the html to the client side -- and that .png file is 100% definitely there and displays fine when I run the above html outside of php's "echo" command. 毕竟,“ echo”只是将html发送到客户端-那个.png文件肯定是100%在那里,当我在php的“ echo”命令之外运行上述html时,它显示正常。

This is because the path to the image changes depending on how your viewing the page. 这是因为图像的路径会根据您查看页面的方式而变化。 in a local context or from a server context; 在本地上下文中或在服务器上下文中; and since you are using an absolute path instead of a Relative path the system can't adjust for the change in the location of the image. 并且由于您使用的是绝对路径而不是相对路径,因此系统无法根据图像位置的变化进行调整。 Unlike when using a PHP function that calls on the php file system functions that do use the internal file system. 与使用PHP函数调用确实使用内部文件系统的php文件系统函数不同。 what your doing is having it send a text file to the browser which isn't rendered as HTML code until after PHP has finished. 您要做的是将文本文件发送到浏览器,直到PHP完成后才将其显示为HTML代码。 because of that it has no access to the php file system to resolve the path to the image on the server. 因此,它无权访问php文件系统来解析服务器上映像的路径。 the way to fix it would be to use the path to the image Relative to the PHP script or use the web accessible path to the image 修复它的方法是使用图像路径(相对于PHP脚本)或使用Web可访问的图像路径

$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';

Should for example be 例如应该

$pngFilename= 'http://localhost/myProj/' . 'just_a.png';

or if the image and the php file are in the same directory you could just do 或者,如果图像和php文件位于同一目录中,则可以执行

$pngFilename= 'just_a.png';

You should try to close your img tag: 您应该尝试关闭img标签:

echo '<img src="', $pngFilename, '"';
echo 'alt="http://localhost/myProj/the_other.png">';

You are generating a image tag like: 您正在生成一个图像标签,例如:

<img src="C:/xampp/htdocs/myProj/just_a.png" alt="http://localhost/myProj/the_other.png" />

You cannot use this path, you need to change it to http://localhost/myProj/just_a.png or something like file:///C:/xampp/htdocs/myProj/just_a.png . 您不能使用此路径,需要将其更改为http://localhost/myProj/just_a.png或类似file:///C:/xampp/htdocs/myProj/just_a.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM