How to upload images and store path in mysql db using php

I would like to learn how to create a web form which will allow end users to upload images. In addition, I would like the uploaded image file path to be stored in a mysql table so that it can be referenced throughout other web pages in my project.

The web form would contain the following fields:
image [upload]

If possible, the ability to create thumbnail versions of the uploaded images on the fly would be a plus. I remember reading that PHP has some built in functions for such a task.

I think I can handle the mechanics of editing and deleting the title and desctiption once they are in the db, but the file upload is what I need help with.

I have read up on php's ftp functions and attempted to implement on my own without success. Since it would be beneficial for my end user to be able to batch upload the files, I thought using FTP would be best, but maybe there is a better solution.

Does anyone have a solution for file upload? I intend to expand on this once we get it up and running to include the ability to upload other file types such as .flv and/or .swf -- as well as expand its functionality to allow the end user to create "galleries" of photos by incorporating another form which will associate groups of these files with each gallery. Again, I think I can figure that one out -- but the file uplaod part is what I am struggling with right now.

Sorry for being so wordy and as always, thank you in advance for any help that is offered.

Who is Participating?
Frosty555Connect With a Mentor Commented:
PHP supports file uploads via a "file" input field in a form. When the form is posted, the contents of the file is uploaded with it and PHP saves it in a temporary location, specifying the path to the file in the $_FILES array. It is your script's job to do something with this file (e.g. copy it to a permanent location) before the script finishes. Any temp files are cleaned up after the script finishes.

Once you have moved the file from it's temp location to somewhere else, you can write the path of where you put the file into your database using a regular INSERT/UPDATE query to mysql.

You must ensure your <form> tag has it's enctype attribute set to enctype="multipart/form-data" otherwise the file will not upload. That one stumped me for a few hours the first time I did this in PHP.

Also, ensure the folder you copy the file to is one that PHP has write access to. The location would be a folder on the server that the PHP script is running on (e.g. your webserver)

A complete tutorial is here::

You can also thumbnail-ify an image once it has been uploaded provided your PHP server has the appropriate extensions installed to handle image manipulation. There are a few functions which allow you to open a JPEG file, bitblt the data from that bitmap, to another bitmap in memory, stretching or squishing or cropping as you need, and then you can either:

1) write that data in JPEG form back to a file for storage
2) echo it directly out from the php page

If you use #1, you save the thumbnail file to the webserver in a folder you have write access to, and save the path of the thumbnail file in your database

If you use #2, you actually generate the thumbnail on the fly every time a visitor goes to your webpage, and your IMG tag's src attribute is set to the PHP page itself, e.g.

<img src="thumbnailgen.php?file=myfile.jpg">

Some examples of doing it the #1 way:
jsvb1977Author Commented:
Thanks, Frosty555.

I will work on this and report back. I have already begun reading the first tutorial you posted. Thanks for your help on this one.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.