Grow your CSS skills. Land your dream job.

Multiple File Upload

  • # April 1, 2013 at 10:01 pm

    Hey guys,

    I can’t get my head around a script to upload multiple photos. I am using:

    to be able to select an array of files and upload them to a new folder that I create for every album.

    Here is my script:

    if (isset($_POST) ) {
    $albumTitle = $_POST;

    $fileTmpName = $_FILES;
    $fileName = $_FILES;
    $fileType = $_FILES;
    $fileSize = $_FILES;
    $fileError = $_FILES;

    $fileCount = count($fileName);

    $albumPath = ‘albums/';
    $albumFolder = md5( uniqid( microtime(), true ) );

    if ( empty($albumTitle) ) {
    echo “There is no album title.
    “;
    exit;
    }

    for ( $i = 0; $i < $fileCount; $i++ ) {
    if ( empty($fileName[$i]) ) {
    echo “There is no file selected.
    “;
    exit;
    }
    if ( $fileType[$i] == ‘image/jpg’ || $fileType[$i] == ‘image/jpeg’ || $fileType[$i] == ‘image/png’ || $fileType[$i] == ‘image/gif’ ) {
    echo “Good image type.
    “;

    if ( $fileError[$i] == 0 ) {
    echo “The files have successfully been uploaded to the tmp folder.
    “;

    if ( mkdir( $albumPath . $albumFolder ) ) {
    echo “Directory is successflly created.“;

    move_uploaded_file($fileTmpName[$i], $albumPath . $albumFolder . ‘/’ . $fileName[$i]);
    } else {
    echo “Error creating a directory”;
    exit;
    }

    } else {
    echo “There was an error uploading the file.
    “;
    exit;
    }
    } else {
    echo “Wrong image type.
    “;
    exit;
    }
    }
    }

    Basically I thought I would loop through the amount of files that were selected and move them in a folder one by one… I really don’t know if that if a good way of doing it and if there are more efficient ways (surely) but it doesn’t seem to work up to the creation of the folder…

    If I upload 1 photo it works, but as soon as there is 2 or more… it doesn’t.

    Thanks for the help!!!

    # April 1, 2013 at 10:12 pm

    So I have made if work finally! Here is the working script and please share if I can optimize it!

    if (isset($_POST) ) {
    $albumTitle = $_POST;

    $fileTmpName = $_FILES;
    $fileName = $_FILES;
    $fileType = $_FILES;
    $fileSize = $_FILES;
    $fileError = $_FILES;

    $fileCount = count($fileName);

    $albumPath = ‘albums/';
    $albumFolder = md5( uniqid( microtime(), true ) );

    if ( empty($albumTitle) ) {
    echo “There is no album title.
    “;
    exit;
    }

    for ( $i = 0; $i < $fileCount; $i++ ) {
    if ( empty($fileName[$i]) ) {
    echo “There is no file selected.
    “;
    exit;
    }
    if ( $fileType[$i] == ‘image/jpg’ || $fileType[$i] == ‘image/jpeg’ || $fileType[$i] == ‘image/png’ || $fileType[$i] == ‘image/gif’ ) {
    echo “Good image type.
    “;

    if ( $fileError[$i] == 0 ) {
    echo “The files have successfully been uploaded to the tmp folder.
    “;
    } else {
    echo “There was an error uploading the file.
    “;
    exit;
    }
    } else {
    echo “Wrong image type.
    “;
    exit;
    }
    if ( is_dir( $albumPath . $albumFolder ) ) {
    move_uploaded_file($fileTmpName[$i], $albumPath . $albumFolder . ‘/’ . $fileName[$i]);
    } else {
    mkdir( $albumPath . $albumFolder );
    move_uploaded_file($fileTmpName[$i], $albumPath . $albumFolder . ‘/’ . $fileName[$i]);
    }
    }
    }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".