简体   繁体   English

php在上传之前重命名上传的文件,如果已经存在则覆盖

[英]php rename uploaded file before upload and overwrite if already exists

Currently using this code for the upload (but happy to use any if suggested)..目前使用此代码进行上传(但如果建议,很乐意使用任何代码)。

<form style="margin-bottom:2px;" method="post" enctype="multipart/form-data" name="formUploadFile">     
            <label>Select CSV file to upload:</label>
            <input type="file" name="files[]" multiple="multiple" /> <input type="submit" value="Upload CSV" name="btnSubmit"/>
        </form> 

        <?php
            if(isset($_POST["btnSubmit"]))
            {
                $errors = array();
                $uploadedFiles = array();
                $extension = array("csv");
                $bytes = 1024;
                $KB = 1024;
                $totalBytes = $bytes * $KB;
                $UploadFolder = "tmp_csv_store";

                $counter = 0;

                foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
                    $temp = $_FILES["files"]["tmp_name"][$key];
                    $name = $_FILES["files"]["name"][$key];

                    if(empty($temp))
                    {
                        break;
                    }

                    $counter++;
                    $UploadOk = true;

                    if($_FILES["files"]["size"][$key] > $totalBytes)
                    {
                        $UploadOk = false;
                        array_push($errors, $name." file size is larger than the 1 MB.");
                    }

                    $ext = pathinfo($name, PATHINFO_EXTENSION);
                    if(in_array($ext, $extension) == false){
                        $UploadOk = false;
                        array_push($errors, $name." invalid file type.");
                    }

                    if(file_exists($UploadFolder."/".$name) == true){
                        $UploadOk = false;
                        array_push($errors, $name." file already exists.");
                    }

                    if($UploadOk == true){
                        move_uploaded_file($temp,$UploadFolder."/".$name);
                        array_push($uploadedFiles, $name);
                    }

                }

                if($counter>0){
                    if(count($errors)>0)
                    {
                        echo "<b>Errors:</b>";
                        foreach($errors as $error)
                        {
                            echo " ".$error.",";
                        }
                        echo "<br/>";
                    }

                    if(count($uploadedFiles)>0){
                        echo "<b>Uploaded:</b>";
                        echo "=";
                        foreach($uploadedFiles as $fileName)
                        {
                            echo " ".$fileName.",";
                        }
                        echo "<br/>";

                        echo "<big><big>".count($uploadedFiles)." file has been successfully uploaded.</big></big>";
                                            }                                           
                }
                else{
                    echo "ERROR: Please press the browse button and select a CSV file to upload.";
                }

            }
        ?>

And would like to modify it so that it renames the uploaded file from "any-file-name.csv" to "foobar.csv" before it uploads the file and it should also overwrite the file if it already exists.并想修改它,以便在上传文件之前将上传的文件从“any-file-name.csv”重命名为“foobar.csv”,如果文件已经存在,它还应该覆盖该文件。

As a bonus the code currently allows for multi-file upload but I really only need it for a single file each time so it could also possibly be shortened a bit if changed to only allow a single file.作为奖励,代码目前允许多文件上传,但我真的每次只需要一个文件,所以如果更改为只允许一个文件,它也可能会缩短一点。

Thanks in advance :-)提前致谢 :-)

To add your custom name:添加自定义名称:

if($UploadOk == true){  
    $name = "foobar.csv";
    move_uploaded_file($temp,$UploadFolder."/".$name);
    array_push($uploadedFiles, $name);
}

For single file, remove multiple="multiple" :对于单个文件,删除multiple="multiple"

<input type="file" name="files[]" />

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

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