Jump to content

Folders: Stopping people doing ../ too many times?


Recommended Posts

Hi, I'm writing a file management script and use the following to define the root directory:

 

$rootDir = 'c:\htdocs\filemanager\uploads';

 

 

The 'uploads' folder has several sub-folders in it and I want the user to be able to navigate through them and get directory listings.

 

To view a folder contents the user can go to:

http://url/filemanager/index.php?page=displayFolder&folderName=/foldername

 

However my problem is that if people change the folderName to '../' or something like that then it will display things that are above the root directory that I  don't want them to see. Is there a way that I can stop them going above the root directory by manipulating the folderName variable?

 

I still need them to be able to navigate up if they are in the subfolders.

 

Thanks in advance,

 

John

 

 

Assuming no folders end with dots, you can simply use...

 

if( strpos('./', $folder) !== FALSE )
die( 'Attempt to go above root folder' );

 

You could also use realpath()

http://php.net/realpath

and verify the 'root' path is still there.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.