繁体   English   中英

在Windows上使用Apache的图像路径

[英]Paths for images using Apache with Windows

我在Windows上使用Apache在本地进行链接测试时遇到问题。 最终链接具有以下格式:

file:///C:/Documents and Settings/USUARIO/My Documents/xampp/htdocs/my_website/images/january/my_image.jpg

图像不显示。 但是,如果我直接从img标签复制src并将其粘贴到浏览器(Firefox)中,则会显示该图像。

我使用以下代码生成链接:

我使用以下命令define(IMAGE_DIR, dirname(__FILE__).'/images/');图像目录: define(IMAGE_DIR, dirname(__FILE__).'/images/');
然后,我使用以下格式设置路径:

$imgPath = IMAGE_DIR.$month.$img;   
if($localServer)
{
   $imgPath = str_replace('/','\\', $imgPath);
   $imgPath = 'file:///'.$imgPath;
}

我做错了什么?

您正在生成从Web服务器加载的页面中使用file:// URL的图像源。 文件URL指示浏览器直接从磁盘加载文件,而无需向Web服务器发出请求。 出于安全原因,Firefox和其他浏览器不允许Web服务器提供的页面引用本地文件,因此请勿显示图像。 远程访问您的站点的人也将无法访问文件,因为图像源将是对其计算机路径的引用。

要显示图像,您需要更改图像源以引用Web服务器上的位置,例如images/january/my_image.jpg/images/january/my_image.jpg

网页浏览器将相对于所请求的页面解释images/january/my_image.jpg 例如,如果图像显示在页面上的http://localhost/page1.php ,则浏览器将请求http://localhost/images/january/my_image.jpg 但是,如果在页面http://localhost/ subdir /page1.php上使用该图像,则浏览器将改为请求http://localhost/ subdir /images/january/my_image.jpg

/ images/january/my_image.jpg将始终相对于网站的根进行解释。 Web浏览器将为http://localhost上的任何页面请求http://localhost/images/january/my_image.jpg

如果您需要引用其他Web服务器上的图像,例如http://otherhost/images/january/my_image.jpg ,则也可以使用绝对URL。

最好使用常量“ DIRECTORY_SEPARATOR”代替“ /”或“ \\”

暂无
暂无

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

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