我希望修改下面的代码以接受随机生成的唯一ID来使用php uniqid函数替换名称。我尝试了几种方法却没有成功。
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$ipath = "upload/";
$ipath .= $_FILES["file"]["name"];
有谁知道我怎么能做到这一点?
编辑:我问在哪里放置uniqid函数,以便将文件与uniqid.extension一起存储在我服务器上的文件夹中。
答案 0 :(得分:8)
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . uniqid());
或者,如果你想保留文件扩展名:
$fileInfo = pathinfo($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . uniqid() . '.' . $fileInfo['extension']);
\\ edit:使用$ ipath变量...
$fileInfo = pathinfo($_FILES["file"]["name"]);
$ipath = "upload/" . uniqid() . '.' . $fileInfo['extension'];
move_uploaded_file($_FILES["file"]["tmp_name"], $ipath);
答案 1 :(得分:1)
$newName = md5_file($ipath);
将生成文件内容的md5,应该是非常独特的。
答案 2 :(得分:-2)
$aaa = uniqid();
$Image_New_Name = $aaa."_".basename($_FILES["fileToUpload"]["name"] );
$target_dir = "upload/".$Image_New_Name;
if(isset($_POST['submit'])) {
//$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"] );
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir )) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]) ." has been uploaded.";
echo "<br/>";
}
$id = $_POST['id'];
$fullname = $_POST['fullname'];
$password = $_POST['password'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$about_me = $_POST['about_me'];
$state = $_POST['state'];
$country = $_POST['country'];
if($_FILES["fileToUpload"]["name"] != "") {
$image = $_FILES["fileToUpload"]["name"];
} else {
$image='BlankFaceGraphic.jpg';
}
$sql="insert into register(id,fullname,password,email,gender,about_me,state,country,image)
values
('$id','$fullname','$password','$email','$gender','$about_me','$state','$country','$Image_New_Name')";