标头位置无法正常工作
我有一个 php 脚本,它渲染图像(使用 imagick)并将其保存到某个目录 "SITE_ROOT.$filePath"
,然后执行 header('Location: ' . SITE_ROOT.$filePath )
,它重定向到的文件是一个 png 图像。
如果我直接转到路径,就像只需在 URL 栏中输入它一样,我可以保存图像并且一切正常,但是当我依靠脚本来重定向我并且我尝试右键单击并保存图像时,它不会认识到我实际上正在尝试保存图像,它认为我正在尝试将其保存为名为“Driver”的非类型文件,这是脚本页面的名称。
我不知道这里出了什么问题,标题位置肯定应该将我带到图像,并且在重定向后没有“驱动程序”文件的记录?
顺便说一句,redirect()
也会发生同样的事情。
预先感谢您的任何帮助!
编辑:通过在 header 命令后面放置 die()
解决了这个问题。
I have a php script that renders an image (with imagick) and saves it to some directory "SITE_ROOT.$filePath"
, then does a header('Location: ' . SITE_ROOT.$filePath)
, the file it redirects to is a png image.
If I go to the path directly, like just type it in the URL bar I can save the image and everything works fine, however when I rely on the script to redirect me and I try to right click and save the image it doesn't recognise that I'm actually trying to save an image, it thinks I'm trying to save it as a non-typed file called 'Driver' which is the name of the script page.
I have no idea what's wrong here, surely the header location should just take me to the image and have no record of the 'Driver' file after it has redirected?
The same thing happens with redirect()
too btw.
Thanks in advance for any help!
Edit: This problem was solved by placing a die()
after the header command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的文件路径不适用于标头位置。你应该使用网址。
最佳做法是在标头位置中使用绝对网址。 PHP 文档说:
之后也始终退出脚本,因为根据我的经验,在某些情况下重定向后的代码可能仍会被执行。因此,一个很好的例子如下所示:
通常您会在这种情况下使用服务器变量:
干杯!
You are using file paths which is not working with header location. You are supposed to use urls.
It's best practice to use absolute urls in header location. PHP documentation says:
Also always exit the script afterwards because otherwise in my experience in certain circumstances code that comes after the redirect might still be executed. So a good example would look like this:
Often you would use a server variable for this case:
Cheers!
header('位置:' . $filePath);
SITE_ROOT 是 htdocs 目录在服务器上的位置;但 header Location 应该是文件相对到 htdocs 目录的路径
header('Location: ' . $filePath);
SITE_ROOT is the location of the htdocs directory on the server; but header Location should be the path to the file relative to the htdocs directory