Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which type of path to store in MySQL database?

Tags:

sql

php

mysql

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://----)
like image 583
web lover Avatar asked Dec 21 '10 11:12

web lover


People also ask

Which datatype is used to store files MySQL?

A Binary Large Object ( BLOB ) is a MySQL data type that can store binary data such as images, multimedia, and PDF files.

Do we need to set path for MySQL?

To use MySQL server on the command line, you must have the correct system path. This is usually done automatically.

How are MySQL databases stored?

MySQL stores each database (also called a schema) as a subdirectory of its data directory in the underlying filesystem. When you create a table, MySQL stores the table definition in a . frm file with the same name as the table. Thus, when you create a table named MyTable , MySQL stores the table definition in MyTable.


2 Answers

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.

like image 114
Mohsen Avatar answered Sep 29 '22 17:09

Mohsen


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

like image 24
Keyne Viana Avatar answered Sep 29 '22 16:09

Keyne Viana