PHP File Uploader

Easily implement a file uploader to handle image, audio, and documents uploaded via a HTML form!

Download .zip Download .tar.gz View on GitHub

PHP File Uploader now supports uploading of audio files!

The best part is it's just as easy to upload audio files as it is images (and no need to worry about changing your HTML because implementation has not changed a bit!).

PHP File Uploader was created to make image uploading easy. I've seen a few uploading scripts that work pretty well, but they can be a pain to try and get them to work correctly. I created this script with one goal in mind, keep it simple. With PHP File Uploader you can be uploading images, audio files, and documents with a few lines of PHP and a simple HTML form!

For a minimal implementation using an HTML form.

<?php
if(isset($_POST['imageUpload'])){
    /*  The three lines below are the only lines needed in PHP to upload an image! */
   /* -- */require 'classes/UploadImage.class.php';
   /* -- */ $imageUploader = new UploadImage();
   /* -- */ $imageUploader->upload($_FILES['image']);


   /* The following below is there to show the output of the script 
and any error message there may have been */
    if($imageUploader->getNumberOfErrorsDuringUpload() > 0){
        echo '<pre>'.$imageUploader->returnFormattedErrors().'</pre>';
    }

    for($i = 0; $i < $imageUploader->getNumberOfSuccessfulUploads(); $i++){
        echo '<img src="'.$imageUploader->getFilePathThumbAtIndex($i).$imageUploader->getFileNameThumbAtIndex($i).'"/>';
    }
}
?>
<html>
<head>
    <title>Image Uploader</title>
</head>
<body>
    <form action='#' method='POST' enctype='multipart/form-data'>
        <input type='file' name='image[]' multiple /> <!-- name='image[]' must be an array or the script will die and your files will not be uploaded -->
        <input type='submit' name='imageUpload' />
    </form>
</body>
</html>

For a basic URL image upload.

<?php
if(isset($_POST['urlUpload'])){
    require 'classes/SingleURLImageUploader.class.php';

    $URLUploader = new SingleURLImageUploader();
    $URLUploader->uploadSingleURLImage($_POST['url']);

    if($URLUploader->getNumberOfErrorsDuringUpload() > 0){
        echo '<pre>'.$URLUploader->returnFormattedErrors().'</pre>';
    }

    for($i = 0; $i < $URLUploader->getNumberOfSuccessfulUploads(); $i++){
        echo '<img src="'.$URLUploader->getFilePathThumbAtIndex($i).$URLUploader->getFileNameThumbAtIndex($i).'"/>';
    }
}
?>
<html>
<head>
    <title>Image Uploader</title>
</head>
<body>
    <form action='#' method='POST' enctype='multipart/form-data'>
        <input type='text' name='url' multiple />
        <input type='submit' name='urlUpload' />
    </form>
</body>
</html>

For basic audio upload.

<?php
    if(isset($_POST['submitAudio'])){
    require 'classes/UploadAudio.class.php';

    if(!empty($_POST['newDirectory'])){
        $directory = $_POST['newDirectory'];
    }
    else {
        $directory = 'AudioUpload/';
    }

    $audioUploader = new UploadAudio();
    $audioUploader->setOriginalFilePath($directory);
    $audioUploader->upload($_FILES['audio']);

    if($audioUploader->getNumberOfErrorsDuringUpload() > 0){
        echo '<pre>'.$audioUploader->returnFormattedErrors().'</pre>';
    }

    for($i = 0; $i < $audioUploader->getNumberOfSuccessfulUploads(); $i++){
        echo 
            $audioUploader->getFileNameAtIndex($i)
            ."  <br />
            <audio style='display:block;' controls src='".$audioUploader->getFilePathAtIndex($i).$audioUploader->getFileNameAtIndex($i)."' type='".$audioUploader->getFullFileTypeAtIndex($i)."'>
                Your browser does not support HTML audio
            </audio>
        ";
    }

    }
?>
<html>
<head>
    <title>Audio Uploader</title>
</head>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        <h3><label for="image">Select Audio:</label></h3>

        <input type='file' name='audio[]' multiple/>
        <br />
        <label>Create new directory (ex. audio/playlists/Eminem/)</label>
        <input type="text" name="newDirectory" />

        <input type="submit" name="submitAudio" value="UploadAudio"/>
    </form>
</body>
</html>

For an upload via HTML form with all the options.

<?php
if(isset($_POST['imageUpload'])){
    require 'classes/UploadImage.class.php';
    $uploader = new UploadImage();
    $uploader->setOriginalFilePath('path/to/where/you/want/the/files') // this directory will be created if it does not already exist
    $uploader->smallImageWidth(300);
    $uploader->smallImageHeight(300); // either the height will be 300 or the width will be 300 depending on which is greater so we can maintain the aspect ratio
    $uploader->thumbImageWidth(150);
    $uploader->thumbImageHeight(150); // same as the smallImageHeight/Width regarding aspect ratio

/* 
 * You may also use this to set small/thumb height/width
    $uploader->setSmallImageWidthAndHeight(300, 300);
    $uploader->setSmallImageWidthAndHeight(150, 150);
*/

    $uploader->fullImageQuality(95);
    $uploader->smallImageQuality(60);
    $uploader->thumbImageQuality(50); // Image quality is for png/jpeg image types
    $uploader->prependToFilename(cuteCats); // the filename is automatically generated so if you want to keep some organization to the filenames then you may do so using this. NOTE: the filesize will also be prepended to the small and thumb images
    $uploader->numberOfFilesPerDirectory(1000); // once the limit is hit a new directory will be created to help with organization
    $uploader->numberOfAllowedImagesToUpload(5); // how many images that can be uploaded at one (make sure your php.ini file will allow the number of uploads you set here)
    $uploader->errorLogFileName('uploadErrorLog');
    $uploader->inParentDirectory(true); // if true this will put the upload folders in a parent directory of where you're calling the script. This may have been a specific need for my application but I decided to leave it in there in case someone else may need it.

    $uploader->upload($_FILES['image']);
}
?>
<html>
<head>
    <title>Image Uploader</title>
</head>
<body>
    <form action='#' method='POST' enctype='multipart/form-data'>
        <input type='file' name='image[]' multiple /> <!-- name='image[]' must be an array or the script will die and your files will not be uploaded -->
        <input type='submit' name='imageUpload' />
    </form>
</body>
</html>

Authors and Contributors

2013: @mdahlke

Support or Contact

Having trouble with ImageUploader? Check out the documentation at https://github.com/mdahlke/ImageUploader/wiki or email madahlke27@gmail.com.