Upload multiple images

Links for php scripts

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Apr 11, 2013 3:17 am

Upload multiple images

Postby octopus » Thu Apr 11, 2013 3:20 am

I've written this code that takes two variables via POST. Some file and a checkbox to say whether the image can be downloaded. I then have this processing code to upload multiple files:

Code: Select all


        // Number of uploaded files
        $num_files = count($_FILES['file']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
         echo "Started loop";
            // check if there is a file in the array
            echo "No file";
                $messages[] = 'No file uploaded';
            echo "Upload Loop";
            $upload_dir =  rand(1,5);
            $rand_code = mt_rand(1,800);
            $rand_string = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 15);
            if ($_POST['imgdownloadable'] == "yes"){
               $downloadable = "yes";
               $downloadable = "no";
                // copy the file to the specified dir
                if(copy($_FILES['file']['tmp_name'][$i],'uploads/' . $upload_dir. '/' . $_SESSION['MM_Username'] . $rand_code . $rand_string .$_FILES['file']['name'][$i]))
                    $downloadlink = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5);
               $filename = $_FILES['file']['name'][$i];
               $mysqlstring = "INSERT INTO images (user, server, img-name, download, download-url) VALUES ($_SESSION[MM_Username], $upload_dir, $filename, $downloadable, $downloadlink";
               $mysql = mysql_query($mysqlstring) or die(mysql_error());
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
               echo "Failed";
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
<?php echo $mysql;
echo '<pre>';
    echo '</pre>';

The bit after echo "Upload Loop; is a bit confusing so i'll explain it
Let's say we're logged in with a username of "example@example.com
Pick a random number between 1 and 5 , say 3
Pick another random number, say 15
Pick a random string, say xyz
If the checkbox is checked set downloadable to yes, else no.
Move our temporary upload (lets say it's called test.png) to uploads/3/example@example.com15xyztest.png

I've written some debug echos in and all I get back is

Thanks for any help!

php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Upload multiple images

Postby seandisanti » Fri Apr 12, 2013 3:38 pm

There have been several good threads on image uploading and modifying images recently in the PHP->Images folder. I am sure a quick code comparison with one of the solved ones will show you your issue.

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest