使上传的文件获得随机名称不起作用

时间:2014-08-28 23:19:47

标签: php string random

所以我希望我上传的文件在文件名的末尾得到一个随机数,但是rand()函数不能正常工作

$post_image = rand($_FILES['image']['name'],1,100);
$image_tmp = rand($_FILES['image']['tmp_name'],1,100);
move_uploaded_file($image_tmp,"../img/$post_image");
$insert_query = "insert into posts
(post_title,post_date,post_author,post_image,post_keywords,post_content) values 
('$post_title','$post_date','$post_author','$post_image','$post_keywords','$post_content')";

我做错了什么?

1 个答案:

答案 0 :(得分:0)

文件名可以是myimage.jpg

您需要使用类似

之类的内容在扩展名之前添加随机数
$parts = pathinfo($_FILES['image']['name']);
$filename = $parts['filename'];
$extension = $parts['extension'];

$post_image = $filename . rand(1, 100) . '.' . $extension;

// image_tmp does not need a random number added.
$image_tmp = $_FILES['image']['tmp_name'];