How to set directory for images in my website? - php

My aim is to make a multi-user website which may have between 30 to 3000 users. once one uploads an avatar or an image, How should it handle directory naming?
1- all files in one folder?
2- all files in folders separated by day?
/img/upload/2013/05/14/brothers32_5464562.jpg
3- all files in folders separated by user name?
/img/upload/users/brothers32/5464562.jpg
I don't like change my way in mid-way.so, according to your experience which way do you suggest to choose?

I agreed with StarCub until I read each user could upload multiple images. Then I'd do this:
Profile picture. All in 1 folder (as StarCub suggested):
/img/upload/users/USERID.jpg
Uploaded pictures. All in another folder:
/img/upload/userimg/2013/PICID.jpg
EXCEPT if you expect users to upload many pictures. I'd say 1000-5000 pictures per folder is good, so maybe order them by month depending on what kind of site the page is.

If you only have to up 3000 users and each user only has one avatar, I would just put them all in one folder (OPTION 1) and name them use user ID or some sort of unique identifier for that user.
If each user can upload multiple images, then I would use OPTION 3.

Related

How to show images in a webpage from root directory in PHP Codeigniter?

I have made a site with Codeigniter and users have options to upload their photos there. As the photos are personal, I think keeping them on web directory is not safe. As a result, I kept them on a root directory like /var/www/images directory. Now, how I can get the images in a webpage?
Wrong Choice dear learner. you images folder needs to be accessible by your code and to the browser to render that image by the image path. For example
This is the image directory of my project. I am using all these images in different pages of my website. Some are selected dynamically by my code.
Now in your case by putting images in www folder, you are making a path of http://images/ which is not recommended by any PHP guidelines. Your images folder should be in your project root not outside.
If you want to disable folders to be viewed, just add empty index.html file, but if you try to protect images from being viewed then your website wont be able to find them too image protection in codeigniter
The code will be as safe as you make it. Well what you are doing is fine, the web server will still have to access the images.
While ignoring the rest of your code or what yo are trying to achieve, you could try referencing the images fro your webroot or probably create a symlink.
Also, I would try to put the images in another place like AWS.
If security is a concern for you, you can try building a ImagesController that checks if image exists, if user has permissions to see it and after that render it.
Uploaded images should also have some reference row inside a table in your database that can have a column which specifies if it should be public (also try to sort them to folders depending on what it represents - user profile pics, product images, etc).
I know it's a little bit of work but this would be the way I would do it.
You can check online for examples of controllers that return an image.
Good luck on your project.

Store images or folder path in database: how?

My plan is to write a few news articles each day to display on my website.
Each news article will contain a certain amount of pictures. There can be up to 50 images in an article.
At this moment my images get stored like this:
/images/2014/theme1/01.jpg
/images/2014/theme1/02.jpg
/images/2014/theme2/01.jpg
/images/2014/theme2/02.jpg
...
/images/2014/theme2/50.jpg
I'd like to know how I can store these images into my database. I already read a lot about this subject and heard that storing the folder path into the database is better than the images itself.
But how should I do this? The maximum amount of pictures per article is 50, so do I really have to create 50 columns (one for each possible image) or is there another way to do is?
#Luca Prodan Flow this Link. .Net core Add folder in static folder (www) and save image file and store the: "PathWithFolderName + "/ImageName.png" in database.

How to protect a public image upload folder from manipulation/direct access

I've found several answers to this question, but each case was different from mine. Before I spend hours implementing what I think will work, I'd like to get an opinion or two. Who knows, there may be an easier solution that will benefit someone else too!
I'm in the process of creating a website which provides a photo modification service. Each of my customers will be uploading their photos using a modified version of a jQuery/PHP upload script I found on Github (BlueImp's File Upload script- props BlueImp!) When each image is done uploading, a thumbnail of the image is displayed next to the image's filename.
The upload page creates a folder based on their Order #, and a thumbnail subfolder where the images are stored. For example, for Order # 12345:
/uploads/12345 <- Folder where the uploaded images will go
/uploads/12345/thumbs <- Folder where thumbnails will be served from
It's important that clients don't access other clients' photos. I moved the uploads folder outside of my website root, but then the thumbnails weren't displaying when the images finished uploading because, well, they weren't in the website root so the links weren't working.
Here's a solution I thought of:
Separate the folders. Move the main upload folder outside of the document root, but leave the thumbnail folder inside the doc root, so the thumbnails can be served up when each upload completes. When the user has finished uploading their images, delete the thumbnail folder corresponding to that Order #.
Is there a better way to do it? My concern is preventing access to photos (A client will only have to upload them once, then wait for us to finish the modifications. They will NOT have to log back in to download them.) I don't want someone to be able to access the upload folder via the address bar or anywhere else. Can I use an .htaccess file to restrict access, but still allow for linking to the thumbnail images via the upload script?
I apologize for the wordy question. I tried to be as succinct as my limited knowledge of programming would allow. Thank you in advance for your time and effort in helping with this.
two way around
Write a blank index.html on each directory, which will prohibit to access files.
Write below in .htaccess ( if not exist then create this file )
Options -Indexes
OR If you want NO ENTRY outside of the LAN
# no nasty crackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24
# this would do the same thing..
#allow from 192.168.0.0 to 192.168.0.24
Reference
One more
You want to show the thumbs to each owner customers but not to others?
knowing the Order number (is sequenziell) you can easily try some links and see some photos.
I suggest to use a .htaccess to password the upload folder and to generate a hash number for every thumb that you have to attribute to the order number.
A second solution: password with a .htaccess the upload folder and generate a thumb when you generate the order confirmation page without saving the thumb. (he has to see it only once as you mention).
have fun!
Two things that you should do are:
Disable directory indexing.
Always keep the file names random. Map them using the database. i.e file1.jpg->file_af324234324324ff . You can also store the thumbnail file information in the same table.

How to store multile images full path into a database so that I can display them in a user home page like Orkut

I am making an application in which every user has to sign in first and then he can access his home page. Now on the home page, I have given an option of uploading an image. Now if the user is uploading one image I am storing the full path of the uploaded image into a database and from there I can display the image easily by an img tag...
But what should I do when the user want to upload many images? Then how should I store their full paths in a database for the same user. Give me an idea just like Orkut or Facebook. Should I make a different table with named images and should I store images in different rows with the same username. What should I do?
I don't know the logic. What should I do? How can I upload many images and how can I store their path and what will be the wisest method and how do I display many images on one page (I can display one)?
You can make a folder, named after user name and id and put all their images there.
To display many images on one page, just add more <img> tags to the page.
This seems to be more of a design question than a PHP question. I would create a separate table to store all paths, this is more normalized.
You still need to handle the UI, but if you are doing a sort of gallery then that is fairly simple with some jQuery sideshows or something like that.
Yes, you want to use a separate table to store the image paths. You'll most likely want a record ID, the User ID, and the path to the image. You could also add a field to contain the sorting order for the images.
Having the sorting order field will allow you to page through the photos if there is more than one page of photos.
Your thinking is correct where you suggest creating a separate table with rows containing the image path and the username. The concept that you are dealing with is called cardinality. I'd recommend that you take a few minutes to read about this concept, since it is so important to database design.
In this case, you're talking about a one-to-many relationship between the user and the images.

Storing pictures per user on website (php)

Users of the website need to able to store images in their "area" , should I store these in the database directly or create a directory for each user.
Or should I just have a single directory for all images and in the database store a list of images that each user owns?
I'm looking for the most effecient way, which I think is a folder for each user?
If you have many pictures per user like avatar and gallery of photo created by the user then use separate folders named with the user id or a hash of the user id.
If you only store avatars have one big folder where the name of the image is the user id or hash of the user id.
It depends on how many pictures there are.
If there is only one picture per user, then perhaps all in the same directory.
However if you have albums and such, there can be millions of photos with all the users photos in the same folder. This would be very slow to search in. Then I'd go with one folder per user.
Also, never give direct access to the folders. Use your database to link to the files.
Better use folder or a cache folder. You can also use resizing functions for the image, so if user upload a large scale picture. Your script will manage to create different sizes, like avatar pics. And cache those images. So thumbnail or avatar viewers only viewing the cache image of the original image. And the large image is usable for future work, like a bigger preview of the avatar.

Categories