Browsing the files on a webserver with php - 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

Related

PHP - No such file or directory (LAMP server)

I've been on this bug for too long so I'm looking for some help from more experienced server programmers.
I'm running a Bitnami LAMP stack. I'm trying to use a PHP script (maintenance.php) to move files on the server. So I'm using rename(filepath, newpath) in my script. However, the PHP script cannot find the file even though it exists on the server.
SOLVED: The problem was that I was calling the script from two different places during debug (my browser, and my linux shell). The "working directory" in each of these places was different so the filepaths represented different locations depending on where I called the PHP script from.
There is no pictures directory at root so /pictures wont work.
Instead you will have to use absolute path to the pictures directory.
There are multiple ways to do this.
You can use $_SERVER['DOCUMENT_ROOT'] which will give you root directory under which the current script is executing.
reserved.variables.server
Also, you can use __FILE__ which will give you the path to current file.
With dirname(__FILE__) you can get directory and then you can work up to the pictures directory by going level down using ../.
This is also a similar question that you can check,
PHP include absolute path

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.

exec zip root path

Im having some problems zipping a directory.
The following line will do the trick but it also includes the root directory.
exec('zip -r '.$tmp_zip.' '.$filename_no_ext.'/rss-ticker/*');
So I only want to zip everything in the dir rss-ticker
How to fix this?
Thanks for your help
What zip program are you using? tar works in the way that you want it to above, then you can gzip it.
Alternatively, chdir() to the directory you want to zip, and specify your path as * - that should only get the files in the current working directory.
If you can't get it to work in the way you want to (or even if you can) try the ZIP Extension or this 3rd party library - doing it in pure PHP will make your code more portable.
Unless you have an overwhelming reason not to (like a desire to cause yourself much pain and headache), you really should be using the ZipArchive toolkit. There is an example of how at this question.
As to getting zip to work with exec, I noticed two points:
you will definitely need the $filename_no_ext variable in there. If you just have it be a .. Right now, because you're starting rss-ticker with /, it is assuming that the folder is at the root of the file-system. I don't think you want that.
You do not need the trailing * to get the command line zip function to work.

Deleteing files in the root dir of my dedicated server with PHP

I have a apache dedicated server with lost of websites.
I also have a red5 installation on the server.
What I want to know how to do is perform file functions - specifically unlink() - on files held in the RED5 directory within the root server dir.
I can move files with this:
copy ("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename, $strDestination);
but
unlink("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename);
...won't work...
Any ideas what I'm doing wrong?
Thanks.
You need to check the permission of the file that you are trying to delete. Apache should (hopefully) not be running as root and therefore cannot delete any files that it does not have permissions for.
You should also be vary wary of security. Allowing an unchecked variable to be used in the end of a copy() or unlink() call could potentially give a user access to your entire filesystem. Take a look at basename.
I suspect you need to use absolute file paths rather than urls/relative paths. Also if you want to delete from root dir, you need to specify that too. Try doing something like below:
unlink($_SERVER['DICUMENT_ROOT'] . '/RED5/' . $yourfiles);
You are addressing the files via HTTP, which you can't use to delete files.
You need to specify a filesystem path such as /etc/httpd/sitename/file.php

PHP fopen filename. Is it relative or absolute?

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

Categories