Rename file on upload PHP











up vote
0
down vote

favorite












I am trying to build a script to upload and rename an image to a folder and store the path in my sql db.



Here is where I am at: The file get uploaded both to the folder and the pathname to the db but I cannot figure out how to rename the filename. Ideally I would like to make the filename unique so I don't duplicates.



<?php   
//preparing the patch to copy the uploaded file
$target_path = "upload/";

//adding the name of the file, finishing the path
$target_path = $target_path . basename( $_FILES['picture']['name']);

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

//getting input from the form
$name = $_POST['name'];
$description = $_POST['description'];

//preparing the query to insert the values
$query = "INSERT INTO complete_table (name, description, picture) VALUES ('$name', '$description', '". $target_path ."')";

//opening connection to db
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>


I am new with all this and I am really trying but after looking at many tutorial and answered questions on Stack-overflow, I realized I needed help. Thank you in advance for helping this newbie.










share|improve this question


















  • 1




    You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
    – Jessica
    Dec 27 '13 at 14:40










  • You need to rename $_FILES['picture']['name']
    – putvande
    Dec 27 '13 at 14:40










  • Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
    – the_arthemis
    Dec 27 '13 at 14:51










  • @Jessica, I hope you are not too disappointed by my attempts.
    – the_arthemis
    Dec 27 '13 at 15:51















up vote
0
down vote

favorite












I am trying to build a script to upload and rename an image to a folder and store the path in my sql db.



Here is where I am at: The file get uploaded both to the folder and the pathname to the db but I cannot figure out how to rename the filename. Ideally I would like to make the filename unique so I don't duplicates.



<?php   
//preparing the patch to copy the uploaded file
$target_path = "upload/";

//adding the name of the file, finishing the path
$target_path = $target_path . basename( $_FILES['picture']['name']);

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

//getting input from the form
$name = $_POST['name'];
$description = $_POST['description'];

//preparing the query to insert the values
$query = "INSERT INTO complete_table (name, description, picture) VALUES ('$name', '$description', '". $target_path ."')";

//opening connection to db
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>


I am new with all this and I am really trying but after looking at many tutorial and answered questions on Stack-overflow, I realized I needed help. Thank you in advance for helping this newbie.










share|improve this question


















  • 1




    You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
    – Jessica
    Dec 27 '13 at 14:40










  • You need to rename $_FILES['picture']['name']
    – putvande
    Dec 27 '13 at 14:40










  • Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
    – the_arthemis
    Dec 27 '13 at 14:51










  • @Jessica, I hope you are not too disappointed by my attempts.
    – the_arthemis
    Dec 27 '13 at 15:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to build a script to upload and rename an image to a folder and store the path in my sql db.



Here is where I am at: The file get uploaded both to the folder and the pathname to the db but I cannot figure out how to rename the filename. Ideally I would like to make the filename unique so I don't duplicates.



<?php   
//preparing the patch to copy the uploaded file
$target_path = "upload/";

//adding the name of the file, finishing the path
$target_path = $target_path . basename( $_FILES['picture']['name']);

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

//getting input from the form
$name = $_POST['name'];
$description = $_POST['description'];

//preparing the query to insert the values
$query = "INSERT INTO complete_table (name, description, picture) VALUES ('$name', '$description', '". $target_path ."')";

//opening connection to db
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>


I am new with all this and I am really trying but after looking at many tutorial and answered questions on Stack-overflow, I realized I needed help. Thank you in advance for helping this newbie.










share|improve this question













I am trying to build a script to upload and rename an image to a folder and store the path in my sql db.



Here is where I am at: The file get uploaded both to the folder and the pathname to the db but I cannot figure out how to rename the filename. Ideally I would like to make the filename unique so I don't duplicates.



<?php   
//preparing the patch to copy the uploaded file
$target_path = "upload/";

//adding the name of the file, finishing the path
$target_path = $target_path . basename( $_FILES['picture']['name']);

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

//getting input from the form
$name = $_POST['name'];
$description = $_POST['description'];

//preparing the query to insert the values
$query = "INSERT INTO complete_table (name, description, picture) VALUES ('$name', '$description', '". $target_path ."')";

//opening connection to db
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>


I am new with all this and I am really trying but after looking at many tutorial and answered questions on Stack-overflow, I realized I needed help. Thank you in advance for helping this newbie.







php file upload renaming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '13 at 14:37









the_arthemis

4717




4717








  • 1




    You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
    – Jessica
    Dec 27 '13 at 14:40










  • You need to rename $_FILES['picture']['name']
    – putvande
    Dec 27 '13 at 14:40










  • Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
    – the_arthemis
    Dec 27 '13 at 14:51










  • @Jessica, I hope you are not too disappointed by my attempts.
    – the_arthemis
    Dec 27 '13 at 15:51














  • 1




    You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
    – Jessica
    Dec 27 '13 at 14:40










  • You need to rename $_FILES['picture']['name']
    – putvande
    Dec 27 '13 at 14:40










  • Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
    – the_arthemis
    Dec 27 '13 at 14:51










  • @Jessica, I hope you are not too disappointed by my attempts.
    – the_arthemis
    Dec 27 '13 at 15:51








1




1




You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
– Jessica
Dec 27 '13 at 14:40




You need to show what you've tried, and explain what didn't work. Renaming a file in PHP is something you could very easily google, where are you stuck?
– Jessica
Dec 27 '13 at 14:40












You need to rename $_FILES['picture']['name']
– putvande
Dec 27 '13 at 14:40




You need to rename $_FILES['picture']['name']
– putvande
Dec 27 '13 at 14:40












Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
– the_arthemis
Dec 27 '13 at 14:51




Hello Jessica, I did google it and the closest I got to a solution was using this documentation php.net/manual/en/function.move-uploaded-file.php but I could only and text after the extension of the image like "upload/nameofimage.pnghereistheextratext" which was not helpful in my case. I also tried creating a new variable with extra parameter and it only resulted in sql errors.
– the_arthemis
Dec 27 '13 at 14:51












@Jessica, I hope you are not too disappointed by my attempts.
– the_arthemis
Dec 27 '13 at 15:51




@Jessica, I hope you are not too disappointed by my attempts.
– the_arthemis
Dec 27 '13 at 15:51












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Well, this is where you set the name of the file being saved:



$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['picture']['name']);


In this case, you're building the file name in the variable $target_path. Just change that to something else. What you change it to is up to you. For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. Something like:



$target_path = "upload/";
$target_path = $target_path . uniqid();


Note that this would essentially throw away the existing name of the file and replace it entirely. If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well.






share|improve this answer





















  • this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
    – the_arthemis
    Dec 27 '13 at 14:58










  • @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
    – David
    Dec 27 '13 at 15:00










  • do you mean I need a separate column in my db to store the type?
    – the_arthemis
    Dec 27 '13 at 15:14










  • @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
    – David
    Dec 27 '13 at 15:17










  • Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
    – the_arthemis
    Dec 27 '13 at 15:50


















up vote
1
down vote













First get the file extension:



$file_extension = strrchr($uploaded_file_name, ".");


Then rename the uploaded file with a unique id + file extension



$uploaded_file_name = uniqid() . $file_extension;


Example:



enter image description here



TIP: Save the original file name and other information in a database.






share|improve this answer






























    up vote
    0
    down vote













    First get the extension with pathinfo()



    Then create your unique name:



    $name = 'myname'.uniqid();


    Then rename your file.



    $target_path = $target_path . $name.$ext);


    move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ).






    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%2f20802504%2frename-file-on-upload-php%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Well, this is where you set the name of the file being saved:



      $target_path = "upload/";
      $target_path = $target_path . basename( $_FILES['picture']['name']);


      In this case, you're building the file name in the variable $target_path. Just change that to something else. What you change it to is up to you. For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. Something like:



      $target_path = "upload/";
      $target_path = $target_path . uniqid();


      Note that this would essentially throw away the existing name of the file and replace it entirely. If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well.






      share|improve this answer





















      • this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
        – the_arthemis
        Dec 27 '13 at 14:58










      • @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
        – David
        Dec 27 '13 at 15:00










      • do you mean I need a separate column in my db to store the type?
        – the_arthemis
        Dec 27 '13 at 15:14










      • @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
        – David
        Dec 27 '13 at 15:17










      • Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
        – the_arthemis
        Dec 27 '13 at 15:50















      up vote
      1
      down vote



      accepted










      Well, this is where you set the name of the file being saved:



      $target_path = "upload/";
      $target_path = $target_path . basename( $_FILES['picture']['name']);


      In this case, you're building the file name in the variable $target_path. Just change that to something else. What you change it to is up to you. For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. Something like:



      $target_path = "upload/";
      $target_path = $target_path . uniqid();


      Note that this would essentially throw away the existing name of the file and replace it entirely. If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well.






      share|improve this answer





















      • this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
        – the_arthemis
        Dec 27 '13 at 14:58










      • @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
        – David
        Dec 27 '13 at 15:00










      • do you mean I need a separate column in my db to store the type?
        – the_arthemis
        Dec 27 '13 at 15:14










      • @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
        – David
        Dec 27 '13 at 15:17










      • Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
        – the_arthemis
        Dec 27 '13 at 15:50













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      Well, this is where you set the name of the file being saved:



      $target_path = "upload/";
      $target_path = $target_path . basename( $_FILES['picture']['name']);


      In this case, you're building the file name in the variable $target_path. Just change that to something else. What you change it to is up to you. For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. Something like:



      $target_path = "upload/";
      $target_path = $target_path . uniqid();


      Note that this would essentially throw away the existing name of the file and replace it entirely. If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well.






      share|improve this answer












      Well, this is where you set the name of the file being saved:



      $target_path = "upload/";
      $target_path = $target_path . basename( $_FILES['picture']['name']);


      In this case, you're building the file name in the variable $target_path. Just change that to something else. What you change it to is up to you. For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. Something like:



      $target_path = "upload/";
      $target_path = $target_path . uniqid();


      Note that this would essentially throw away the existing name of the file and replace it entirely. If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 27 '13 at 14:43









      David

      144k26143207




      144k26143207












      • this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
        – the_arthemis
        Dec 27 '13 at 14:58










      • @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
        – David
        Dec 27 '13 at 15:00










      • do you mean I need a separate column in my db to store the type?
        – the_arthemis
        Dec 27 '13 at 15:14










      • @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
        – David
        Dec 27 '13 at 15:17










      • Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
        – the_arthemis
        Dec 27 '13 at 15:50


















      • this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
        – the_arthemis
        Dec 27 '13 at 14:58










      • @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
        – David
        Dec 27 '13 at 15:00










      • do you mean I need a separate column in my db to store the type?
        – the_arthemis
        Dec 27 '13 at 15:14










      • @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
        – David
        Dec 27 '13 at 15:17










      • Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
        – the_arthemis
        Dec 27 '13 at 15:50
















      this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
      – the_arthemis
      Dec 27 '13 at 14:58




      this works pretty good but it also kills the extension of the file... will that be a problem when I retrieve the path to display the image?
      – the_arthemis
      Dec 27 '13 at 14:58












      @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
      – David
      Dec 27 '13 at 15:00




      @the_arthemis: Not for displaying on the web, no. File extensions don't actually mean anything. As long as you're sending the correct Content-Type header when displaying the image, the browser will know what to do with it. When the file is uploaded, it should contain the type here: $_FILES['picture']['type'], which you should store in the database with the other information about the file.
      – David
      Dec 27 '13 at 15:00












      do you mean I need a separate column in my db to store the type?
      – the_arthemis
      Dec 27 '13 at 15:14




      do you mean I need a separate column in my db to store the type?
      – the_arthemis
      Dec 27 '13 at 15:14












      @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
      – David
      Dec 27 '13 at 15:17




      @the_arthemis: That would be advisable, that way you'll be able to provide the browser with the file type when you serve the file later.
      – David
      Dec 27 '13 at 15:17












      Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
      – the_arthemis
      Dec 27 '13 at 15:50




      Well I don't know how I did it but I now get files renamed with '[uniqueid][originalname].[extension]' That said I have no clue how it happened.
      – the_arthemis
      Dec 27 '13 at 15:50












      up vote
      1
      down vote













      First get the file extension:



      $file_extension = strrchr($uploaded_file_name, ".");


      Then rename the uploaded file with a unique id + file extension



      $uploaded_file_name = uniqid() . $file_extension;


      Example:



      enter image description here



      TIP: Save the original file name and other information in a database.






      share|improve this answer



























        up vote
        1
        down vote













        First get the file extension:



        $file_extension = strrchr($uploaded_file_name, ".");


        Then rename the uploaded file with a unique id + file extension



        $uploaded_file_name = uniqid() . $file_extension;


        Example:



        enter image description here



        TIP: Save the original file name and other information in a database.






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          First get the file extension:



          $file_extension = strrchr($uploaded_file_name, ".");


          Then rename the uploaded file with a unique id + file extension



          $uploaded_file_name = uniqid() . $file_extension;


          Example:



          enter image description here



          TIP: Save the original file name and other information in a database.






          share|improve this answer














          First get the file extension:



          $file_extension = strrchr($uploaded_file_name, ".");


          Then rename the uploaded file with a unique id + file extension



          $uploaded_file_name = uniqid() . $file_extension;


          Example:



          enter image description here



          TIP: Save the original file name and other information in a database.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 11 '14 at 15:58

























          answered Jul 11 '14 at 15:39









          Porta Shqipe

          57657




          57657






















              up vote
              0
              down vote













              First get the extension with pathinfo()



              Then create your unique name:



              $name = 'myname'.uniqid();


              Then rename your file.



              $target_path = $target_path . $name.$ext);


              move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ).






              share|improve this answer



























                up vote
                0
                down vote













                First get the extension with pathinfo()



                Then create your unique name:



                $name = 'myname'.uniqid();


                Then rename your file.



                $target_path = $target_path . $name.$ext);


                move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ).






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  First get the extension with pathinfo()



                  Then create your unique name:



                  $name = 'myname'.uniqid();


                  Then rename your file.



                  $target_path = $target_path . $name.$ext);


                  move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ).






                  share|improve this answer














                  First get the extension with pathinfo()



                  Then create your unique name:



                  $name = 'myname'.uniqid();


                  Then rename your file.



                  $target_path = $target_path . $name.$ext);


                  move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday









                  Rust

                  3,95451944




                  3,95451944










                  answered Dec 27 '13 at 14:44









                  Jorge Faianca

                  771510




                  771510






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20802504%2frename-file-on-upload-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python