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

  • Types of classes

    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 and over the last couple of days we have been talking about classes in Java. I’m fairly certain I understand…

  • | |

    How I Built It is Officially Hosted by Castos!

    Castos is a fantastic WordPress-centric podcast hosting platform that I’ve been recommending in my courses and coaching calls. But I have a confession to make: since Castos launched after I launched my podcast, I’ve been using Libsyn. For a long time, I’ve considered making the switch, but the migration process always seemed so daunting. On…

  • |

    I’m on Github!

    I know it’s something I should have had for a while but I generally don’t feel my code is up to snuff. Seeing as I’ve been promising people my code, however, it makes sense. My username on @github is jcasabona. You can get to my repo using the link below. My Github Repository Right now…

  • |

    Learn a Programming Language

    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!Over at LifeHacker, they are hosting a spirited discussion on what the programming language to learn first is. I think this…

  • |

    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…

  • Programming in C

    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!This semester I am taking a class where it was recently decided the primary language will be C. While I did…