How i can change the image name before uploading it to folder?

前端 未结 1 1556
忘了有多久
忘了有多久 2021-01-29 12:44

can someone please help i just need to change the image name before uploading it to folder. whatever user type in here:



        
相关标签:
1条回答
  • 2021-01-29 13:18

    I don't know what you are trying to do or why you are doing it, as you haven't provided full details of what you want to change to or why, but here is the code :

    <?php
    
    $filename = "YOUR_FILE_NAME";
    
    $target_dir = "photos/";
    $target_file = $target_dir . $filename;
    $uploadOk = 1;
    $imageFileType = strtolower(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)) {
       echo "Sorry, file already exists.";
       $uploadOk = 0;
    }
    
    if ($_FILES["fileToUpload"]["size"] > 5000000) {
       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.";
       }
    }
    
    Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
    ?>
    
    0 讨论(0)
提交回复
热议问题