0

I am requesting an html file from the server A where the server A downloads it from another server(B) and send it to the original client. This html file has images embedded in img tags like <img src="abc.png">. In order to download those images successfully in the successive requests, I need to add a prefix to the src path like "prefix/abc.png" once server A receives the successive request for the image.

Now my problem is, is there a way I can figure out whether a request is for the page or for a successive request for a resource like image or script so I can add the prefix to the path properly?

lloydh
  • 395
  • 2
  • 5
  • 16

1 Answers1

0

Isn't is just as simple as checking if the requested path terminates in .png, .gif, .jpg etc... ? i.e.

/* $reqPath is for example /the/stuff/i/want/image.png */

if (in_array(strtolower(substr($reqPath, -4)), ['.png', '.jpg', '.gif', '.svg']) {
    /* It's an image! */
} else {
    /* Must be something else */
}

I added the strtolower just in case you have a request for IMAGE.PNG, Image.Png or something silly.

Jimmy
  • 553
  • 2
  • 9
  • I edited the question because I know that there are ways like you mentioned above, but I wonder if there is more advanced way to find it. What if the successive request is not only for images but for other resources like scripts etc. ? – lloydh Aug 15 '13 at 06:52
  • well someone could serve a .png file with a .xyz extension if the server sent the MIME type PNG in the response... The browser would render it as PNG, same as for file without extension if the MIME type was PNG. The only sure-fire way to do what you want to do is request everything from the other server, and see what it is by the returned MIME type. You could do a "HEAD" request rather than a "GET" so you don't have to recieve the file, you can check if the HEAD response suits your requirements and then GET the file. – Jimmy Aug 15 '13 at 06:55