PHP fopen filename. Is it relative or absolute? - php

I'm having problems with my paths and fopen with reference to my web server.
I have a file saved in public/dan/new/apps/lovescopes/thisfile.php.
thisfile.php will create a new file in public/internalServer/lovescopes/xml/2009/12 using fopen "x+".
Now my errors show in the line where fopen is:
If I type in the path as relative like ../../../../internalServer/lovescopes/xml/2009/12 I end up with a Permission Denied error.
If I type an absolute path like /public/internalServer/lovescopes/xml/2009/12 I end up with "Failed to open Stream: No such File or Directory"
I'm still confused if I should use relative or absolute paths. I have a ftp_nlist and it worked perfectly well with #2. Is Fopen the same?
Also with the different error messages which I believe points to the same path, I don't know which way am I doing it right 1 or 2?

Why not use use realpath() with your relative paths.
Also permission denied indicates that the folder you are trying access to is not readable and/or writable by the webserver or whatever user your PHP process runs under.
In addition, if you want to read/write files in one go from/to a variable, you might want to use file_get_contents() and file_put_contents().

The PHP function itself is neither absolute nor relative, that depends on the path.
Both the errors you're getting are related to that specific path, either permissions or existence, so I'd recommend getting a path you know exists and you have access to and trying that (the current directory may be a good place). You may also temporary chmod the file and path, just to make sure you have read access. Print the results and any errors.
fopen("public/dan/new/apps/lovescopes/test.php");
fopen("./test.php");
If neither of those return errors, you'll know it was just a path issue.
Also, your PHP process may have different permissions than your user, so watch out for that.

Also, make sure that you're relative path is specified relative to the file where the function is being called (not necessarily where the actual code is).
In PHP use getcwd()(get current working directory; manual) to figure out which directory to base your relative path off of.

You have to make sure your script has permission to access that file

Related

PHP file_exists returns false but the file does exist

I have seen several similar questions, but no answer worked in my situation, except that it probably has something to do with permissions.
A PHP script served by Apache tells me unable to open database file.
When I print the path to that file, it returns a valid path, say DBPATH. The file does exist at that location; I gave it and its parent folder 777 rights; I gave them user:user access, where user is the sudoer that all script files belong to. I did the same to the whole htdocs/ folder, just in case.
When I print file_exists(DBPATH), it returns false. Is is most likely a matter of permissions, but I don't know what I should change for PHP to have access rights. I tried apache:apache, too. I cannot su apache (user not available).
My scripts are in htdocs/. DBFILE is somewhere out of it (I tried /tmp/test, all in 777, but no luck either).
No safe_mode, PHP 5.4 freshly installed, CentOS7.
Please someone give me a clue at least to help debug it.
Maybe such as: how can I check whether my file will be readable from apache/my php script, without running the script itself? How can I get the name of the user that is used to execute it?
Solved, more or less.
To debug I had the idea to move DBFILE to the same folder where the PHP script lives, and check it can find it - it did. Then I move DBFILE one folder after another in the tree to see where it stopped finding it.
It occurs that if only one of the folders in the whole path does not have execute rights for all users (xx5), the file cannot be found and file_exists returns false.
So the solution was to create another folder in a totally executable place (/var/www/data/ worked after chmod 755 data), and move the file there.
Do you use an absolute path or relative path?
Because file_exists() doesn't work with HTTP addresses (which is an absolute path). But you can enter the relative path.
I had the same problem and it fixed it. It was the same problem with unlink().
Exemple:
$file_relative_path = "./wp-content/uploads/fileDirectory/fileName.jpg";
if (file_exists($file_relative_path)) {
unlink($file_relative_path);
}
I had a similar problem and was able to solve it by the answer of JulienD:
If the execute flag of a directory in the file system (Linux) is not set, then PHP (still) scans this directory with glob or scandir. However, a subsequent check with file_exists() on this list of results, I wanted to find broken symbolic links, returned false!
So the solution was to set the Execute right for the directory, as mentioned by JulienD.

PHP is_writable error returns not writable

I have this code
if(is_writable($destination)){
print "destination is writable";
}
else {
print "destination is not writable";
}
$destination is a relative path like ../subdir/dir/currectDir/ and I have checked and the permission is 777 but this code above returns "destination is not writable" and I don't understand why. Can someone help me? Thanks
There are many reasons why a directory may not be writable, aside from permissions. Some of them are:
The directory is outside of the paths which PHP is able to write to because of open_basedir restrictions
The directory is on a drive which is mounted as read-only
The directory has immutable or appendonly attributes set
The filesystem is corrupt
However, my guess in this case would be that the path you are using is not correct. Try running
echo getcwd();
Just before your is_writable() call.
It will tell you the current directory the PHP script is running in, and from there you should be able to work out the correct relative path. Not that if running a PHP script from the command line its path will be that of the current directory, whereas running via a web server such as Apache it will usually be the public_html or httpdocs directory. It may have also been changed earlier on in the script execution.
You can also specify an absolute path (e.g. /home/youruser/public_html/subdir/dir/currectDir)
I can't comment, so I have to respond here but it looks as though the path (you posted a relative path, not an absolute path by the way) is incorrect - have you tried
var_dump(file_exists($destination));
first, to determine if you are looking in the right place for your directory?

Scandir fails to open directory

$contentdirectory = '/dead-wave/dead-wave_content';
$contentlaunch = scandir($contentdirectory);
that's what I'm using to create an array from which I echo it's values using a for each statement. this works perfectly on my dedicated server, but once hosted on godaddy servers returns an error message 'failed to open dir: No such file or directory in...' now the directory path is certainly correct the actual problem is unknown to me. Any Thoughts?
Are you sure the path is correct? If the path is a subdirectory of your current directory, you should use 'dead-wave/dead-wave_content' instead of '/dead-wave/dead-wave_content'.
are you sure the path is correct? on a hosting you are usually rooted to a different directory (like /home/user1).
So the Path from above would be /home/user1/dead-wave/dead-wave_content/
you can do a
exec('pwd',$return);
print_r($return);
to find out where you actually are.
I'm not sure this is the same problem you're having but sometimes I can't use any file i/o functions when I'm running unit tests on top of Zend Server via Zend Studio even though they worked on Apache.
I think you need to rewrite you paths somehow making them relative to the server web directory constant. I'm not sure what that is but I'm sure there is one.
EDIT Oh! I don't think Apache recognizes '/toplevel/secondlevel' path writing style.

PHP: is_readable not working for relative path, works for absolute

I'm moving a site from one server to another. On the old server, my code calls is_readable("filename") and it works. On the new server, it does not work. The files are exactly the same and "filename" is in the same place relative to the calling page.
When I put in an absolute path instead, is_readable returns true as expected. Any suggestions about what the problem could be?
safe_mode is off and open_basedir is not set in my php.ini. I also modified the file permissions, it doesn't work even if I chmod 777 (but that shouldn't matter since it reads properly when using the absolute path).
The servers probably have different configurations causing the current working directory (CWD) not to be the one where the script is being read. Relative paths are always relative to the CWD, not the current executing script.
You can check the CWD by calling getcwd() or by using realpath() to resolve the relative path into an absolute one. If the value is incorrect, you will have to either configure the server properly, or set the CWD manually by doing the following:
chdir(dirname(__FILE__));
Try using realpath on you relative path to check if it points to the right file.
You could also use getcwd to check if you're in the correct directory.
Typically, if you pass a relative path to is_readable (or file_exists), it will return false unless the path happens to be relative to the current PHP direct -- View PHP chdir info

Browsing the files on a webserver with php

Is there a way to browse and the get the path to files that are on a web server using php...? Possibly any jquery plugin to accomplish this....? Or else how can i do this using only php...??
Edit : I've found some scripts that run as stand alone on the webserver and let you browse the file structure.. But i need to know the method with which it is done so that i can implement it in my own script....
Thanks a lot for your suggestions....
To explore the server's filesystem you'll need the directory functions:
chdir — Change directory
chroot — Change the root directory
dir — Return an instance of the Directory class
closedir — Close directory handle
getcwd — Gets the current working directory
opendir — Open directory handle
readdir — Read entry from directory handle
rewinddir — Rewind directory handle
scandir — List files and directories inside the specified path
But if you're looking for some code to read through, that does what it seems you're looking for, check out filebrowser. No longer in active development, if I remember correctly it's pretty simple code at the core.
I suggest that you have a look at the code for CKEditor's file manager.
Here's the PHP filesystem reference

Categories