Question

I have this script for multiple image uploader and the problem is renaming the files. Well, I want to give names to the images like albumid_photoid.png (example 4_1.png). When selecting multiple images in the loop I always get same name (look at the last variable $newName) (exampl: 4_1.jpg, 4_1.jpg, 4_1.jpg, - supposing I've selected 3 files).

if (isset($_POST['upload'])) {
        $count_files = count($_FILES['uploaded_file']['tmp_name']);
        $sql_album_id = "SELECT * FROM albums WHERE title = '".$_GET['album']."'";
        $res_album_id = mysql_query($sql_album_id) or die(mysql_error());
        $row_album_id = mysql_fetch_assoc($res_album_id);

        for ($i = 0; $i < $count_files; $i++) {
        // Access the $_FILES global variable for this specific file being uploaded
        // and create local PHP variables from the $_FILES array of information
        $fileName = $_FILES["uploaded_file"]["name"][$i]; // The file name
        $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"][$i]; // File in the PHP tmp folder
        $fileType = $_FILES["uploaded_file"]["type"][$i]; // The type of file it is

        $fileSize = $_FILES["uploaded_file"]["size"][$i]; // File size in bytes
        $fileErrorMsg = $_FILES["uploaded_file"]["error"][$i]; // 0 for false... and 1 for true
        $kaboom = explode(".", $fileName); // Split file name into an array using the dot
        $fileExt = end($kaboom); // Now target the last array element to get the file extension
        $fileTitle = $kaboom[0];

        // START PHP Image Upload Error Handling --------------------------------------------------
        if (!$fileTmpLoc) { // if file not chosen
            echo "ERROR: Please browse for a file before clicking the upload button.<br />";

        } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
            echo "ERROR: Your file was larger than 5 Megabytes in size.<br />";
            unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder

        } else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
            // This condition is only if you wish to allow uploading of specific file types    
            echo "ERROR: Your image was not .gif, .jpg, or .png.<br />";
            unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder

        } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
            echo "ERROR: An error occurred while processing the file. Try again.<br />";

        }
        // END PHP Image Upload Error Handling ---------------------------------

        $row_select = mysql_fetch_assoc($res_select);

        // renaming images
        $sql_count = "SELECT COUNT(id) FROM photos WHERE album_id = '".$row_album_id['id']."'";
        $res_count = mysql_query($sql_count) or die(mysql_error());
        $row_count = mysql_fetch_row($res_count);
        $rc = $row_count[0] + 1;
        $fileTitle = $row_album_id['id']."_".$rc;
        $newName = $fileTitle.".".$fileExt;
        echo $newName;

    }

}
Was it helpful?

Solution

It seems to me you only need to change:

$rc = $row_count[0] + 1;

to:

$rc = $i + 1;

as $i is the counter you are using for the images.

And just in case, the mysql_* functions are deprecated and you have an sql injection problem. You'd better switch to PDO or mysqli and use prepared statements with bound variables.

Edit: To be able to get the next sequence number from the database, you have to store the images in the database, otherwise you will never get an increase in the number of images found.

Apart from that this method will fail: If you delete for example the 2nd image of 8, your count will give you 8 as the next available number and you will overwrite your number 8 which is actually your 7th image.

You need to store the sequence numbers in the database to make sure you don't overwrite anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top