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

标签 php mysql upload renaming

我有一个用于多个图像 uploader 的脚本,问题是重命名文件。好吧,我想为图像命名,例如 albumid_photoid.png(示例 4_1.png)。在循环中选择多个图像时,我总是得到相同的名称(查看最后一个变量 $newName)(例如: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;

至:

$rc = $i + 1;

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

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

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

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

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

关于php - 上传多个实例时需要重命名文件的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131750/

相关文章:

javascript - 提交消息(来自 Bootstrap 的模板)

java - 填写网站中的 PHP/HTML 字段

php - 谷歌AMP : amp-list vs php loop - SEO/Pagespeed

javascript - Blueimp 文件上传 - 预览图像像素化

c# - ASP.Net 处理程序 Request.Files 始终为空

php - search.php 页面的 htaccess 规则

php - 如何将 mySQL 的输出查询放入 php int 变量

python - SQLAlchemy 和原始 SQL : List as an input

mysql - 迁移后什么会导致mysql性能下降?

html - HTML 上传表单中的文件类型