我有一段简单的代码用于在 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/