提问者:小点点

如何更改php[复制]中的文件名


实际上,我想在插入数据库后更改文件名,我尝试过,但在插入值时,我更改了,但在移动文件时,我无法更改文件名,

我想在移动到一个文件夹后更改文件名,并在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();
  }
}

共2个答案

匿名用户

你必须先重命名它,然后再上传它-

$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

匿名用户

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

此函数将文件移动到上传文件夹。所以如果你想更改文件名,只需chage

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

换个名字。前任:

move_uploaded_file($_FILES['file']['tmp_name'],"upload/[YOUR_FILE_NAME]");