Problems writing file to server using fopen/file_exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TonyBunney
Forum Newbie
Posts: 7
Joined: Thu Jul 30, 2009 8:26 am

Problems writing file to server using fopen/file_exists

Post by TonyBunney »

Sorry guys, I have been trying a number of differrent ways to do this but I am now totally confused.
My require is:
1. Check to see if a file exists on a particluar server
2. if it does not then create the file with some contents.
I have read a number of articles about checking safe_mode etc and the server I am using, which is where all the files that I want to write to exists has the following settings:-
allow_url_fopen = On
allow_url_include = Off
file_uploads = On
safe_mode = Off
The server where I am running the script is the same server where I want to write the files to.
So I started to use
$FileToCheck = "http://www.mysite.com/Admin/test.php";
if (!file_exists($FileToCheck)) {echo "File " . $FileToCheck . " Does Not Exists";}

This always returns false even is the file exists.
So I tried another method as follows:
$FileToCheck = "http://www.mysite.com/Admin/test.php";
if($handle = fopen($FileToCheck, 'x+')) {echo "File Opened OK</br>";}
This always returns true even if the file does not exist.

So now I am confused and need some help on whether I need to check other php.ini setting or
why file_exists and fopen are not doing what I am expecting

Thanks in Advance.
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: Problems writing file to server using fopen/file_exists

Post by towerspl »

you cant use file_exists to check a file on another server, has to be on the local, same for fopen.

I had the same problem, the quick way i founf(hacky quick way) was to use file_get_contents(). Off the top of my head like the following:

Code: Select all

 
if(@file_get_contents("http://www.google.com") {
//exists
 
}else{
//dont exists
}
 
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems writing file to server using fopen/file_exists

Post by jackpf »

towerspl wrote:same for fopen.
That's not true. If you have "allow url fopen" set to "on" in php.ini, you can open remote files with fopen(), the same as file_get_contents().
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: Problems writing file to server using fopen/file_exists

Post by towerspl »

well you learn something new everyday
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems writing file to server using fopen/file_exists

Post by jackpf »

:)


Well, as far as I know. Here's some more info if you're interested though: http://uk3.php.net/manual/en/filesystem ... -url-fopen

Regards,
Jack.
Post Reply