Home  >  Q&A  >  body text

php - 数据库存图片,是存图片名称?还是存图片路径??

数据库存图片,是存图片名称?还是存图片路径??

问题如上,之所以会有这么个疑问,目前图片是在本地服务器上存的,但是以后若是业务规模扩大,那图片肯定是要迁移到云存储服务上的,那个时候图片路径会改动,我怕原有网站图片显示,全部挂掉...。

不知道,大家存图片到数据库的时候,是存图片名称(test.jpg) 还是 存图片路径(一般都是 /Data/UploadImage/2017-04-14/test.jpg)??

存图片名称的话,以后,路径改动,直接修改图片路径的代码就解决了。

存图片路径的话,以后,路径改动,要更新数据库中所有图片的路径才能够解决。

不知道你们在上传图片后,保存图片的时候,是怎样保存的呢??

大家讲道理大家讲道理2741 days ago1933

reply all(6)I'll reply

  • 黄舟

    黄舟2017-04-17 16:54:36

    I’m used to it
    The path, name, and suffix are stored separately, and then there is a field for the complete URL, which is to connect all the above

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:54:36

    Judging from your question, since the relative path of the image is stored in the database at the beginning, if you want to store the third-party image service in the future, you can just write a script to replace it with the third-party image address in batches. If you want to If you get it right in one step, you can now use third-party image services

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:54:36

    This usually stores the image path in its own database. If there are too many images, how much space will it take up? Real pictures can be stored on third-party platforms, such as Qiniu, etc. The implementation idea is: first upload to Qiniu Cloud Server, obtain the stored path strength, and then save it to your own database.

    reply
    0
  • 阿神

    阿神2017-04-17 16:54:36

    The current common practice for saving images is to upload image resources to the image server uniformly, and then save the image path. If the amount of data is very large, it can be stored in separate tables based on the Hash value. If used in a project, all images will be loaded using images.xxx.com/path.png. In addition, if you do not use an image server and use a dedicated domain name, you can create a resource folder in the root directory of the website and store them all here. Use "/" in all places where paths are used, and start from the root directory to facilitate migration. , and there will be no path problems.

    reply
    0
  • 阿神

    阿神2017-04-17 16:54:36

    If you save your own server, the relative path of the data inventory! It doesn’t matter if you migrate or change the domain name, you can just change the domain name and don’t worry about anything else. If you save the full path and change the domain name IP, it will be miserable!

    If you save it to a third party, it will return an ID to you. Just save this ID directly. Then you can request this ID to get the picture you want!

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:54:36

    If it is an image on the local server, then save the path relative to the root directory of your project (such as /upload/20170413/xxxx.png). This has two advantages: 1. You can use it directly when referencing it on the front end (prerequisite The configuration root of your nginx or the like is your project path). 2. When migrating your project, you can migrate it directly without any major problems.
    If it is a third-party address, then just save the complete address directly (such as http://img.xxx.xx.qiniu.com/x...). You, the third party, can't interfere with anything, and there will be no interference. Address migration argument.

    reply
    0
  • Cancelreply