1

there are many questions on this topic, but I couldn't find an answer to my scenario.

The client project I am working on is hosted on a server running PHP 4.3.9 and I am trying to copy a remote image to the server. This always fails with a timeout:

var_dump(
   copy('http://domain.com/path/to/image.jpg', 
        '/opt/www/myfolder/myimage.jpg')
);

The error message for this being: Warning: copy(http://domain.com/path/to/image.jpg): failed to open stream: Connection timed out in /opt/www/myfolder/my-script.php on line xx bool(false)

I did a trace of several ini settings, which would seem fine to me.

var_dump(
    ini_get('upload_max_filesize'), 
    ini_get('post_max_size'), 
    ini_get('max_execution_time'), 
    ini_get('max_input_time'), 
    ini_get('allow_url_fopen'));

This traces:

string(2) "8M" string(2) "8M" string(2) "30" string(2) "60" string(1) "1"

So allow_url_fopen seems to be enabled, and the other settings seems fine too. I tried different images, all accessible via browser, all with small sizes, different remote hosts. My server's target directory is writable and copying for example a file from my own server's URI copies the file just fine.

I also tried a curl variant from this answer, with same result.

When I try the same script on a different server with newer PHP version it works fine.

What setting on the project server can cause this to fail? Is there something about PHP 4.3.9 I am missing that causes this to fail?

Please, please note that I cannot change the PHP version and I have no need for answers advising me to upgrade or convince my client to upgrade

Edit: Changed upload_max_filesize to 40MB and max_execution_time to 600 with no different result.

Community
  • 1
  • 1
kontur
  • 4,934
  • 2
  • 36
  • 62
  • Try SSH into the PHP4 server and download the file using wget or something. See if you get similar errors. – Salman A Dec 20 '12 at 08:34
  • @SalmanA I get a timeout same as with the `copy` - does that mean the user account on the server is not permitted remote calls of that type? If so, what does that leave me with to solve the problem? – kontur Dec 20 '12 at 08:42
  • 2
    Talk to your web hosting provider. A timeout means that the remote machine didn't respond *or* a firewall between you and them is dropping the connection silently (without rejecting the connection). Only your host will be able to investigate this issue. – Charles Dec 20 '12 at 08:51
  • In that case changing PHP version or using another script won't help. See @Charles comment. – Salman A Dec 20 '12 at 08:52
  • Also, big fat warning: PHP4 support was dropped *years* ago. Your web hosting provider is downright irresponsible for letting you continue to use it, and you probably want to dump their service. – Charles Dec 20 '12 at 08:52
  • Thanks for the input @Charles and @SalmanA. @Charles, would that also imply that `curl` variants of trying to solve this problem won't work? -- I am very unhappy with this hosting situation and well aware of the outdated PHP and the implications, so I can only repeat: "Please, please note that I cannot change the PHP version and I have no need for answers advising me to upgrade or convince my client to upgrade" – kontur Dec 20 '12 at 09:28
  • @kontur, a timeout is a timeout is a timeout, but there's no reason not to at least *try* curl out. Don't get your hopes up just in case. – Charles Dec 20 '12 at 09:34
  • Curl also failed, afraid to say. Charles, Salman A, both your comments are answers to my problem by having helped me figure out why the calls timed out. If you want to leave an answer, I will gladly accept, otherwise I provide a summary answer myself tomorrow for this topic. – kontur Dec 20 '12 at 10:40
  • Have you tried `file_get_content function` ? – Prasad Rajapaksha Dec 21 '12 at 07:42
  • Thanks for the suggestions. I have tried it right now, with same result. Previously, I also used `fopen`, but it seems to be indeed a connection problem independent from PHP itself. – kontur Dec 21 '12 at 07:45

3 Answers3

1

Chances are that you should increase the timeout for that specific request. So create a context for the copy command increasing the timeout:

$arrRequestHeaders = array(
        'http' => array(
            'method' => 'GET',
            'timeout' => 600
        )
    );
copy('http://example.com/path/to/image.jpg', '/opt/www/myfolder/myimage.jpg',stream_context_create($arrRequestHeaders));
 

You may have further information here https://www.php.net/manual/en/function.copy.php

  • While I appreciate the answer 10 years later, and in a generic case the timeout threshold might be the problem, as stated earlier it was an issue on the server side receiving/blocking the copy request. – kontur May 02 '23 at 07:59
0

As pointed out by @Charles and @SalmanA in the comments, there seems to be a connectivity problem on the server preventing the copy calls.

Based on @SalmanA's comment, trying to use wget from the ssh shell showed a similar timeout result, indicating a blocked connection.

Contacting the server host and inquiring further is the solution here.

kontur
  • 4,934
  • 2
  • 36
  • 62
-2

You should change the default max_execution_time to max_execution_time = 600 in php.ini - MUST

Please change the upload max size from its default value to your need in php.ini file or thourgh your code using ini_set().. this is helpful when u try to upload only...

upload_max_filesize = 40M

Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Thanks for your answer. Edited the ini values based on your input but with same result. Fails with timeout. – kontur Dec 20 '12 at 08:22
  • 1
    Why "must"? Explain in detail why the maximum execution time *must* be 600 seconds and why the *upload* filesize has to be changed? There is no file upload going on here. The upload settings are for files uploaded from the user, not over streams. – Charles Dec 20 '12 at 08:51
  • @Charles: Hello i have mentioned if file upload is there then it is needed ok... dont you see that?????? – Vinoth Babu Dec 20 '12 at 09:27
  • @Kuntur: please restart your apache after editing the ini values and check it out........... – Vinoth Babu Dec 20 '12 at 09:30
  • Thanks again for your input. I changed the values via .htaccess and `ini_set`and did the same `var_dump()` trace I mentioned in my original post, with all the changed values having been moved according to your suggestion. It just did not affect the outcome. – kontur Dec 20 '12 at 09:32
  • @Kuntur: please print and check the value from ini whether it is changed or not in browser... – Vinoth Babu Dec 20 '12 at 09:34
  • The values are changed. But same result, fails with timeout. – kontur Dec 20 '12 at 10:41