tags:

views:

74

answers:

5

Hello, i can't understand one thing. In code, for example:

$filePath = 'http://wwww.server.com/file.flv';

if( file_exist($filePath) )
{
  echo 'yes';
}
else
{
  echo 'no';
}

Why does script return 'no', but when i copy that link to the browser it downloads?

A: 

file_exists() check for filesystem files and directories. Use fopen() too see whether that web URL is accessible. In case the respective server will return 404 Not Found for that resource, fopen() will return false and issue an warning. A much better solution would be to issue an HTTP HEAD request.

Ionuț G. Stan
+2  A: 

The file_exists() function is looking for a file or directory that exists from the point of view of the server's file system. If http://www.server.com/ equates to /home/username/public_html/ then you need to make your code:

$filename = '/home/username/public_html/file.flv';
if(file_exists($filename))
{ 
 //true branch 
}
else
{
 //false brach
}

See http://php.net/file_exists for more info.

Parvenu74
A: 

First of all the php function you need to use is file_exists() with the 's' at the end. Second of all, I think the path to the file needs to be a local file path, not a URL. Not sure though...

Pete
+1  A: 

use

$_SERVER["DOCUMENT_ROOT"]

to assure the right filesystem path, not dependent by development or production system for example.

in this case, it will be

$filePath = $_SERVER["DOCUMENT_ROOT"].'/file.flv';
avastreg