简体   繁体   English

上传前重命名并替换多个图像文件

[英]rename and replace multiple image file before upload

i have an upload script through which user can upload and rename multiple images to the server. 我有一个上传脚本,用户可以通过该脚本将多个图像上传并重命名到服务器。 it works perfectly but what i want is that while renaming each image should be named as 1,2,3,4.. and so on depending on the number of uploaded image and it should also replace the previous image having the same name. 它完美地工作,但我想要的是在重命名每个图像时应将其命名为1,2,3,4 ..,依此类推,具体取决于所上传图像的数量,并且它还应该替换具有相同名称的先前图像。 Here is my code: 这是我的代码:

HTML part: HTML部分:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Attachment(s): <input type="file" name="file[]" id="file" maxlength="500" accept="application/pdf,image/*" multiple>
    <input type="submit" name="submit" value="Request">
</form>

upload_file.php upload_file.php

<?php

// config
$upload_dir = 'uploaded_files'; // set your upload dir
$max_size = 1048576; // max file size: 1 MB
$allow_override = FALSE; // allow uploading files overriding existing ones
$valid_exts = array( // allowed extensions
    'gif',
    'jpeg',
    'jpg',
    'png',
    'pdf',
);
$valid_types = array(
    'image/gif',
    'image/jpeg',
    'image/jpg',
    'image/pjpeg',
    'image/x-png',
    'image/png',
    'text/pdf',
    'application/pdf',
);

// reorganize files array
$files = array();
foreach ($_FILES['file'] as $attr => $arr) 
    {
        foreach ($arr as $k => $v) 
            {
                $files[$k][$attr] = $v;
            }
    }

// loop thru files
foreach ($files as $file)
    {
        $status = 'Failure';
        // get extension
        $extension = pathinfo($file['name'], PATHINFO_EXTENSION);

        // make sure extension and type are not empty
        if ( ! (strlen($extension) && strlen($file['type']))) 
            {
                $msg = 'File extension or type not found';
            }
        else 
            {

                // make sure extension and type are allowed
                if ( ! (in_array($file['type'], $valid_types) && in_array($extension, $valid_exts))) 
                    {
                        $msg = "Extension '$extension' or file type '$file[type]' is not permitted";
                    }
                else 
                    {

                        // make sure file is not empty
                        if ( ! $file['size']) 
                            {
                                $msg = 'File seems to be empty (0 KB)';
                            }
                        else 
                            {

                                // make sure file is not too large
                                if ($file['size'] > $max_size) 
                                    {
                                        $msg = 'File is too large (' . ceil($file['size'] / 1024) . 'kB > ' . floor($max_size / 1024) . 'kB)';
                                    }
                                else 
                                    {

                                        // rename file here as you need
                                        $target = "$upload_dir/$_SESSION[myusername]Rejoin.$file[name]"; //rename file by placing rejoin before it
                                        // make sure files don't override
                                        if ( ! $allow_override && file_exists($target)) 
                                            {
                                                $msg = "File already exists";
                                            }
                                        else 
                                            {
                                                // no other errors
                                                if ($file['error'] > 0) 
                                                    {
                                                        $msg = "Unknown upload error (Code: $file[error])";
                                                    }
                                                else 
                                                    {
                                                        // attempt uploading
                                                        if ( ! move_uploaded_file($file['tmp_name'], $target)) 
                                                            {
                                                                $msg = 'Upload failed. Folder issues?';
                                                            }
                                                        else
                                                            {
                                                                // all good!
                                                                $msg = 'Upload successful!';
                                                                $status = 'Success';
                                                            }
                                                    }
                                            }
                                    }
                            }
                    }
            }       
        $out[] = "$file[name]: $status. $msg";
    }
echo implode("\n", $out);

/* End of file */
?>

Would appreciate if someone could guide me 如果有人可以指导我将不胜感激

Adding the $key => $value into your foreach would give you the position of the file in the upload. 在您的foreach中添加$ key => $ value会给您文件在上传中的位置。 You could use this late to change the $target variable, by concatenating together the $key instead of the $file[name]. 通过将$ key而不是$ file [name]连接在一起,您可以稍后使用它来更改$ target变量。

If you are going to rename the files and overwrite the ones already on the server, skipping the $file[name] would be appropriate, since you dont really need what the user called the picture, in favor of your numerical renaming. 如果要重命名文件并覆盖服务器上已经存在的文件,则跳过$ file [name]将是适当的,因为您确实不需要用户称为图片的东西,而是支持数字重命名。

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM