Im running PHP 5 and IIS 6 and whenever i try to run getimagesize() on a remote URL it will either timeout or give me a 401 unauthorized error.

I was running php 4.4.4 on IIS 5 and it worked just fine. I have been researching this most of the day with no luck. Anyone have a suggestion?

Thanks

    Ok, there seems to be a problem with opening any file remotely. The fopen wrapper is set to allow URLs. I am about certain there is a setting on the server that is causing this.

      Perhaps to help you in debugging, refer to phpinfo() - look near the top to ensure 'http' appears under "Registered PHP Streams"; also check that to make sure you're modifying the correct php.ini file that the interpreter is using

      Good luck

        i read the suggested line o phpinfo and http was in there. the php.ini file was also correct.
        Would it be a security issue on the IIS server?

          Could just be a compatibility issue... maybe IIS/PHP is being screwy?

          Are you positive allow_url_fopen (or whatever it is) is set to true? What happens if you just try to use [man]file_get_contents/man to download the image first?

            If it's coming back with a 401 error then for some inexplicable reason the remote server is asking for your script to authorise itself (either that or some bug in the URL wrapper code is mapping the server's response code badly). Maybe you've got a proxy between you and the other server that's being unnecessarily paranoid.

            theiviaxx wrote:

            there seems to be a problem with opening any file remotely.

            Any remote file? Including ones hosted by, say, the BBC? Or do you mean any file on the server you're trying to reach?

              Are you positive allow_url_fopen (or whatever it is) is set to true? What happens if you just try to use file_get_contents() to download the image first?

              yes, it is set to "On". i havent tried file_get_contents(), but even fopen() fails.

              Any remote file? Including ones hosted by, say, the BBC? Or do you mean any file on the server you're trying to reach?

              Anything i have tried so far. I have tried to get a file from the same server using:

              http://SERVER/files/file.ext

              and it will fail. however if i use:

              files/file.ext

              it works just fine. Another weird thing is if i go onto the server using remote access, i open IE or FF and try to go to the site, i can't get past the HTTP auth.

                I'm just curious why you need to call getimagesize on remote URLs anyways?

                As for the 401 message, I have seen many instances where the remote website employs a referer check to validate that the browser request to load an image comes from an html page on the same website. In other words, they want to prevent somebody from doing what your are attempting to do (well, the specific goal is to keep you from pulling their bandwidth for an image embedded in your website.) You might try sticking ethereal or another packet sniffer between to see what the request headers look like.

                  we have a CMS and it has a WYSIWYG editor. If someone pastes an image in there from a different web page, the image is linked. So my script attempts to copy the image locally so we are not dependent on someone else's image staying up.

                  how would they prevent me from just <img /> to their site? wouldn't that steal bandwidth just as easy?

                  I only have a few different server to try this on and it has worked fine on them. Just not this new one.

                  Thanks for all of your help

                    how would they prevent me from just <img /> to their site? wouldn't that steal bandwidth just as easy?

                    Yes, sometimes trying to hinder doesn't help in the end. 😃

                    I only have a few different server to try this on and it has worked fine on them. Just not this new one.

                    Are you saying the servers you have the application hosted on, or servers hosting the images being dropped into your WYSIWYG editor? If the latter, post the server URL and I can tell you real quick. Actually, you could just try inserting a <img src="http://SERVER/files/file.ext"> onto your website anywhere and see if image shows up at all.

                      Are you saying the servers you have the application hosted on, or servers hosting the images being dropped into your WYSIWYG editor? If the latter, post the server URL and I can tell you real quick. Actually, you could just try inserting a <img src="http://SERVER/files/file.ext"> onto your website anywhere and see if image shows up at all.

                      the image shows up if i use <img src="http://SERVER/files/file.ext" />. Just no php fopen functions.

                      i tried hosting the application on a few different servers and they are all able to perform an fopen on exteral URLs.

                      Unfortunately the problem server is internal only 🙁

                      I think i am going to ask the IT guys some questions. It's probably something simple of the server i am unaware of.

                        I was behind a proxy 🙁 So after a lot of research, i finally got it to work:

                        <?php
                        $aContext = array(
                             'http' => array(
                                  'proxy' => '[PROXY_URL:PORT]',
                                  'request_fulluri' => True,
                                  ),
                             );
                        
                        $cxContext = stream_context_create($aContext);
                        
                        $sImageURL ='http://image.jpg';
                        file_put_contents("image.jpg", file_get_contents($sImageURL, False, $cxContext));
                        var_dump(getimagesize("image.jpg"));
                        ?>
                        

                        Thanks for the help, much appreciated!

                          Ah, those evil proxies! Having been an IT guy and a PHP coder I can attest that IT guys get bored and try to think up ways to make work harder for everyone else 😃

                          Oh, and it looks like Weedpacket wins the "it might be this" contest. He mentioned a proxy a few posts back.

                            One more hurdle 🙁

                            I tried running this on a https:// address and it fails. Is this not going to be possible given that i am behind a proxy?

                              That depends; can your build of PHP do SSL (it'll need the [man]OpenSSL[/man] library)? There'll be a section in phpinfo if it is.

                                i had todo this for a project once... using curl solved all my problems

                                  It's possibly your firewall; https typically communicates via port 443 instead of port 80; the proxy would need to allow traffic through that. (There is also some stuff in Appendix M of the manual that may be relevant regarding the https:// stream wrapper and the ssl:// transport scheme; frankly I've tended to just go thump the network guys until they gave me what I wanted).

                                    hmmm, sounds like a bit much. I think i will punt this part of it 🙂 Thanks guys

                                      Write a Reply...