-1

So, I have the following code to upload a PDF and i would like to rename it because if it has the same name that already exists it will replace the old one.
Here's my code:

$allowedExts = array("pdf");
        $temp = explode(".", $_FILES["pdf_file"]["name"]);
        $newfilename = round(microtime(true)) . '.' . end($temp);
        $extension = end($temp);
        $upload_pdf=$_FILES["pdf_file"]["name"];
        move_uploaded_file($_FILES["pdf_file"]["tmp_name"],"anexos/" . $_FILES["pdf_file"]["name"]);
        $querypdf  = "INSERT INTO anexos (pdf_file,id_colaborador) VALUES (?,?)";
        $qpdf = $pdo->prepare($querypdf);
        $qpdf->execute(array($upload_pdf,$id));
        //die(print_r($_FILES));
Carlos
  • 1
  • 3
  • Possible duplicate of [How to rename uploaded file before saving it into a directory?](https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory) – Kashif Rafique Jan 17 '19 at 15:57

1 Answers1

1

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

$newfilename = round(microtime(true)) . '.' . end($temp);

move_uploaded_file($_FILES["pdf_file"]["tmp_name"],"anexos/" . $newfilename);
Siva
  • 1,481
  • 1
  • 18
  • 29