I have been trying to find if a file_exist in the directory. If not i want to use a different image. But as i am using the file_exists function it always returns false.
The code i used is
while($r=mysql_fetch_row($res))
{
if(!file_exists('http://localhost/dropbox/lib/admin/'.$r[5]))
{
$file='http://localhost/dropbox/lib/admin/images/noimage.gif';
}
else
$file='http://localhost/dropbox/lib/admin/'.$r[5];}
But the function always return false even if the file exits. I checked that by using
<img src="<?php echo 'http://localhost/dropbox/lib/admin/'.$r[5]; ?>" />
This displayed the image correctly.
Please Someone Help Me
file_exists uses file system paths, not URLs. You use URLs in a browser to access your PHP scripts through a web browser and web server over the network. The PHP script itself can access the local file system though and uses that, it does not go through the network stack to access files.
So use something like file_exists('C:\\foo\\bar\\dropbox\\lib\\admin\\' ...).
You are passing URL to the file_exists function which is wrong. Instead of that pass your local path of the folder.
while($r=mysql_fetch_row($res))
{
if(!file_exists('/dropbox/lib/admin/'.$r[5]))
{
$file='/dropbox/lib/admin/images/noimage.gif';
}
else
$file='/dropbox/lib/admin/'.$r[5];
}
file_exists does not support addresses using HTTP (you can see this because stat is not included on the list of wrappers supported over HTTP). As the file_exists documentation says, remote files can be checked with some wrappers, such as FTP, but it is not possible over HTTP, so file_exists will always return false.
Presuming that this file is on the local machine (which is suggested by localhost, you'll need to access it with a local file path. It's hard to guess what this might be for you, but it might look like /var/www/dropbox....
file_exists() checks if file exists in local filesystem. You're passing an URL. Change it to local path to your dropbox directory and it should work:
if(file_exists('/path/to/your/dropbox'))
You are passing URL to the file_exists function which is wrong parameter. Instead of that pass your local path there.
To know more about file_exist() function read this php manual :
http://php.net/manual/en/function.file-exists.php
The function file_exists can only work for URL protocols that are supported by the stat() function in PHP.
Currently, the http protocol is not supported by this wrapper.
http://uk3.php.net/manual/en/wrappers.http.php
Related
I have a folder in my website named GameUploads and I am trying to use is_dir to test if it's is a directory, which I assumed meant the same as folder. The php code I run is in .../php/test.php and the folder I'm trying to reach is .../GameUploads/ where '...' is the url to the webpage. My code is here:
//initiate file check
$check_dir = "../../GameUploads/";
if(is_dir($check_dir)){
echo "$check_dir is a directory";
}
else{
echo "$check_dir is not a directory";
}
clearstatcache();
I'm not sure what I was trying to do with $check_dir but originally is was just .../GameUploads which returned false, and then I entered $check_dir='..' which returned true. I guess I'm trying to find a way to check if GameUploads is a directory from the php file. How can I do this? I'm not quite understanding how to use is_dir very well... I have already read through php.net and w3schools
"Yes the full server path works! Thank you haha I am satisfied now. I am curious now as to why the server path worked but not the website URL. I will have to poke around but thanks again! – Pixelknight1398"
As I stated in comments:
Use a full server path.
I.e.:
/var/usr/public/the_folder_in_question
You may have to add a trailing slash at the end.
"I am curious now as to why the server path worked but not the website URL."
As per what the manual states:
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
http://php.net/manual/en/function.file-exists.php
You will need to use CURL instead, if that is available for you to use.
You have to use the full server path due to $check_dir checks through the whole servers directory, and not just public forward.
In a zend framework application I want to validate a path to an image file. The file may be a previously uploaded file that reside in a folder inside the server machine or accessed via an url. I want to check weather the file path is valid or not. I don't want to use zend file element and upload the file. I have searched many times and was not successful. Could anyone tell me how to do that?
You are probably looking for the PHP file_exists function.
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
You could probably build a validator around that, or just use the function directly.
For files on remote servers you will probably need to use something like file_get_contents which also returns false if the file doesn't exist.
The function excerpt below fails, but when I copy and past the URL in the error message into the browser the file displays just file. I've also confirmed the file is uploaded in the correct folder. This is part of a class in a library in Codeigniter 2.1.0.
What is really strange is while !file_exists fail, file_get_contents works fine. Thanks in advance for any help on this.
public function output() {
if (!file_exists($this->file)) {
return "Error loading template file ($this->file).<br />";
}
$output = file_get_contents($this->file);
file_exists accesses the file over the local filesystem, while the browser uses http (and a webserver routes the request to the filesystem).
Just because one works it doesn't mean the other should work too.
Make sure the file exists by using eg. ls on the console.
file_exists use local path, while file_get_contents can use both local path and URL. So if $this->file is an URL, it's normal that file_exists fails but file_get_contents works.
I have to check using the file_exists function...
But, if I use something like that
if (file_exists('http://horabola.com/imagens/dt_2845.jpg')) {
//code
}
it doesn't work...
I know and I'm sure that the file "dt_2845.jpg" exists in the folder "imagens" ....
now, how do I check that? How do I get the server's file path?
Try:
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/imagens/dt_2845.jpg')) {
//code
}
Good luck
The server's filesystem path for url / is stored in the $_SERVER['DOCUMENT_ROOT'] predefined variable.
The current script's path is always stored in the __FILE__ constant. You may want to use
dirname(__FILE__);
to know the current script's filesystem path.
What you tried by calling:
file_exists('http://horabola.com/imagens/dt_2845.jpg');
which is open files via HTTP, needs the use of fopen() instead of file_exists() (thanks #Gordon for pointing this out), and you need, on your PHP server, the so-called url wrapper for fopen(). Anyway, using the HTTP protocol to open files on the same server as the runnning script is a bit of a performance waste (using HTTP, that is network, instead of hard disk, that is 10x to 1000x faster).
For our application we need to store files above the root so they can be accessed by the streaming media software.
I successfully created a symlink from:
/var/www/vhosts/myhost.com.au/httpdocs/fileserver/videostream/
to:
/usr/local/MediaServer/content/
and PHP will happily process my raw video following that symlink to the real file above the root.
However, if I try
file_exists('/var/www/vhosts/myhost.com.au/httpdocs/fileserver/videostream/myfile.mp4')
I run into all sort of "open_basedir restriction in effect" errors.
Is there a way around this? or do I need to just assume that if my database entries are correct and say the file was processed, that it actually was.
Would trying fopen work any better or is there still the basedir restrictions?
We are on a dedicated host with root permission so we can do whatever is needed.
Thanks.
Found a workaround, but it doesn't solve the question :-)
I can actually use CURL to pull in the headers of the actual file and this lets me know whether it exists or not. Plus an additional method is to check the existence of the Streaming Media servers custom URL for that file using CURL. Problem solved, but not quite how I wanted it to be.
You should look into is_link instead of file_exists
If you just want to check if the link exists (and not that the link target exists to which you have no access anyway because of the basedir restrictions) you could use the readlink function instead of file_exists. If it returns a string then the link exists, if it returns false then the link most likely does not exist.
if (#readlink($filename) !== false)
{
echo "Yay!";
}
In theory the basedir restrictions shouldn't trigger here, but I don't know for sure.