Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP脚本替换图像_Php_Apache_.htaccess - Fatal编程技术网

通过PHP脚本替换图像

通过PHP脚本替换图像,php,apache,.htaccess,Php,Apache,.htaccess,我正试图通过一个PHP脚本为我的所有图像提供服务。最终的目标是在满足某些条件的情况下动态地替换图像,但现在我只是尝试让脚本返回一个特定的图像文件。我有一个.htaccess,它正在向这个脚本发送所有图像请求,但目前图像没有显示在页面上 PHP脚本的完整内容: $img = '/imgs/img1.jpg'; $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($img)

我正试图通过一个PHP脚本为我的所有图像提供服务。最终的目标是在满足某些条件的情况下动态地替换图像,但现在我只是尝试让脚本返回一个特定的图像文件。我有一个.htaccess,它正在向这个脚本发送所有图像请求,但目前图像没有显示在页面上

PHP脚本的完整内容:

$img = '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);

我想获取重定向到此脚本的图像请求的URL。根据具体情况,我希望立即为映像提供服务,或者使用映像生成一个带有GD的新映像并返回该映像。

您提供的是硬盘中位置的路径,而不是docroot。因此,除非文件位于硬盘中的/imgs/中,否则它将无法工作。尝试:

$img = $_SERVER['DOCUMENT_ROOT'] . '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);

为什么要通过HTTP下载图像?现在为什么不从磁盘上提供呢?您可能没有启用HTTP fopen包装器。您也可以根据需要302重定向到映像。我同意,请改为提供映像的相对路径。将文件路径更改为/imgs/img1.jpg,但仍然无效。您提供的是指向硬盘中位置的路径,而不是docroot。因此,除非位于硬盘驱动器/imgs/中的文件无法工作。您如何知道.htaccess中的重定向正在工作?也许不是。