Filesystem, below web root
Moderator: General Moderators
Filesystem, below web root
In a web poll on top most programmers suggest to store Filesystem, below web root that means where it is not accessed by the web.
Now how do i store images below the webroot and and how to retrieve them for displaying? Please tell me the logic and sample code too if possible.
Now how do i store images below the webroot and and how to retrieve them for displaying? Please tell me the logic and sample code too if possible.
I would recommend doing that as a layer of security, but not for EVERYTHING.
Anyways, just put the images outside of your document root? /home/user/images, instead of /home/user/public_html/images.
You would then need to write a script to output the image, making it accessable through something like script.php?image=someimage.jpg.
This script would need to get the contents of the image, then supply the appropriate header information.
Anyways, just put the images outside of your document root? /home/user/images, instead of /home/user/public_html/images.
You would then need to write a script to output the image, making it accessable through something like script.php?image=someimage.jpg.
This script would need to get the contents of the image, then supply the appropriate header information.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
OK, that means i will have to store the images manually in /home/user/images ... is there anyway i can store using upload or something... much more convinient and any is idea how to get the contents of the image, then supply the appropriate header information.scottayy wrote: You would then need to write a script to output the image, making it accessable through something like script.php?image=someimage.jpg.
This script would need to get the contents of the image, then supply the appropriate header information.
Any code to do it or some logic or tutorial?
A sample script would be like this:
EDIT| Yes, you can upload them. When you go to use move_uploaded_file() just move it to /home/user/images.
Code: Select all
<?php
//get the image name or id or something.. probably needs some
//security checks
$image = $_GET['image'];
//check if it exists
if (!file_exists('home/user/images/' . $image))
{
die('Image ' . $image . ' does not exist.');
}
//send the content type header
header('Content-type: image/jpeg');
//send the contents of the image
echo file_get_contents('home/user/images/' . $image);Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
that means if i am uploading the code you suggested in /home/user/public_html then it would access /home/user/images which cannot be accessed otherwise . Correct ? And why did you say it is not suggested for everything in the previous reply any drawbacks of this system?scottayy wrote:A sample script would be like this:
.. indicates going up a directory, which is what you need to do if you're in the document root.Code: Select all
<?php //get the image name or id or something.. probably needs some //security checks $image = $_GET['image']; //check if it exists if (!file_exists('../images/' . $image)) { die('Image ' . $image . ' does not exist.'); } //send the content type header header('Content-type: image/jpeg'); //send the contents of the image echo file_get_contents('../images/' . $image);
It cannot be accessed through a web browser.
Anything that you access through your browser should be in the document root (unless, like in this case, you want it to be protected a little more). If you put everything below the document root, that'd include your php and html files... and just wouldn't make much sense.
Anything that you access through your browser should be in the document root (unless, like in this case, you want it to be protected a little more). If you put everything below the document root, that'd include your php and html files... and just wouldn't make much sense.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Yes. The first line of the script is $image = $_GET['image']; 
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Ok thanks.. I got another idea to store all the images in /home/user/public_html/images as usualscottayy wrote:Yes. The first line of the script is $image = $_GET['image'];
and then run a php code which takes each and every file in the directory image and store it's reference url, time, date, filename information in the database (using auto incrementing the id in database).
So in this case how would i write the php code which automatically insert each images associated refernce url like mysite.com/images/1.jpg in the database. I think this would be a better solution?
I don't know what would be a better solution.. because I don't know what the problem is. You just asked me how to do something and I showed you. 
What is the problem or the goal? Maybe we can come up with the best solution for it.
What is the problem or the goal? Maybe we can come up with the best solution for it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
The aim is to store around a 1000 images and maybe ppt and videos using a better effective method which doesn't take too much server resources AND to have some sort of number of views and rating associated with each file like we have with articles etc...scottayy wrote: What is the problem or the goal? Maybe we can come up with the best solution for it.
Can't figure out how to store below document root or above document root or in database or use combination or what..
You don't need to store below document root for that.
Just upload them, and store the path to the file in the database. Have a field for views, rating, etc. Then on the script that displays the image or video, mysql_query() to update the database.
Just upload them, and store the path to the file in the database. Have a field for views, rating, etc. Then on the script that displays the image or video, mysql_query() to update the database.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Any suggestions how to store the path of the image/video in database. Suppose i upload in dir /home/user/public_html/image an image123.jpg using normal upload method what to do next and how?scottayy wrote:You don't need to store below document root for that.
Just upload them, and store the path to the file in the database. Have a field for views, rating, etc. Then on the script that displays the image or video, mysql_query() to update the database.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
my first instinct would be to store them to
then you only need to store the md5 in the DB, as well as info like the suggested filename. I would also store the mime type of the file in the DB as well, because that would be useful to sort/group by.
Also, by using the md5() of the file contents you can avoid collisions as well as save filesystem space on duplicate uploads, even by different users!
Maybe write a simple File class that stores / retrieves the files. The possibilities are endless.
Code: Select all
$_SERVER['DOCUMENT_ROOT'].'/../files/'+md5(file_get_contents($the_file))Also, by using the md5() of the file contents you can avoid collisions as well as save filesystem space on duplicate uploads, even by different users!
Maybe write a simple File class that stores / retrieves the files. The possibilities are endless.