Jump to content

script to see if the file exists or not


Sangha-08

Recommended Posts

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

$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.

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;

}

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.

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.