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

  • | |

    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…

  • | |

    Create a Temp/Splash Page in 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!Often times I will be working on a WordPress theme that I want to live test, but I don’t want to…

  • | | |

    Learning HTML and CSS (with My New Book, Out Now!)

    I’ve always been a learn-by-doing kind of guy. Maybe it’s because I’m thinking about other things when something is explained to me, or maybe people are bad at explaining things. But either way, a concept doesn’t really sink in until I do it, or until I see an immediate application for it. When I learned…

  • | |

    What Developers can learn from Home Improvement

    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! It’s no secret that I love the show Home Improvement, especially if you know me personally or follow me on…

  • | |

    Simplecast Episodes and New Projects Page

    Over the weekend, I announced on Twitter that I officially released a plugin I’ve been working on for a while called Simplecast Episodes, which allows you to add and embed podcasts hosted at simplecast.fm into WordPress. I’m happy to say that it’s officially in the WordPress Plugin Directory.

  • Use Input Masks for Better UX and Easier Validation

    Recently I was developing a few forms for a project at work and wanted a fairly specific format for the input of one of the fields. It was a time of day, and since the <time> element isn’t very well supported in browsers yet, I opted for my own text input and validation. While I…