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

Php 上传多个实例时需要重命名文件的帮助吗,php,mysql,upload,renaming,Php,Mysql,Upload,Renaming,我有这个脚本的多图像上传和问题是重命名的文件。我想给像albumid\u photoid.png示例4\u 1.png这样的图像命名。在循环中选择多个图像时,我总是得到相同的名称,请查看最后一个变量$newName exampl:4_1.jpg、4_1.jpg、4_1.jpg,-假设我选择了3个文件 if (isset($_POST['upload'])) { $count_files = count($_FILES['uploaded_file']['tmp_name']);

我有这个脚本的多图像上传和问题是重命名的文件。我想给像albumid\u photoid.png示例4\u 1.png这样的图像命名。在循环中选择多个图像时,我总是得到相同的名称,请查看最后一个变量$newName exampl:4_1.jpg、4_1.jpg、4_1.jpg,-假设我选择了3个文件

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;

    }

}

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

$rc = $row_count[0] + 1;
致:

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

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

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

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


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

我知道了,但数据库是问题所在。我想继续数据库中的序列。例如,如果我有8张相册id相同的照片,我想命名下一张照片9、10和11。明白了吗?@AmirRami你有存储在数据库中的序列号吗?如果有,如何/在哪里?没有,我有:$sql\u count=SELECT COUNTid FROM photos where album\u id='。$row\u album\u id['id']$res\u count=mysql\u query$sql\u count或diemsql\u错误$row\u count=mysql\u fetch\u row$res\u count;你说得对。但是你有没有其他的想法来唯一地命名图像,而不是id-autoincrement@AmirRami我会将它们与序列号一起存储在数据库中,以便您可以轻松识别文件名。然后,使用SELECT MAXSERSEQUENCE_编号,您可以获得最高值,并且使用foreach循环,显示时是否缺少编号并不重要。在执行任何其他操作之前,您确实应该检查['error']。此外,您还容易受到sql注入攻击。您的文件扩展名regex无效,将允许foogf作为有效文件名通过。您正在从$res_select获取,但从未运行查询来生成该句柄,诸如此类blah@marc-b$res_select变量在我代码的其余部分,并且['error']没有错误
$rc = $i + 1;