0

Actually i want change the filename after i have insert the DB ,i tried but while inserting the value that time i changed but while moving the file is i can't change file name,

I want to change the filename afeter i want move to one folder,inserting that value in DB

if (isset($_FILES['file'])) {
  move_uploaded_file(
      $_FILES['file']['tmp_name'],
      "upload/" . $_FILES['file']['name']
  );

  // here i got original filename,but i wnt change the filename
  $filename = $_FILES['file']['name'];
  $extension = pathinfo($filename, PATHINFO_EXTENSION);

  // so here i changed filename but inserting in this file name and while in originalfilename,how to change that original filename
  $original_file = md5($filename) . time() . rand(10, 1000) . '-' .$extension;

  $sql = mysql_query("INSERT INTO useralbum(photo) VALUES('$original_file')");
  if ($sql) {
    echo "SUCCESS";
  } else {
    echo "ERROR".mysql_error();
  }
}
Pavlin
  • 5,390
  • 6
  • 38
  • 51
Raja R
  • 21
  • 6
  • 2
    checkout this : http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory – Happy Coding Nov 26 '15 at 08:21

2 Answers2

0

You have to rename it first then upload it -

$filename =$_FILES['file']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$original_file =md5($filename).time().rand(10,1000).'.'.$extension; // Rename
move_uploaded_file($_FILES['file']['tmp_name'],"upload/".$original_file); // Upload
// rest of the code
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0
move_uploaded_file($_FILES['file']['tmp_name'],"upload/".$_FILES['file']['name']);

This function move the file into upload folder. So if you want to change the file name, just chage

$_FILES['file']['name']

to another name. Ex :

move_uploaded_file($_FILES['file']['tmp_name'],"upload/[YOUR_FILE_NAME]");
ZenithS
  • 987
  • 8
  • 20