Home > Mobile >  Random file name & delayed redirect after upload
Random file name & delayed redirect after upload

Time:05-08

I need to add two functions to this code. The first one is to make each file upload with a random file name. I would assume '/ids/somethinggoeshere' The second one is after successful upload I would like the user to be redirected to a different page after 3 seconds or so, maybe index.php for example. Any assistance would be appreciated. Thanks in advance.

  <?php 

    // check the file is uploaded or not
    if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {   
    
        // Determine the file location
        $newname = dirname(__FILE__) . '/ids/' .basename($_FILES['attachment']['name']);
    
        if($_FILES['attachment']['size'] > 5000000) {
            $errors[]='File size must be under 5 MB';
        }
        
        // Check Allowed File Types
        $file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
        $extensions= array("pdf","jpg","jpeg","png","gif");
        if(in_array($file_ext,$extensions)=== false){
            $errors[]="File extension not allowed, please choose a pdf or jpg file.";
        }
        
        if(empty($errors)==true){
            // Move the file from temporary location to determined location
            if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
                echo "<p>ERROR:  A problem occurred during file upload!</p>\n";
            } else {
                echo "<p>Your Document has been Uploaded</p>\n";
            }
        }
        else{
            print_r($errors);
        }
  }
  

 ?>

CodePudding user response:

Many ways to do it. But i would use a hash for randomising Filename. For delaying response you can use the PHP funktion sleep();.

Hash: hash('md5', uniquid(), false);

sleep() sleep(2); // sleep 2 sec.`

CodePudding user response:

  <?php 

    // check the file is uploaded or not
    if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {   
    
        // Determine the file location
        $newname = dirname(__FILE__) . '/ids/'.time() . rand(11, 99)  .basename($_FILES['attachment']['name']);
    
        if($_FILES['attachment']['size'] > 5000000) {
            $errors[]='File size must be under 5 MB';
        }
        
        // Check Allowed File Types
        $file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
        $extensions= array("pdf","jpg","jpeg","png","gif");
        if(in_array($file_ext,$extensions)=== false){
            $errors[]="File extension not allowed, please choose a pdf or jpg file.";
        }
        
        if(empty($errors)==true){
            // Move the file from temporary location to determined location
            if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
                echo "<p>ERROR:  A problem occurred during file upload!</p>\n";
            } else {
                echo "<p>Your ID Document has been Uploaded</p>\n";
            }
        }
        else{
            print_r($errors);
        }
        
          {
echo "<meta http-equiv='refresh' content='5;URL=index.php'>";
}
    }
  
   ?>
  •  Tags:  
  • php
  • Related