php - 在php mysql中上传时重命名文件

标签 php mysql

我有一段简单的代码用于在 php/mysql 中上传文件,它运行良好。

$target = "uploads/clients/";
  $target = $target . basename( $_FILES['photo']['name']);

  $pic=($_FILES['photo']['name']);

  //Writes the photo to the server
  if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
  {
  $insertSQL = sprintf("INSERT INTO clients (img, `cname`) VALUES (%s, %s)",
                       GetSQLValueString($pic, "text"),
                       GetSQLValueString($_POST['cname'], "text"));

  mysql_select_db($database_thebest, $thebest);
  $Result1 = mysql_query($insertSQL, $thebest) or die(mysql_error());

现在我想要的是重命名该文件名(如果该文件名已存在),然后将重命名的文件名插入数据库

感谢您的回复

最佳答案

试试这个...

$upload_dir = "uploads/clients/";
$target = $upload_dir . basename( $_FILES['photo']['name']);
while(file_exists($target)){
    $new = time().rand();
    $target = $upload_dir . '_' . $new . '_' . basename( $_FILES['photo']['name']);
}

// rest of the code now...
$pic= $new . ($_FILES['photo']['name']);

关于php - 在php mysql中上传时重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086564/

相关文章:

mysql - 有没有一种简单的方法可以在 mysql 上调整成绩

php - 通过 CSV 将客户数据库导入 RDS (MySQL)

php - 处理 mysqli 准备好的语句时获取的替代方法?

php - Joomla 文章页面 : code to check published or not

php - 有没有关于 zend framework 2 的基本概述?

sql - 比较2张表,发现不存在 "id"

mysql - 将多条sql更新语句合二为一

MySQL VB.Net 连接问题

php - Mysqli_stmt_get_result 在 PHP 5.5.3 中返回 fatal error

php - mysql_fetch_assoc 未获取数据