这是我将图像上传到FTP服务器及其名称到Mysql db:
的代码session_start();
require 'config2.php';
require_once 'user.class.php';
$target = "sent/";
$target1 = "sent/";
$target2 = "sent/";
$target3 = "sent/";
$target4 = "sent/";
$target5 = "sent/";
$target = $target . basename( $_FILES['photo']['name']);
$target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = $target2 . basename( $_FILES['photo2']['name']);
$target3 = $target3 . basename( $_FILES['photo3']['name']);
$target4 = $target4 . basename( $_FILES['photo4']['name']);
$target5 = $target5 . basename( $_FILES['photo5']['name']);
//This gets all the other information from the form
$login = $_SESSION['login'];
$name=$_POST['name'];
$name1 =$_POST['name1'];
$name2 =$_POST['name2'];
$name3 =$_POST['name3'];
$name4 =$_POST['name4'];
$name5 =$_POST['name5'];
$pic=($_FILES['photo']['name']);
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
$pic4=($_FILES['photo4']['name']);
$pic5=($_FILES['photo5']['name']);
$id=$_SESSION['id'];
// Connects to your Database
//$op = mysql_query("select id from users where id = '$id' ");
//Writes the information to the database
//mysql_query("INSERT INTO users (name, photo, photo1, name1) VALUES ('$name', '$pic' ,'$pic1', '$name1')") ;
mysql_query("UPDATE users SET name='$name', name1='$name1', name2='$name2', name3='$name3', name4='$name4', name5='$name5', photo = '$pic', photo1 = '$pic1', photo2 = '$pic2', photo3 = '$pic3', photo4 = '$pic4', photo5 = '$pic5' WHERE login = '$login' ");
$allowed = array('jpg', 'jpeg', 'gif', 'png');
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'] ,$target) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
}
if(move_uploaded_file($_FILES['photo1']['tmp_name'] ,$target1) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo1']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
}
if(move_uploaded_file($_FILES['photo2']['tmp_name'] ,$target2) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo2']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
}
if(move_uploaded_file($_FILES['photo3']['tmp_name'] ,$target3) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo3']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
}
if(move_uploaded_file($_FILES['photo4']['tmp_name'] ,$target4) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo4']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
}
if(move_uploaded_file($_FILES['photo5']['tmp_name'] ,$target5) )
{
//Tells you if its all ok
echo "Plik ". basename( $_FILES['photo5']['name'] ). " uploaded successfuly";
echo implode(', ', $allowed);
工作正常。但是现在我想添加用于分配新名称的代码,如果有任何用户上传了具有相同名称的图像。因为当两个或多个图像的名称相同时,脚本将覆盖较旧的图像。那么,你能看到这个问题的任何解决方案吗?
答案 0 :(得分:0)
为文件名添加时间戳。
$target = $target . time().basename( $_FILES['photo']['name']);
$target1 = $target1 . time().basename( $_FILES['photo1']['name']);
$target2 = $target2 . time().basename( $_FILES['photo2']['name']);
$target3 = $target3 . time().basename( $_FILES['photo3']['name']);
$target4 = $target4 . time().basename( $_FILES['photo4']['name']);
$target5 = $target5 . time().basename( $_FILES['photo5']['name']);
答案 1 :(得分:0)
只需更改目标文件的名称即可。这是您设置这些名称的地方:
$target = $target . basename( $_FILES['photo']['name']);
$target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = $target2 . basename( $_FILES['photo2']['name']);
$target3 = $target3 . basename( $_FILES['photo3']['name']);
$target4 = $target4 . basename( $_FILES['photo4']['name']);
$target5 = $target5 . basename( $_FILES['photo5']['name']);
您可以将这些变量设置为您想要的任何字符串。例如,如果要创建全局唯一的文件名,则可以使用GUID或某些unique ID作为整个文件名或附加到文件名。 您想要的“唯一性”的衡量取决于您。但最终您的代码显式设置目标文件名,因此您可以显式将该值设置为您想要的任何值。
答案 2 :(得分:0)
我已经尝试过,这成功了。
$target = time().( $_FILES['photo']['name']);
例如。
// Example of accessing data for a newly uploaded file
$fileName = time().$_FILES["pic"]["name"];
$fileTmpLoc = $_FILES["pic"]["tmp_name"];
time()
此函数将重命名具有相同名称的文件,并保留用户上传的另一个相同文件。希望这会有所帮助。感谢