Laravel Image Gallery Logic

Recently, I began to develop a rather huge site. On the site, I would like to allow users to upload their samples. We are currently quite limited, so the images will be stored on our server.

I am a little fixated on logic. So my logic would be like that.

The user creates a folder with a name that is stored in the database with the users id attached to it

folder table

Ranks

 id | folder | user_id 1 | Some folder | 1 2 | New folder | 4 3 | Nother folder | 7 

Image table

Ranks

 id | image_name | folder_id | 1 | image1.jpg | 1 2 | image2.jpg | 1 3 | image3.jpg | 1 4 | image4.jpg | 2 5 | image5.jpg | 2 6 | image6.jpg | 2 

Relationship

 class Folder extends Eloquent { public function images() { return static::has_many('Images'); } } class Image extends Eloquent { public function folder() { return static::belongs_to('Folder'); } } 
Folder structure

on server

 - samples -user_id - folder_id - image1 - image2 - image3 

so that you can see, the user creates a folder after creating the folder, user loads the image name into the database using the folders id , and the image will be displayed as described above with the implementation.

So, my questions.

  • This is good logic, in your opinion.
  • This may lead to problems in the future.
  • what do you offer for this feature.

And the fact that I am the most sacred is 2 things.

I think this will lead to a huge database, secondly, id's , after x time, when there will be more users, id's will increase, and I know that it will sound strange, but since many users will upload images, it will lead to to the huge identifiers that I mean, maybe it will reach millions, is there a way to solve this problem?

thanks for the help

+8
php image image-processing laravel
source share
2 answers

Ok - allows you to break it into several sub-answers;

Question:

 - Is this a good logic in your opinion - Can this lead problems in the future - What would you offer for this functionality 

Answer:

The logic seems sonic - but I'm curious where you will store the images? Inside public_html - or outside the web root? If you have images inside public_html - and allow the browser to access them directly, this will allow users to β€œguess” other user folders and access them. You need to store data safely.

To make images outside the website and make sure that only authorized users are available to them - you should use readfile () . Something like this will do the trick

 function user_file($file_name = "") { if ($file_name) { // Ensure no funny business names to prevent directory transversal etc. $file_name = str_replace ('..', '', $file_name); $file_name = str_replace ('/', '', $file_name); // now do the logic to check user is logged in if (Auth::check()) { // Serve file via readfile() - we hard code the user_ID - so they // can only get to their own images readfile('../your_app/samples/'.Auth::user()->id.'/'.$file); } } } 

Question:

I think this will lead to a huge database, secondly to id, after x time when there will be more users, the identifier will increase, and I know that it will sound strange, but since many users upload images, they lead to huge id, I mean that it may reach millions.

Answer:

According to the mySQL function pages :

We use MySQL Server with databases that contain 50 million records. We also know users who use a MySQL Server with 200,000 tables and about 5,000,000,000 rows.

So these are 5 billion lines. You might get to a few million. Thus, you are safe here (depending on your equipment).

Question:

... but as many users upload images, this will result in a huge id, which I mean, it will probably reach millions, is there any way to solve this problem?

Answer:

If you don't want to store millions of records and worry about performance, one option is to keep the folder table, but discard the image table. Instead, you can use scandir () in the folder and get PHP to get the file names from the directory itself. Then you do not have much overhead.

 <?php $list_of_user_files = scandir("$user_id/$folder_id"); foreach ($list_of_user_files as $file) { echo "File: $file <br>"; } ?> 
+19
source share

The method of saving the folder table and using the scandir function is the standard procedure. And let php extract the file names from the folder. If you have several files, try classifying them with the order of the year and month, as in wordpress. how

 2012 01 02 03 2013 01 02 03 

etc. inside the folder id. Thus, the total number of images in the folder will be relatively smaller.

+1
source share

All Articles