After upload how do I stay on the same page?

Discussion in 'PHP' started by chrisj, Oct 6, 2014.

  1. #1
    This Form successfully uploads, but then takes me to the next page.
    I'd like some assistance with how to stay on the same page, after upload, please.
    Any help will be appreciated.

    
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = strtolower( end($temp) );
    if (  $_FILES["file"]["size"] < 2000000 && in_array($extension, $allowedExts)){
    if($_FILES["file"]["error"]!= 0){
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }else{
    $length = 20;
    $randomString = substr(str_shuffle(md5(time())),0,$length);
    $newfilename = $randomString . "." . $extension;
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename );
    $file_location = '<a href="http://www.---.com/upload/' . $newfilename . '">http://www.---.com/upload/' . $newfilename . '</a>';
    }
    }else{
    echo "Invalid upload file";
    }
    
    PHP:
    
    <form enctype="multipart/form-data" action="" method="POST">
    <input type="file" name="file" id="file">
    <input class="upload-video-form-input" type="hidden" name="form_submitted" value="yes" />
    <input type="submit" value="submit" />
    </form>
    
    HTML:

     
    Last edited: Oct 6, 2014
    chrisj, Oct 6, 2014 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    javascript/jquery handler for uploads! :) or start uploading in a new window target="_blank" or something :)
     
    EricBruggema, Oct 6, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    As long as both the PHP and HTML are in the same file (ie, your upload-handler is in the same file as the form), you shouldn't see any redirect - the code you posted above doesn't redirect anywhere, and the action-attribute is empty (which it shouldn't be, either omit it completely, if you're using HTML 5, or do action="#" if not) - hence there's nowhere for the script to go - unless you're not posting the actual code for us to see.
     
    PoPSiCLe, Oct 7, 2014 IP
  4. darkvisje

    darkvisje Well-Known Member

    Messages:
    231
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #4
    If you have a form you can easly, its just a fast copy past:

    if(isset($_POST['upload'])){

    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $uploadDir = '../balbalba/';
    $filePath = $uploadDir . $fileName;
    $result = move_uploaded_file($tmpName, $filePath);
    }

    <form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" >
     
    darkvisje, Oct 15, 2014 IP
  5. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thank you
     
    chrisj, Oct 15, 2014 IP