在php中随机重命名相同的上传个人资料图片?

3

我已经制作了一个用php实现的图片上传脚本,在上传图片时可以成功上传,但现在如果用户再次上传相同的个人资料图片,我希望能够将其随机更名。我使用了rand(1,100000)来实现这一功能,但是遇到错误。

我的脚本如下:

<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
 $_FILES['file']['name']=rand(1,100000).$_FILES['file']['name'];
    $uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 600000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
 
 if (is_file($target_file))
{
echo "Successful<BR/>";
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>";
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>";
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>";
echo "<img src=\"$target_file\" width=\"150\" height=\"150\">";
}
}
?>

我得到了以下错误: 在此输入图片描述
4个回答

2

像这样在此函数中进行更改。

if (file_exists($target_file)) {
    $target_file = $target_dir .rand(1,100000). basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
}

Windows 8.1 目标目录正确,当我上传新图片时,它会正确地上传。 - Rebecca Joanna
那么现在的问题是什么? - Hardik Paghdar
请执行 echo $target_file; 命令,并告诉我输出结果。我想知道为什么数字在上传前会添加 88627upload - Archish
你好,能否推荐一些有关提高 PHP 概念的书籍或网站?谢谢。 - Rebecca Joanna
请阅读以下网站。 http://php.net/manual/zh/ http://www.w3schools.com/php/default.asp - Hardik Paghdar
显示剩余2条评论

1

改变

if (file_exists($target_file)) {
 $_FILES['file']['name']=rand(1,100000).$_FILES['file']['name'];
    $uploadOk = 0;
}

to
if (file_exists($target_file)) {
 $_FILES['fileToUpload']['tmp_name']=rand(1,100000).$_FILES['fileToUpload']['tmp_name'];
    $uploadOk = 0;
}


1
现在它显示以下错误:文件是一个图像 - image/jpeg。对不起,您的文件未上传。 - Rebecca Joanna

1
你需要将你的代码从以下内容进行更改:

if (file_exists($target_file)) {
 $_FILES['file']['name']=rand(1,100000).$_FILES['file']['name'];
    $uploadOk = 0;
}

to

if (file_exists($target_file)) {
    $target_file = $target_dir . rand(1,100000) . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 0;
}

1
使用以下代码:
if (file_exists($target_file)) {
 $_FILES['fileToUpload']['tmp_name']=rand(1,100000).$_FILES['fileToUpload']['tmp_name'];
    $uploadOk = 0;
} 

已经完成,谢谢。我尝试了这个方法,但是出现了错误:“文件是图像 - image/jpeg。抱歉,您的文件未上传。” - Rebecca Joanna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接