上传多个实例时需要帮助重命名文件

时间:2013-11-21 20:58:59

标签: php mysql upload multiple-files renaming

我有这个脚本用于多个图像上传器,问题是重命名文件。好吧,我想给像albumid_photoid.png(例如4_1.png)这样的图像命名。当在循环中选择多个图像时,我总是得到相同的名称(查看最后一个变量$newName)(例如:4_1.jpg,4_1.jpg,4_1.jpg, - 假设我选择了3个文件)。 / p>

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;

    }

}

1 个答案:

答案 0 :(得分:0)

在我看来,你只需要改变:

$rc = $row_count[0] + 1;

为:

$rc = $i + 1;

因为$i是您用于图像的计数器。

以防万一,mysql_*函数已被弃用,并且您有一个SQL注入问题。您最好切换到PDO或mysqli并使用带有绑定变量的预准备语句。

编辑:为了能够从数据库中获取下一个序列号,您必须将图像存储在数据库中,否则您将永远不会增加找到的图像数量。 / p>

除此之外,此方法将失败:如果您删除例如8的第2张图像,您的计数将为您提供8作为下一个可用的数字,您将覆盖您的数字8,这实际上是您的第7张图像。

您需要将序列号存储在数据库中,以确保不会覆盖任何内容。