PHP fopen function timed out? - php

any idea why fopen would timeout for a file if it is on my server and I know the url is correct?
update: sorry, i should have mentioned this is in php.
the code is:
fopen($url, 'r');
It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.
Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)

you can use
ini_set('default_socket_timeout',2);
before opening the fopen $url . This actually set the default socket connection timout without responding.
Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions.
Try this may be helpful for you.

It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?
If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:
the server really is taking a long time to send back the answer, or
the TCP connection is being blocked
For example, it may be that your local firewall rules only permit access to www.example.com if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.

maybe your "allow_url_fopen" is set to "Off"
check your php.ini file or phpinfo()

If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.
fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.

Check the comments on the documentation of fopen. There's a whole lot of gold in there.

Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.

Related

PHP move_uploaded_file over specific ports

Ok, so I've created a project where a client can drag and drop files onto our server and all works great! Now I've been asked to have the files that are being uploaded/transferred by our clients over a specific port range (let's say between 10000 and 11000 for argument sake). I do not know how to accomplish this. My current uploading function looks something like this:
File's name: test/upload.php
$dir = "path/to/directory/";
$tempFile = $_FILE['file']['tmp_name'];
$tagetFile = $dir.$_FILE['file']['name'];
move_uploaded_file($tempFile, $targetFile);
Where $_FILE is a file being uploaded.
Please disregard any syntax or spelling error in the code above, it works perfect at the moment. I have removed a lot of code to give a simplistic idea of what my code is currently doing.
If any configuration changes to PHP are to be made, they need to target this directory specifically as the rest of our website needs to stay on the current port. I am not exactly sure where to begin with specifying the ports to be used for file transfers. The file transfers are purely client to server and will never be vice-versa. We do have an FTP server setup however if possible, we'd like to remain off of it. I am not sure if what I am asking is possible otherwise.
I am using the Dropzone.js plugin (from here: http://www.dropzonejs.com/), however all the PHP code is mine.
I am not sure if something like the code below (from here) is the way to go, I've never used the fsockopen function before.
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
$responding = 1;
if (!$fp) { $responding = 0; }
$tend = microtime(true);
fclose($fp);
All answers are welcome. Thank you.
Since you want the client to upload the file on a different port, you will need to stand up a web server on that port. You could tell your current server to listen on that port, but that would do nothing to reduce load on your main website, so a separate machine is necessary. The new machine will have to be set up to listen to only the file upload port, but will need to contain your server (Apache, etc), PHP, and have access to the network storage location for your files.
If you have one, you may need to configure your firewall so traffic comes in to the correct machine depending on which port it is sent to.
The actual PHP code you use will not really be any different from what you have working now. Your JS code will need to be updated to it posts to the server using port 10000 or whatever you choose.
Here's a simple diagram that may help.

When I run following url on my browser it runs fine but when i use php function its getting 505 Page . Can any one help me with that

$output = file_get_contents("http://www.canadapost.ca/cpc2/addrm/hh/current/indexa/caONu-e.asp");
var_dump($output);
HTTP 505 Status means the webserver does not support the HTTP version used by the client (in this case, your PHP program).
What version of PHP are you running, and what HTTP/Web package(s) are you using in your PHP program?
[edit...]
Some servers deliberately block some browsers -- your code may "look like" a browser that the server is configured to ignore. I would particularly check the user agent string that your code is passing along to the server.
Check in your PHP installation (php.ini file) if the allow_url_fopen is enabled.
If not, any calls to file_get_contents will fail.
It works fine for me.
That site could be blocking the server that you're using to access it.
When you run the URL from your browser, your own ISP is used to get the information and display in your browser. But when you run from PHP, the ISP of your web host is used to get the information, then it passes it back to you.
Maybe you can do this to check and see what kind of headers its returning for you?
$headers=get_headers("http://www.canadapost.ca/cpc2/addrm/hh/current/indexa/caONu-e.asp");
print_r($headers);

Including a remote php file as a resource

I am trying to include remote php files as a resource but I am having a bit of trouble. I went into the php.ini files and set allow_url_fopen to ON. I also looked for the setting allow_url_include but it was not in the file, I added it to the php.ini file and also set that to on.
If I try to include using
include ('http://somewebsite.com/lib/somescript.php');
The server / php spits out a message saying:
URL file-access is disabled in the server configuration
I also get a message saying:
failed to open stream: no suitable wrapper could be found in blah blah blah
The seconed way I am trying to acomplish the same result is using fopen but I am just getting the content of the file, thats not what I need I need my local script to see the remote script as an executabel rescource.
$myscript = fopen("http://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
// include in the contents of my_script.php
echo $incmyscript;
I have to be doing something wrong? I know echoing out the variabel $incmyscript is wrong, but I can't think of a way to place in the code. I am not sure if fopen is the best best way to get what I want.
Any ideas?
The message you are getting:
URL file-access is disabled in the server configuration
Indicates that the allow_url_include setting in your php.ini is set to Off. Enabling that option will allow you to do remote file inclusion, but be very careful with this as it's a pretty big security risk once the other site would be compromised (A hacker could easily inject their own remote code to your site).
Instead of echo, you could use eval.
Only do this if you want to execute PHP code from the other server, not if you just want to include HTML!
Even if you really want to execute PHP code from the other server, a man-in-the-middle could execute arbitrary PHP code on your server. You should therefore better use HTTPS or avoid the inclusion of the remote file at all.
Example:
$myscript = fopen("https://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
$incmyscript);
Instead of the echo you could use this:
eval($incmyscript);
But be careful, this is very bad practice!
READ THIS: http://php.net/manual/en/function.eval.php
If you can trust remote script then you can call eval:
eval ($incmyscript);
If http://somewebsite.com/lib/somescript.php served by server supporting PHP you're trying to include it's output, not the code itself! Otherwise it's a just wrong and may be considered as security hole!
What you're trying to do is opening of a major security hole!
If the remote server is configured to process .php files, you won't be able to get the source for it. The server will process the PHP and then return any output. If getting remote PHP sources were possible, hackers would be grabbing our code and looking for vulnerabilities way too easily!

The server at www.localhost.com is taking too long to respond

A very strange thing is happening. I am running a script on a new server (it works on my current server and laptop).
The strange thing is that I only get it to (sort of) work when I increase memory limit to 1024M (!). It is extracting a large zip file and going through the files, so I thought it was normal. Instead of this script terminating or ending with errors. I get an error from my browser:
The server at www.localhost.com is
taking too long to respond.
Localhost.com? The web server is just localhost:9090 and I can see Apache is still running. Maybe Apache crashes momentarily and it can't find the server? But nothing about apache crashing in the log files.
This isn't a server issue, its more to do with my PHP script and memory usage I think, so no need to move to server fault.
What could be the problem? How can I narrow do the cause, I am at loss here!
The server is a windows server running Apache 2.2 with PHP version 5.3.2. May laptop and the other working server are running version 5.3.0 and 5.3.1 for PHP.
Thanks all for any help
Ensure that,
ini_set('display_errors','On');
ini_set('error_reporting',E_ALL);
ini_set('max_execution_time', 180);
ini_set('memory_limit','1024MB' );
I'd pop this in the top of the script and see what comes out. It should show you errors and the like.
The other thing, have you checked fopen and the path of the file which it's loading?
Abs said,
check files being zipped up can be zipped by PHP (permissions
especially on a Windows OS with multi
users)
I kept getting this problem too, and none of these sites really helped until I started looking at the same thing for people using Internet Explorer. The way I fixed it is to open up the system hosts file, located at C:\Windows\System32\drivers\etc\hosts, and then uncomment out the line that mentions ::1, which is needed for IPv6. After that it worked fine.
Somehow your system's munged up and isn't treating localhost as the local 127.0.0.1 address. Is your hosts file properly configured? This is most likely why you're getting the "too long to respond" error:
marc#panic:~$ host www.localhost.com
www.localhost.com has address 64.99.64.32
marc#panic:~$ wget www.localhost.com
--2010-08-03 22:41:05-- http://www.localhost.com/
Resolving www.localhost.com... 64.99.64.32
Connecting to www.localhost.com|64.99.64.32|:80... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.
www.localhost.com is full valid hostname as far as the DNS system is concerned.
I am not a php guru by any means but are you writing the extracted files to a temporary local storage location that is within the scope of the application? Because if you are not then I think what is happening is that the application is storing the zip file and extracted files in memory and then is attempting to read them. So if it is a large zip and/or the extracted files are large that would introduce a huge amount of overhead on top of the overhead introduced by your read and processing actions.
So if you are not already I would extracted the files and write them to disk in their own folder, dispose of the zip file at this point, and then iterate over the files in your newly created directory and perform whatever actions you need to on them.

php_network_getaddresses: getaddrinfo failed: Name or service not known

Here is a snippet of my code
$fp = fsockopen($s['url'], 80, $errno, $errstr, 5);
if($fp){
fwrite($fp, $out);
fclose($fp);
When I run it, it outputs:
unable to connect to www.mydomain.net/1/file.php:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known
I'm using this to submit GET data to the $s['url']
I can't figure out why. Any help would be greatly appreciated.
You cannot open a connection directly to a path on a remote host using fsockopen. The url www.mydomain.net/1/file.php contains a path, when the only valid value for that first parameter is the host, www.mydomain.net.
If you are trying to access a remote URL, then file_get_contents() is your best bet. You can provide a full URL to that function, and it will fetch the content at that location using a normal HTTP request.
If you only want to send an HTTP request and ignore the response, you could use fsockopen() and manually send the HTTP request headers, ignoring any response. It might be easier with cURL though, or just plain old fopen(), which will open the connection but not necessarily read any response. If you wanted to do it with fsockopen(), it might look something like this:
$fp = fsockopen("www.mydomain.net", 80, $errno, $errstr, 30);
fputs($fp, "GET /1/file.php HTTP/1.1\n");
fputs($fp, "Host: www.mydomain.net\n");
fputs($fp, "Connection: close\n\n");
That leaves any error handling up to you of course, but it would mean that you wouldn't waste time reading the response.
I had a similar problem on my local testserver and local testdomains e.g.: www.testdomain.loc with the function GetImageSize();
Solved it by adding the hostname in the hosts file on the local server:
In the file /etc/hosts I added:
192.168.1.1 www.testdomain.loc
If you only want to submit GET data to the URL, you should use something straightforward like file_get_contents();
$myGetData = "?var1=val1&var2=val2";
file_get_contents($url.$myGetData);
Had such a problem (with https://github.com/PHPMailer/PHPMailer), just reload PHP and everything start worked
for Centos 6 and 7:
service php-fpm restart
$url = "http://user:pass#www.example.com/abc.php?var1=def";
$contents = file_get_contents($url);
echo $contents;
you are trying to open a socket to a file on the remote host which is not correct. you could make a socket connection (TCP/UDP) to a port number on a remote host. so your code should be like this:
fsockopen('www.mysite.com', 80);
if you are trying to create a file pointer resource to a remote file, you may use the fopen() function. but to do this, you need to specify the application protocol as well.
PHP provides default stream wrappers for URL file opens. based on the schema of the URL the appropriate stream wrapper will be called internally. the URL you are trying to open does not have a valid schema for this solution. make sure there is a schema like "http://" or "ftp://" in it.
so the code would be like this:
$fp = fopen('http://www.mysite.com/path/file.txt');
Besides I don't think the HTTP stream wrapper (that handles actions on file resources on URLs with http schema) supports writing of data. you can use fread() to read contents of a the URL through HTTP, but I'm not sure about writing.
EDIT:
from comments and other answers I figured out you would want to send a HTTP request to the specified URL.
the methods described in this answer are for when you want to receive data from the remote URL. if you want to send data, you can use http_request() to do this.
I was getting the same error of fsocket() and I just updated my hosts files
I logged via SSH in CentOS server. USERNAME and PASSWORD
type
cd /etc/
ls //"just to watch list"
vi hosts //"edit the host file"
i //" to put the file into insert mode"
95.183.24.10 [mail_server_name] in my case ("mail.kingologic.com")
Press ESC Key
press ZZ
hope it will solve your problem
for any further query please ping me at http://kingologic.com
In my case this error caused by wrong /etc/nsswitch.conf configuration on debian.
I've been replaced string
hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns
with
hosts: files dns
and everything works right now.
Try to set ENV PATH. Add PHP path in to ENV PATH.
In order for this extension to work, there are DLL files that must be available to the Windows system PATH. For information on how to do this, see the FAQ entitled "How do I add my PHP directory to the PATH on Windows". Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the system's PATH), this is not recommended. This extension requires the following files to be in the PATH: libeay32.dll
http://php.net/manual/en/openssl.installation.php
in simple word your site has been blocked to access network. may be you have automated some script and it caused your whole website to be blocked. the better way to resolve this is contact that site and tell your issue. if issue is genuine they may consider unblocking
I had a similar problem when connecting to a local MySQL database, only changed the url for the ip address 127.0.0.1 and it worked. You could change the url for the ip address of the server.

Categories