[英]Image isn't showing up even though path is correct
Right now my file structure looks like this: 现在我的文件结构如下所示:
root
/images
/banner.png
/dir
/file.php
/index.php
I'm working in file.php
. 我在file.php
工作。 I'm trying to access banner.png
, and so far I have this: 我正在尝试访问banner.png
,到目前为止我有这个:
<?php echo dirname( dirname(__FILE__) ) . '/' . $recent1["Image"];?>
Where $recent1["Image"]
is images/banner.jpg
. $recent1["Image"]
是images/banner.jpg
。
When I echo the code as plain text, this path shows up: 当我将代码作为纯文本回显时,此路径显示:
/home/htdocs/images/banner.png
Which I feel like is right, but I'm not sure if it's the exact same thing as 我觉得这是对的,但我不确定它是否完全相同
(root)/images/banner.png
Again, I feel like those two are the same thing, but the image isn't displaying when I put that code I showed above in for the src
attr for an img
tag. 同样,我觉得这两个是相同的东西,但是当我将上面显示的代码放在src
attr中用于img
标记时,图像不显示。 It shows that broken picture icon thing. 它显示了破碎图片图标的东西。 Yes, I'm sure that images/banner.png
exists. 是的,我确定images/banner.png
存在。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。
Edit: I also might add that in index.php
, using just $recent1["Image"]
does indeed display the image. 编辑:我也可以在index.php
添加,只使用$recent1["Image"]
确实显示图像。
Try using a different Magic constant 尝试使用不同的Magic常量
I think you're looking for 我想你在找
__DIR__
Using your provided example 使用您提供的示例
<?php echo __DIR__ . '/' . $recent1["Image"];?>
Instead of __FILE__
you should use $_SERVER['PHP_SELF']
. 而不是__FILE__
你应该使用$_SERVER['PHP_SELF']
。 The documentation explains: 文档说明:
The filename of the currently executing script, relative to the document root. 当前正在执行的脚本的文件名,相对于文档根目录。 For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php. 例如,地址为http://example.com/foo/bar.php的脚本中的$ _SERVER ['PHP_SELF']将为/foo/bar.php。
<?php echo dirname( dirname($_SERVER['PHP_SELF']) ) . '/' . $recent1["Image"];?>
You could also use $_SERVER['SCRIPT_FILENAME']
. 你也可以使用$_SERVER['SCRIPT_FILENAME']
。 They could be different if you have URLs like /dir/file.php/foo/bar
(this is sometimes preferred for SEO reasons, although it's often rewritten in .htaccess
to /dir/file.php?param1=foo¶m2=bar
) 如果您有像/dir/file.php/foo/bar
这样的URL,它们可能会有所不同(这有时候因为SEO原因而首选,尽管它经常在.htaccess
重写为/dir/file.php?param1=foo¶m2=bar
)
You could also use a relative path: 您还可以使用相对路径:
<?php echo "../" . $recent1["Image"]; ?>
dirname()
twice. 提供的代码中的主要问题是您正在调用dirname()
两次。 <?php echo dirname( dirname(__FILE__) ) . '/' . $recent1["Image"];?>
"Given a string containing the path of a file or directory, this function will return the parent directory's path that is levels up from the current directory." “给定一个包含文件或目录路径的字符串,此函数将返回从当前目录升级的父目录的路径。” http://php.net/manual/en/function.dirname.php http://php.net/manual/en/function.dirname.php
Given that __FILE__
results to '/home/htdocs/index.php'
. 鉴于__FILE__
导致'/home/htdocs/index.php'
。
dirname(__FILE__)
= '/home/htdocs/'
dirname(__FILE__)
= '/home/htdocs/'
dirname('/home/htdocs/');
= '/home/'
= '/home/'
Then, you append the image path '/images/banner.jpg'
然后,您追加图像路径'/images/banner.jpg'
'home/images/banner.jpg'
When the path really should be 当路径真的应该
'/home/htdocs/images/banner.jpg'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.