Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Rasbin Rijal
PLUS
Rasbin Rijal
Courses Plus Student 10,864 Points

How to restrict direct url access to files which are inside a folder

Hi All,

I need help about restricting files. I'm using wamp and Inside my www folder I have a folder named MYFILES where I have some files. These files are directly accessible through URL if someone types the exact address like localhost/rasbinupdate/myfiles/image1.jpg . I want to restrict direct URL access of the files inside 'myfiles' folder.

How should I do this? Any suggestions?

NOTE : I have restricted the direct url access to php files by keeping checking through isset and some session variable like username inside the php files at the beginning. For example : localhost/rasbinupdate/downloads.php won't be accessed if the user tries to access through direct URL without logging in.

But now, the restriction should be done for files like .jpg, .pdf which are inside a folder, so I don't know how to do it.

Any help is appreciated. Thanks in advance

1 Answer

Tomas Hartl
Tomas Hartl
5,124 Points

If your server is Apache you can use the .htaccess file. Cleaning URLs with Rewrite Rules covers it well enough for your purposes. It is part of Enhancing a Simple PHP Application and should cover what you need, perhaps with a bit of additional regex research.