Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File_exists problem 1

Status
Not open for further replies.

freenut

IS-IT--Management
Jun 21, 2001
4
GB
I am trying to check if a file exists on a remote server, I have the file_exists command working fine on the local server but Im not sure how, what or where to specify the remote servers name if it is possible !!

 
Question:

How are you connecting to the remote server?
The answer lies within the fact how you connect.
Different protocols have different ways to detect.

is_file only works on your local file system of your box (unless you have a distributed filesystem such as AFS).
 
Hi DRJ478

Im not too sure of the best way of connecting - I know you can FTP using PHP but find this a little messy. What other ways of connection do you know?? Im only going to check if a file exists so there is no need to copy it or read it.

Thanks
Freenut
 
What kind of server are you referring to? A fileserver, a webserver? Or do you really just mean another network client? What pair of OS's are you working with?

-Rob
 
I think you can use HTTP as long as the file is web-accessible:

Code:
$myUrl = "[URL unfurl="true"]http://www.myServer.com/myPath/myFile.html";[/URL]
# parse the URL so you get the parts you need
$fsPara = parse_url($myUrl);
# open a socket connection to the server
$fID = fsockopen($fsPara["host"], 80);
if (!fID){
   echo("Could not connect");
} else {
   # use a HEAD header to see if it returns status code 200
   fputs($fID, "HEAD $fsPara["path"] HTTP/1.0\r\nHost: $fsPara["host'}\r\n\r\n");
   # read the response
   $head = fread($fID, 4096);
   fclose($fID);
   # check for status 200 in the header
   if (!preg_match('#^HTTP/.*\s+200\sOK\s#i', $head)){
       # ... no file;
   } else {
       # file was accessible through HTTP
   }
}

Caveats:
If the file is not accessible via HTTP it doesn't mean it's not there. Anything else than status 200 fails with the above code.
 
We are using Sco and RH Linux, the files will not be web accessible - what options does that leave us??

Freenut
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top