Sangha-08 Posted June 9, 2008 Share Posted June 9, 2008 hi, i just wanted to add a new function in my script that fetches the flv file from veoh.com . I want to add a function if file exists or not. I just want a simple function to see if the file exists or not , if i doesn't then i want to load a video thats hosted on my server... any suggestions ? Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/ Share on other sites More sharing options...
abdfahim Posted June 9, 2008 Share Posted June 9, 2008 $filepath=path_to_your_file; if(file_exists($filepath)){ //do whatever you want } oops sorry .. I misread ur post. I thought the file you wanna chk is on your server I think you can't do it by PHP cause php is server side language. So it has no idea what is happening in outer world other than its server. So you have to go for any client side language like Javascript etc. Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/#findComment-561345 Share on other sites More sharing options...
Psycho Posted June 9, 2008 Share Posted June 9, 2008 Here's a quick and dirty function that will work using file_get_contents. Although I remember reading information about this previously that there are some specific issues when doing this. It worked n some simple tests that I ran though. function httpFileExists($url) { if (!@file_get_contents($url)) { return false; } return true; } Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/#findComment-561468 Share on other sites More sharing options...
DarkWater Posted June 9, 2008 Share Posted June 9, 2008 Here's a quick and dirty function that will work using file_get_contents. Although I remember reading information about this previously that there are some specific issues when doing this. It worked n some simple tests that I ran though. function httpFileExists($url) { if (!@file_get_contents($url)) { return false; } return true; } That could be bad because it has to download the whole file first. Give me a second and I'll see if I can think of something. Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/#findComment-561470 Share on other sites More sharing options...
Psycho Posted June 9, 2008 Share Posted June 9, 2008 This appears to work too if (!@fopen($file, 'r')) EDIT: Just did some searching and it appears the manual for file_exists() includes several scripts in the user comments for external filess: http://us.php.net/manual/en/function.file-exists.php Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/#findComment-561568 Share on other sites More sharing options...
Orio Posted June 9, 2008 Share Posted June 9, 2008 That function is way too puffed with unneeded stuff. Try this: <?php function http_exists($url) //$url must be valid, starts with http:// { $headers = get_headers($url) if($headers === FALSE) return FALSE; return (strtolower($headers[0]) == 'http/1.1 404 not found'); } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/109421-script-to-see-if-the-file-exists-or-not/#findComment-561572 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.