开发者

Which type of path to store in MySQL database?

When I come to build a simple site using PHP and MySQL, what 开发者_如何转开发is the best method for storing paths of files and images?

  • Inserting paths to Mysql as (system path C:/----)
  • Inserting paths to Mysql as (HTTP path http://----)


Always save file paths relative to your web sites directory. Consider your website's address is "foo.com" and you want to store images in "foo.com/images". It better to store images address without "http" and without "foo.com".

Therefore just save the filename and its subfolder if exists.


Neither, the best is to store just the filename and leave your application to determine where to locate this file. You can determine it accordingly to your database table. For example, let's say we have an user table which have an avatar field. You can store just the filename (foo.jpg), and then determine the path in your application (c:\xampp\htdocs\project1\img\users\foo.jpg). Or even better, you can leave this field and store nothing about the image, then you can check if an image exists with the id of the user, like 1.jpg, 2.jpg etc.

But, if you really want to store absolute paths, start storing from your project folder. For example, lets say you have c:\xampp\htdocs\my_project\module1 Then, start from /module1

If you need to store a external URL you can save the entire like http://www.foo.com/test


I propose you save the relative path, not the absolute one. I.e, if you have this structure:

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

Store "file.jpg" as "files/file.jpg". Thenn save "c:/install_folder/" in a configuration value somewhere in your application. That way you can easily move the application around and just update the configuration file.


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

Including the path whether, absolute, virtual or the web address will add an overhead of the file path data to each entry in the image field column.

If you must store this information in your database why not store it as a foreign key to a paths table.


It depends on how you use your data. When you want as example edit your file the best solution is to use the system path. And when you want, as example link to another page you can use the HTTP path

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜