我已经在 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."; 
    } 
 
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!