Web Hosting Talk







View Full Version : file_exists - Please help me with this function!


dpny
06-04-2004, 07:23 PM
I've tried lots of way trying to figure out the best way to check if file exist on a remote server and none worked out, the file I am trying to see if exist is a music file (.rm)
I tried using this:


$filename = 'http://server.com/file.rm';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}


Even thought the file is there, all I get is the File does not exist :(

What am I doing wrong here?
:eek:

SeņorAmor
06-04-2004, 07:34 PM
IIRC, it has to be a local file.

file_exists('/path/to/file/here');

dpny
06-04-2004, 07:51 PM
how do I check on a remote server?
;)

ilyash
06-04-2004, 08:36 PM
how about the program downloads it and reads a few bytes out of the file.

laserlight
06-05-2004, 02:23 AM
You cant use it on remote files, at least on PHP4.

Not so sure about PHP5 though.

phatlenix
06-05-2004, 03:02 AM
if(file("http://google.com")) { echo "yay!"; } else { echo "get ur **** fixed!" }

dpny
06-05-2004, 12:38 PM
phatlenix, i tried on a .RM file, when I used a dummy file.. this is what I get:

Warning: file(http://www.***********.com/girlfriend42.rm): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/******/public_html/test.php on line 2
get ur **** fixed!

p.s. u forgot the <b>;</b> on your 2nd echo...

dpny
06-05-2004, 12:41 PM
I added the error_reporting(0);
.. hehe.. i like that now!!!

I needed this to make a http streaming jukebox, what happens is that my music is on server2 and site is on server1.. I just want to make sure server2 is responding .. instead of giving the jukebox the url if the file isn't there.. i dont want ppl finding out the URL on realplayer when it displays error msg along with the URL
heh!

Burhan
06-06-2004, 02:59 AM
Wouldn't it just be easier to setup a streaming server on server2?

Seems like a round-about way of doing things.

Imagine if you were checking for a 40 megabyte file. Your PHP script would have to download it -- (since file() opens a file and returns an array with the file contents). For large files, your script would most likely time out.

A better (but that not great) solution would be to use fopen() and read only a few bytes.

TechSolution
06-06-2004, 10:37 AM
$strServer = 'server.com';
$strURI = '/file.rm';

$objHTTP = fsockopen($strServer, 80);

fwrite($objHTTP, 'HEAD '.$strURI." HTTP/1.0\r\nHost:".$strServer."\r\n\r\n");

$strBuffer = '';

while (!(feof($objHTTP))) {
$strBuffer .= fgets($objHTTP, 128);
}

fclose($objHTTP);

$intStatus = substr($strBuffer, 9, 3);

if ($intStatus != 404) {
// file exists
} else {
// file unavailable
}


Hope this helps. It should only use 1-2 kbytes of data transfer.

dpny
06-06-2004, 05:41 PM
Now what I am doing is...
I've uploaded a very tiny 1x2 image file on the song folder, and check to see if the file is up... then make the playlist.. :D
the only problem now is...
while the song is playing, if the server goes down.. the user will end up getting file not found error from realplayer...
:eek:

sym0ng
06-08-2004, 06:46 AM
then make sure it dont go down without warning them ! :P

dpny
06-08-2004, 02:15 PM
lol..
ya..
but for now.. i'll make sure the server is up.. heh!