Uploading Images in PHP

Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don't hold it against me. One of the perils of having a 20+ year old website!

After dabbling in PHP for some time, and writing an image upload script about 8 months ago, I would like to post it here, with a short tutorial. I have recently reused the script and a friend of mine asked how he would go about doing such a thing. My upload script, to begin, is a function in PHP that returns the path of the image file, which I then add to a database.

The first thing you must do is set the form up correctly. along with the form name and method (for this, method="post"), you have to specify that you are uploading more than text. Simply add enctype="multipart/form-data" in the form tag. Second, you have to set the permissions on your server to allow uploads. You have to set the Permissions to “777” or Read, Write, Execute for Owner, Group and Public (or World).

Permissions Image

Now onto the function. While processing the form, all you do is add something like $image= upload_pic($picName);. This calls the function upload_pic(), passing the argument $picName. The following is an image with well documented code explaining what is going on. For some reason, WordPress doesn’t like it when I code right in the post. At the bottom of the post is a .txt file with all of the code in this post.

Upload Image Image

Here is the .txt file with all of the code shown above. If you have any questions, or feel I missed something, be sure to comment on it! Later.

Similar Posts

  • |

    Gloss WordPress Plugin

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Today, I officially released my first public WordPress plugin, Gloss. Gloss is a dictionary/glossary management plugin I decided to build which…

  • |

    Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images). function attachment_search( $query ) {…

  • | |

    My First NETTUTS Tut

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Web Design tutorial blog NETTUTS published an article I wrote about Building a Facebook Appication Check it out! New post here…

  • |

    WordPress Helper Functions for Detecting IE

    The other day I was working on a problem where I wanted to check if a website was using a specific browser (in this case IE) and version (in this case 9 or below). I came up with 2 functions that would serve an a nice, reusable check for both. These can also be extended…

  • |

    Quick Tip: Add a Widget to WordPress

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a…

  • The coding process

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!During my winter break, which is very quickly coming to a close, I have done a lot of coding- stuff I…