简体   繁体   中英

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.

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" :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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