How to rename and save files in php











up vote
-2
down vote

favorite












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. **










share|improve this question






















  • Possible duplicate of How to rename uploaded file before saving it into a directory?
    – billynoah
    Nov 10 at 6:21















up vote
-2
down vote

favorite












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. **










share|improve this question






















  • Possible duplicate of How to rename uploaded file before saving it into a directory?
    – billynoah
    Nov 10 at 6:21













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











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. **










share|improve this question













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. **







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 5:49









kole12

11




11












  • Possible duplicate of How to rename uploaded file before saving it into a directory?
    – billynoah
    Nov 10 at 6:21


















  • Possible duplicate of How to rename uploaded file before saving it into a directory?
    – billynoah
    Nov 10 at 6:21
















Possible duplicate of How to rename uploaded file before saving it into a directory?
– billynoah
Nov 10 at 6:21




Possible duplicate of How to rename uploaded file before saving it into a directory?
– billynoah
Nov 10 at 6:21












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










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");
}
}
}





share|improve this answer





















  • Thank you for the your answer. After upload the file. how we can view the files ?
    – kole12
    Nov 10 at 6: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 at 7:04










  • I need to show as a link.
    – kole12
    Nov 10 at 7:19


















up vote
0
down vote













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.






share|improve this answer




























    up vote
    0
    down vote













    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?






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236346%2fhow-to-rename-and-save-files-in-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      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");
      }
      }
      }





      share|improve this answer





















      • Thank you for the your answer. After upload the file. how we can view the files ?
        – kole12
        Nov 10 at 6: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 at 7:04










      • I need to show as a link.
        – kole12
        Nov 10 at 7:19















      up vote
      0
      down vote



      accepted










      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");
      }
      }
      }





      share|improve this answer





















      • Thank you for the your answer. After upload the file. how we can view the files ?
        – kole12
        Nov 10 at 6: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 at 7:04










      • I need to show as a link.
        – kole12
        Nov 10 at 7:19













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      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");
      }
      }
      }





      share|improve this answer












      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");
      }
      }
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 10 at 6:34









      ryantxr

      2,6391520




      2,6391520












      • Thank you for the your answer. After upload the file. how we can view the files ?
        – kole12
        Nov 10 at 6: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 at 7:04










      • I need to show as a link.
        – kole12
        Nov 10 at 7:19


















      • Thank you for the your answer. After upload the file. how we can view the files ?
        – kole12
        Nov 10 at 6: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 at 7:04










      • I need to show as a link.
        – kole12
        Nov 10 at 7:19
















      Thank you for the your answer. After upload the file. how we can view the files ?
      – kole12
      Nov 10 at 6:59




      Thank you for the your answer. After upload the file. how we can view the files ?
      – kole12
      Nov 10 at 6:59




      1




      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 at 7:04




      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 at 7:04












      I need to show as a link.
      – kole12
      Nov 10 at 7:19




      I need to show as a link.
      – kole12
      Nov 10 at 7:19












      up vote
      0
      down vote













      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.






      share|improve this answer

























        up vote
        0
        down vote













        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.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 5:56









          ashiish.me

          440313




          440313






















              up vote
              0
              down vote













              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?






              share|improve this answer



























                up vote
                0
                down vote













                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?






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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?






                  share|improve this answer














                  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?







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 6:20

























                  answered Nov 10 at 6:07









                  Amir Mustafa

                  213




                  213






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236346%2fhow-to-rename-and-save-files-in-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini