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

  • |

    Add IE Conditionals to wp_enqueue_style

    When creating a WordPress theme, it’s best practice to use wp_enqueue_style for adding all stylesheets, including style.css. At first glance, this can pose a challenge if you want to conditionally include CSS based on the browser (like IE-only styles, for example). Luckily, there is a quick way to do this in WordPress using $wp-styles: global…

  • | |

    2015: Resolving to Do More, and Less

    Every year I publish my list of resolutions as well as a scorecard from the previous year. For 2015, I’ve redesign the site (well, I used HTML5 Boilerplate) and broke my resolutions down into 2 categories: “Do More” and “Do Less.”

  • |

    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…

  • Fixing the Fatal ‘add_rewrite_tag()’ Error

    I’m working on a WordPress little side project where I am registering a new Custom Post Type and Taxonomy and came across this: Fatal error: Call to a member function add_rewrite_tag() on a non-object… After doing a Google Search to try to figure out what the issue is, all I came up with was “disable…

  • 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…

  • | |

    Deleting 3 Million Spam Comments from a WordPress Database

    Over at the Crowd Favorite blog, I wrote up a post about an interesting problem I solved recently. I laid out everything you need to know there, but it involves downloading a HUGE database and putting WP-CLI to good use. If you haven’t used it before and you do development with WordPress, it’s super valuable….