Php 使上传文件获取随机名称无效
因此,我希望上传的文件在文件名的末尾获得一个随机数,但rand()函数不起作用Php 使上传文件获取随机名称无效,php,string,random,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,pos
$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')";
我做错了什么?文件名可能类似于
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'];
兰德公司的工作很好,你只是不必费心去阅读手册如何正确使用它。它要么不接受参数,要么接受2个整数。不是一个字符串和两个整数我怎么能让它工作呢,我是php新手,我需要另一个变量吗?感觉你问错了问题。你想做什么/阻止什么?当我的图像上传并移动到另一个文件夹时,我试图添加一些随机数。