How do I solve this php is_dir code? - php

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.

Related

file_put_contents could not store image on server

$path = 'http://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/372096_100002534902767_1927052265_n.jpg';
$info = file_put_contents('new/angel.jpg', file_get_contents(urldecode($path)));
echo $info;
It works fine on localhost but it did not work on my website.
Any idea what the problem might be?
Check your logs for error messages
Does the folder ("new") exist?
Are permissions set to allow writing by scripts?
Are you sure the error is in file_put_contents? file_get_contents could fail if the host has disallowed url_fopen.
unless $path is actually hardcoded, you will probably introduce an arbitrary file disclosure security issue. Make sure you validate your input.
you need to give a permission (write ) to upload folder
you can do that by the FTP program by using this codes in numeric value :777
Make sure that there are write permissions to the file in which you are writing
I think the relative path is the problem. It can possible that your local has the path to that file but the path does not exists on server.
3.Please put a forward slash in line $info=file_put_contents('new/angel.jpg', file_get_contents(urldecode($path))); before your "new" folder and try i think this might be a problem

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

How do I get a file's path in 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).

file_exists with symlink-ed files?

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.

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