PHP upload files and give a custom name












3















I am using this function to upload my files:



if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}


How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?



Thanks!










share|improve this question


















  • 1





    Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

    – Marc B
    Aug 23 '11 at 14:31
















3















I am using this function to upload my files:



if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}


How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?



Thanks!










share|improve this question


















  • 1





    Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

    – Marc B
    Aug 23 '11 at 14:31














3












3








3








I am using this function to upload my files:



if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}


How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?



Thanks!










share|improve this question














I am using this function to upload my files:



if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}


How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?



Thanks!







php upload






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 23 '11 at 13:57









DiegoP.DiegoP.

18.4k2779100




18.4k2779100








  • 1





    Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

    – Marc B
    Aug 23 '11 at 14:31














  • 1





    Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

    – Marc B
    Aug 23 '11 at 14:31








1




1





Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

– Marc B
Aug 23 '11 at 14:31





Don't use the ['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.

– Marc B
Aug 23 '11 at 14:31












3 Answers
3






active

oldest

votes


















7














The only line you need modified in your code is:



$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);


Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.



You whole code would be:



if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}





share|improve this answer





















  • 1





    +1 to it. It is recommended the use of pathinfo function.

    – diosney
    Aug 23 '11 at 14:13





















1














$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;





share|improve this answer































    0














    I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:



    $parts = explode('.', $_FILES['Artwork']['name']);
    $newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')





    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',
      autoActivateHeartbeat: false,
      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%2f7162157%2fphp-upload-files-and-give-a-custom-name%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









      7














      The only line you need modified in your code is:



      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);


      Where 'CustomName.' is the new name you want for the image.
      pathinfo if the PHP function to handle the operations with paths and files names.



      You whole code would be:



      if ((($_FILES["Artwork"]["type"] == "image/gif")
      || ($_FILES["Artwork"]["type"] == "image/jpeg")
      || ($_FILES["Artwork"]["type"] == "image/jpg")
      || ($_FILES["Artwork"]["type"] == "image/pjpeg"))
      && ($_FILES["Artwork"]["size"] < 20000000))
      {
      if ($_FILES["Artwork"]["error"] > 0)
      {
      //echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
      }else{
      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
      move_uploaded_file($_FILES["Artwork"]["tmp_name"],
      $path_image . $imageName);
      }
      }else{
      //echo "invalid file";
      }





      share|improve this answer





















      • 1





        +1 to it. It is recommended the use of pathinfo function.

        – diosney
        Aug 23 '11 at 14:13


















      7














      The only line you need modified in your code is:



      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);


      Where 'CustomName.' is the new name you want for the image.
      pathinfo if the PHP function to handle the operations with paths and files names.



      You whole code would be:



      if ((($_FILES["Artwork"]["type"] == "image/gif")
      || ($_FILES["Artwork"]["type"] == "image/jpeg")
      || ($_FILES["Artwork"]["type"] == "image/jpg")
      || ($_FILES["Artwork"]["type"] == "image/pjpeg"))
      && ($_FILES["Artwork"]["size"] < 20000000))
      {
      if ($_FILES["Artwork"]["error"] > 0)
      {
      //echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
      }else{
      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
      move_uploaded_file($_FILES["Artwork"]["tmp_name"],
      $path_image . $imageName);
      }
      }else{
      //echo "invalid file";
      }





      share|improve this answer





















      • 1





        +1 to it. It is recommended the use of pathinfo function.

        – diosney
        Aug 23 '11 at 14:13
















      7












      7








      7







      The only line you need modified in your code is:



      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);


      Where 'CustomName.' is the new name you want for the image.
      pathinfo if the PHP function to handle the operations with paths and files names.



      You whole code would be:



      if ((($_FILES["Artwork"]["type"] == "image/gif")
      || ($_FILES["Artwork"]["type"] == "image/jpeg")
      || ($_FILES["Artwork"]["type"] == "image/jpg")
      || ($_FILES["Artwork"]["type"] == "image/pjpeg"))
      && ($_FILES["Artwork"]["size"] < 20000000))
      {
      if ($_FILES["Artwork"]["error"] > 0)
      {
      //echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
      }else{
      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
      move_uploaded_file($_FILES["Artwork"]["tmp_name"],
      $path_image . $imageName);
      }
      }else{
      //echo "invalid file";
      }





      share|improve this answer















      The only line you need modified in your code is:



      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);


      Where 'CustomName.' is the new name you want for the image.
      pathinfo if the PHP function to handle the operations with paths and files names.



      You whole code would be:



      if ((($_FILES["Artwork"]["type"] == "image/gif")
      || ($_FILES["Artwork"]["type"] == "image/jpeg")
      || ($_FILES["Artwork"]["type"] == "image/jpg")
      || ($_FILES["Artwork"]["type"] == "image/pjpeg"))
      && ($_FILES["Artwork"]["size"] < 20000000))
      {
      if ($_FILES["Artwork"]["error"] > 0)
      {
      //echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
      }else{
      $imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
      move_uploaded_file($_FILES["Artwork"]["tmp_name"],
      $path_image . $imageName);
      }
      }else{
      //echo "invalid file";
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 23 '11 at 14:13

























      answered Aug 23 '11 at 14:04









      leticialeticia

      1,88652435




      1,88652435








      • 1





        +1 to it. It is recommended the use of pathinfo function.

        – diosney
        Aug 23 '11 at 14:13
















      • 1





        +1 to it. It is recommended the use of pathinfo function.

        – diosney
        Aug 23 '11 at 14:13










      1




      1





      +1 to it. It is recommended the use of pathinfo function.

      – diosney
      Aug 23 '11 at 14:13







      +1 to it. It is recommended the use of pathinfo function.

      – diosney
      Aug 23 '11 at 14:13















      1














      $ext = last(explode('.', $_FILES['Artwork']['name']));
      $custom_name = 'something';
      $imageName = $custom_name.'.'.$ext;





      share|improve this answer




























        1














        $ext = last(explode('.', $_FILES['Artwork']['name']));
        $custom_name = 'something';
        $imageName = $custom_name.'.'.$ext;





        share|improve this answer


























          1












          1








          1







          $ext = last(explode('.', $_FILES['Artwork']['name']));
          $custom_name = 'something';
          $imageName = $custom_name.'.'.$ext;





          share|improve this answer













          $ext = last(explode('.', $_FILES['Artwork']['name']));
          $custom_name = 'something';
          $imageName = $custom_name.'.'.$ext;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 23 '11 at 14:00









          yodayoda

          6,844155888




          6,844155888























              0














              I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:



              $parts = explode('.', $_FILES['Artwork']['name']);
              $newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')





              share|improve this answer




























                0














                I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:



                $parts = explode('.', $_FILES['Artwork']['name']);
                $newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')





                share|improve this answer


























                  0












                  0








                  0







                  I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:



                  $parts = explode('.', $_FILES['Artwork']['name']);
                  $newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')





                  share|improve this answer













                  I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:



                  $parts = explode('.', $_FILES['Artwork']['name']);
                  $newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 23 '11 at 14:01









                  Aleks GAleks G

                  43.2k18126195




                  43.2k18126195






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7162157%2fphp-upload-files-and-give-a-custom-name%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings