开发者

Why is image request generating a 302?

开发者 https://www.devze.com 2023-04-05 23:42 出处:网络
I\'m using PHP to generate the following image element: <img id=\"uploaded-image\" src=\"http://localhost/gonzo/plans/image/123/tablemap.png\" />

I'm using PHP to generate the following image element:

<img id="uploaded-image" src="http://localhost/gonzo/plans/image/123/tablemap.png" />

This image URL is routed through my PHP MVC framework to the following function, which grabs the image from my db and prints it:

    public function image($hexCode='',$fileName) {

    if (!$this->validRequest('get')) return false;

    Framework::loadModel('plan_model.php');
    $plan = new PlanModel();

            // Return image data from Blob field in db.
    $image = $plan->getImage('123');

    if ($image) {
        header("Content-Type: image/png");
        print $image;
        die;
    } else {
        echo 'error';
    }
}

The request generates a 302, with the wrong location and content-type headers:

Why is image request generating a 302?

The browser does not display the image where this image tag is on the page. But, when the image URL is typed directly into the address bar, the image is displayed.

Why would my server be generating a 302 when loading the image into an tag, but generating a 200 when the image URL is typed directly into开发者_JS百科 the address bar? I'm running Apache on Windows.


The described behavior isn't possible at all - since the server doesn't know a bit about how an URL is embedded in a web-page.

Provide more details of the actual call of the PHP-script: Print environmental details - $_GET, $_POST and $_SERVER - to a log file.

It might be helpful to register a tick-function and trace each executed statement to a log-file.

Check your PHP sources.


It turns out that this was caused by a strange concurrency problem. My framework was still processing the first request (which generated the page containing the img element) while the browser requested the image.

In other words, when the browser requested the image, it was still receiving output from the first request to the script. This made things go wacky.

Output buffering didn't solve the problem, but possible solutions are:

  1. Pause execution of the second (image request) script for a sufficient amount of time to let the first one complete.
  2. Host the image serving script on a separate subdomain.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号