2014-08-28 66 views
-2

所以我想我的uploded文件,以获得在文件名的最后一个随机数,但rand()函数不工作制作uploded文件中获得随机名称不工作

$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')"; 

我在做什么错误?

+3

兰特正常工作,你只是没有刻意去阅读手册关于如何正确使用它。它接受无参数或2个整数。没有一个字符串和两个整数 – 2014-08-28 23:22:30

+0

我怎样才能使它工作,我是新的PHP我需要另一个变量? – user3684958 2014-08-28 23:26:55

+0

感觉就像你在问错误的问题。你想要做什么/防止什么? – PeeHaa 2014-08-28 23:28:10

回答

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'];