What type of path to store in a MySQL database?

When I come to the creation of a simple site using PHP and MySQL, what is the best way to store file and image paths?

  • Insert paths in Mysql as (system path C:/---- )
  • Insert paths in Mysql as (HTTP path http://---- )
+4
source share
5 answers

Always keep file paths relative to your website directory. Think of your website address as "foo.com" and you want to save the images in "foo.com/images". It is better to store the image address without "http" and without "foo.com".

Therefore, just save the file name and its subfolder if it exists.

+6
source

Neither one nor the other is best to store only the file name and leave your application to determine where to find this file. You can define it according to the database table. For example, let's say we have a user table in which there is an avatar field. You can save only the file name (foo.jpg) and then define the path in your application (c: \ xampp \ htdocs \ project1 \ img \ users \ foo.jpg). Or even better, you can leave this field and not store anything in the image, then you can check if the image with a user ID exists, for example 1.jpg, 2.jpg, etc.

But, if you really want to store absolute paths, start storing them from the project folder. For example, let's say you have c:\xampp\htdocs\my_project\module1 Then, start with /module1

If you need to save an external URL, you can save everything as http://www.foo.com/test

+3
source

I suggest you keep a relative path, not an absolute one. Ie if you have this structure:

 c:/install_folder/ c:/install_folder/script.php c:/install_folder/files/file.jpg 

Save "file.jpg" as "files / file.jpg". Then save "c: / install_folder /" in the configuration value somewhere in your application. Thus, you can easily move the application and just update the configuration file.

+1
source

Just save the image name and let your application handle directory creation.

The inclusion of the path, whether it is an absolute, virtual or web address, will add the service data of the path data to each record in the column of the image field.

If you need to store this information in your database, why not save it as a foreign key in the path table.

+1
source

It depends on how you use your data. If you want to edit the file as an example, the best solution would be to use the system path. And when you want, as an example of a link to another page, you can use the HTTP path

0
source

Source: https://habr.com/ru/post/1332888/


All Articles