-2

This is my html form

<form action="index.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>

This is my index.php file

<?php
foreach ($_FILES["userfile"]["error"] as $key => $error) {
      if ($error == UPLOAD_ERR_OK) {
          echo"$error_codes[$error]";
          move_uploaded_file(
          $_FILES["userfile"]["tmp_name"][$key], 
          $_FILES["userfile"]["name"][$key] 
          ) or die("Problems with upload");
      }
}
?>

**The code is working properly. But, What I really need is to change the name of the 1st uploaded file to birthcertificate and the name of the 2nd uploaded file into NIC. **

**Example : If I upload a file named 123 or abc (whatever the name) the 1st file's name should be birthcertificate and the 2nd file's name should be NIC. **

kole12
  • 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) – But those new buttons though.. Nov 10 '18 at 06:21

3 Answers3

0
move_uploaded_file(file, location);

You can use file and new name in location parameter like this:

$newname = "yourimg.png";
enter code here
move_uploaded_file($_FILES["userfile"]["tmp_name"][$key], "your location" . $newname);

This is the basic way of renaming, make changes to the loop for renaming both files. If you only upload 2 files at a time, you can use the array index for your logic.

Ashish Yadav
  • 1,901
  • 2
  • 17
  • 23
0

You can rename a file:
Instead of below code inside foreach you have shared

move_uploaded_file(
          $_FILES["userfile"]["tmp_name"][$key], 
          $_FILES["userfile"]["name"][$key] 
          ) or die("Problems with upload");

You can use:

$temp = explode(".", $_FILES["userfile"]["name"]);
$newfilename = 'birthcertificate' . '.' . end($temp);
move_uploaded_file($_FILES["userfile"]["tmp_name"], $newfilename) or die("Problems with upload");

Try giving the file seperate names, in PHP you can receive them and make in one array if you need

You can also refer to this link:

How to rename uploaded file before saving it into a directory?

Amir Mustafa
  • 85
  • 2
  • 7
0

There are probably lots of ways to do this. I thought that making a list of the new file names might be the way to go.

<?php
// Make a list of the new file names
$newFileNames = ['birthcertificate', 'NIC'];
foreach ($_FILES["userfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        echo"$error_codes[$error]";
        // Grab new file name
        $newFileName = array_shift($newFileNames);
        // Make sure we actually got one
        if ( $newFileName )  {
            move_uploaded_file(
                $_FILES["userfile"]["tmp_name"][$key], 
                $newFileName) 
            or die("Problems with upload");
        }
    }
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • Thank you for the your answer. After upload the file. how we can view the files ? – kole12 Nov 10 '18 at 06:59
  • 1
    You aren't supposed to ask follow on questions like this. Open a new question. Be prepared to describe in detail what you want to do. Who is "we" that needs to see the files? What types of files are they? Where are they stored? How do you want your app to show them? – ryantxr Nov 10 '18 at 07:04