How do I get a file's path in PHP? - php

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

Related

How do I solve this php is_dir code?

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.

Access to client's localhost from server

Here is the context : I need to load heavy files in the Google Earth plugin on a website. I have a php script that build up a path to those files and send it to javascript through ajax. Then, the plugin download the file and build it on earth. This take a long time. I need to speed that up for a public event. The computer can run a web server and have these files on his hard drive.
So here is my question : Is there a way for php to check if the client is running a local server and check if it contains a specific file ? Or at least, to execute a php script from this local server ?
Something like :
if(is_file('localhost/files/heavy.kmz'))
$path = 'localhost/files/heavy.kmz';
else
$path = 'www.randomsite.com/files/heavy.kmz';
return $path
Of course, this localhost refers to the wrong server :(.
I guess that if it was possible, there would be security issues. But i ask anyway.
I'm not very used to stackoverflow habits, I hope a didn't do anything wrong.
Kororo.
Edit : I will try to clarify a bit.
I know the path of the files on localhost. I need to check if it exist or not in order to send a path or another. If i can find it, i don't have to download it from the server.
The local webserver is only needed on machine to allow php check of the file.
If the data file is on the same machine, and you can tell where your data file should be relative to the script you are using to check it's existence, just use the __DIR__ (or dirname(__FILE__) if your php is ancient) to create the path.
So for example, you have have the directory structure like this:
files/
heavy.kmz
check.php
...
Then inside the check.php the following work as expected:
if (is_file(__DIR__.'/files/heavy.kmz')) {
$path = __DIR__.'/files/heavy.kmz';
} else {
$path = 'www.randomsite.com/files/heavy.kmz';
}

function file_exists not working in php

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

PHP ftp_put and remote paths

Does any of you know what might cause ftp_put to fail when passing a full remote path rather than filename only?
I am asking because according to http://www.php.net/manual/en/function.ftp-put.php the second parameter is The remote file path but someone apparently had a problem: http://www.php.net/manual/en/function.ftp-put.php#89986
I would like to avoid having to ftp_chdir to all targets because it costs too much time.

PHP rename file on same host

$url = 'http://site.com/images/name.jpg';
Its a full path for the file, which is already exists on ftp.
How can I change file's name?
As others already point out, the PHP function you're looking for is rename, but you can't rename a file through a http:// URL (At least not in PHP - As #Artefacto says, WebDAV can do this.).
You will need to specify a proper filesystem path.
if your script on the server root (http://site.com/script.php) that script will do:
rename('images/oldname.jpg', 'images/newname.jpg');
since that's the relative path of the image from the script point of view.
Check out the rename() function. Seems to be what you're looking for here.
http://php.net/manual/en/function.rename.php

Categories