Can't open .txt or a FTP connection with PHP


I’ve recently started getting some errors when trying to run php scripts that was suposed to open/read external .txt files.
I also can’t open FTP connections with php codes, because it just returns that the connection couldn’t be made.

Examples of things that I’m trying out:

// This gives me a “failed to open stream: No such file or directory” even though, if I just copy that address in a web browser, it opens up fine!
$array = file(“”);

// This one timeout after a while.
$array = file(“”);

The script that I’m trying to run is vital to our business, so I did the following as a workaround:

// It Works! (note that I had to go one level below, since the script is in another folder)
$array = file_get_contents(’…/folder1/folder2/test.txt’);

So, it seems like the problem is something with the php script not being able to resolve the “www” address? In the case of the .txt file (that is hosted inside the same server) it is pretty much ok, since I could open via manual pathing.

But that also happens when I try to open a FTP address (that is hosted in the same server):

// This doesn’t work, it can’t establish a connection
// “ftp_login() expects parameter 1 to be resource, boolean given” is returned
$conn_id = ftp_connect(“”);

$login_result = ftp_login($conn_id, “user”, “pass”);
print ($login_result);

The thing is that I need to open a ftp connection in the php script, because the files that I need to use is in another user (the .php scripts are in UserA, while the files that I need to manipulate is in UserB). Again, the FTP connection works fine when I try opening it with a web browser, or a FTP Client, using the same credentials that I gave in the code…

What confuses me even more, is that these scripts that I’m trying to run was working fine for a whole 2 years or so, and suddenly, it stopped working! (more specifically, since the last week). I should also note, that there hasn’t been any major (or minor) update in the server (in fact, it literally stopped working for no reason).

Sorry for the long post, I wanted to make sure that I didn’t miss anything.

uhm… silly question: are you sure that your site works via http and it’s not https only? Have you checked the httpd access and error logs for more precise suggestions?

Similarly for the ftp, are you sure that plain ftp is still available on the server? And which server are we talking about (are these DreamHost servers? shared hosting, vps or what?)

Yeah, I’m sure that it works via http, I can even access the .txt file via a web browser (tried to access via https, and it gives me a timeout).

In the access log, when I try to open the FTP script, it returns me with this line:

There’s nothing in the error log file… except if I try to open the original script (that tries to open one FTP connection, send a file, close the connection, and repeat that 2 more times - 3 FTP’s connection at the end of the script execution). In that case, I get a “Internal Server Error” page

The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

  • It also writes the following lines into the error.log file:
[Tue Apr 11 08:45:54 2017] [error] [client *IP from my work computer*] Premature end of script headers: updateFTP_all.php
[Tue Apr 11 08:45:54 2017] [error] [client *IP from my work computer*] File does not exist: /home/user/domainName/internal_error.html

As for the specific questions about the FTP:
Yes, I can access the FTP address normally. I even made a copy of the script, and ran it in my localhost server and there was no errors…
The FTP is on the same domain than the webpages! (example: site name is “” . FTP access is “”).

It is a Dreamhost VPS server.
If necessary, I can share the .php code, but I don’t think that the problem is in the script, since it works fine in localhost.

  • “domainName” is just a substitute for the real domain name, ok?

Thanks for reading this!

so if I read the timestamps correctly, the code read_FTP.php starts execution at 08:30:14 and around 15 minutes later the server throws a Premature end of script headers error. Is it normal that read_FTP takes over 15 minutes to execute? If so, then the error and that execution time suggests that the code may be timing out.

No, it doesn’t take 15 mins to execute, the first one: “/fileOpenTest/read_FTP.php” is a test script that I sent to the DH server, it only tries to establish one FTP connection (that doesn’t finish - the page returns a “ftp_login() expects parameter 1 to be resource, boolean given” error, that is the same thing than not getting an answer from the FTP connection).

The second script, that I tested 15 mins later, is the “real” one, that tries to open the 3 FTP connections (one at a time, of course): “updateFTP_all.php”.

This one most likely returns the “Internal Server Error” page because none of the 3 attempts return anything that the script can work on…

Is there something specific in the DH server that may be blocking the resolution of the addresses?
Because as I mentioned before, the script works fine locally (a 100% exact copy of the script that is running in the DH server).

Again, thanks for taking your time to answer me!

oh, right… i didn’t realize that the names of the php files were different in the logs.

I hope that the Internal Server Error in case of a missed name resolution would be different and more specific. Maybe try to add more debugging info to your php script, to see where it stops. Have you checked that your local environment and the remote one are the same (versions and various options?) Checking php docs, I noticed [quote]Tip A URL can be used as a filename with this function if the fopen wrappers have been enabled. [/quote] on


I did some tests, and by accident (read that as “trial and error”, mostly) I discovered that the direct IP number of the server works when trying to establish the connection (for both opening a .txt file and a FTP connection), as in:
// This doesn’t work
$conn_id = ftp_connect(“”);
// This… works!
$conn_id = ftp_connect(“XXX.XXX.XXX.XXX”);

// This doesn’t work
$txtFile= file_get_contents(‘’);
// This… works!
$txtFile= file_get_contents(‘http://XXX.XXX.XXX.XXX/folder1/folder2/file.txt’);

So… the problem was that the server can’t resolve dns’s. The actual question is "why?"
I mean, the DNS is suposed to be a name that points to the IP number, right?
I’ve read that setting “allow_url_fopen = On” in the php.ini file should fix this, but then again, some more questions:

  1. As far as I know, we can’t edit the php.ini file in a VPS, right?
  2. It was working fine until sometime ago, so, what caused it to change this setting (if it did indeed change, that is)?