Jump to content

How to store an image in MySQL


LeonLatex
Go to solution Solved by kicken,

Recommended Posts

I have a TABLE called article. In this table I have 4 columns which is articleid, articletext and and articledate .

Time has come to upgrade the TABLE with one more COLUMN. That's not the problem, how to create it. The problem is what values/parameters I need to enter/set.

I am not storing the images in the database. I will just store the link to the picture that is stored in its own directory (images) given its own name on upload to the directory where the pictures are stored.

This is how far I have come til now:

ALTER TABLE article
ADD [COLUMN] articleimage column_definition |AFTER articletext];

To start somewhere to move on, I think the best thing is to create the path that will be converted to a link in a PHP script including href="http://www.******.tld /images/article_images/
As you understand, the link to the images directory will be fixed in PHP/HTML, but when it is loaded, the name of the file will be inserted after article_images/

So, when the page loads PHP and HTML, the link will be created and printed out to be shown visitor's browser. Since the name of the image will be arbitrarily/automatically entered, I am not sure which values/parameters I should enter for the column I will create/have created since the image can then consist of all kinds of letters and characters.

If I'm completely lost here, please tell me how this is normal and how to do it. Because it's been so long, at least 15 years since I've done something like this.

Whoops, this must have gotten messy, but I hope you understand what I'm trying to figure out.

 

Link to comment
Share on other sites

I think you're making things more difficult that it really needs to be?

Here's an example of how you can save an image path in PHP:

$image_path = "images/my_image.jpg";

In this example, the variable $image_path is set to the path of the image file "my_image.jpg" located in the "images" directory.

You can then use this variable to display the image on a webpage or perform other operations with the image file. To display an image in HTML, you can use the <img> tag and set the src attribute to the path of the image file.

<img src="images/my_image.jpg" alt="My Image">

 

  • Great Answer 1
Link to comment
Share on other sites

  • Solution

If you want to store a path to an image in the DB, just use a VARCHAR column with some resonable maximum length.   If you have control over the images file names (ie, you're making them / receiving them as an upload) then you can choose a length and just stick to it.  For uploaded files, I tend to use a VARCHAR(16) and generate a random name of that length for the file.

If you don't control the name (say, you're just storing a URL to an external image) then choose a maximum length that should be fine and just validate that in your form when creating a record.  Either that or us a text column if you want to allow crazy long URLs.  I tend to use VARCHAR(2048) for arbitrary URLs.

 

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.