0

I'm trying to add a redirection to a page after the file finishes uploading, however I've tried a few methods unsuccessfully. I'd like the user to be redirected to a specific page or to display the success message on a popup. Guidance will be greatly appreciate it. This is what I have:

$target_dir = "galleries/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
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;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} 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.";
    }
}
Yarseo
  • 47
  • 5

1 Answers1

0

in the last part of your code in the else block, you can add a header and it will be redirected to the url given

....    
else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            header('Location: http://google.com/'); // after the upload user will be redirected to the page

        } 
        ....
    } 
Shobi
  • 10,374
  • 6
  • 46
  • 82
  • It didn't work. I'm getting this message in the error_log [25-Feb-2019 15:04:12 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/admin/upload.php:40) in /home/public_html/admin/upload.php on line 41 – Yarseo Feb 25 '19 at 15:06
  • that means, somewhere, most probably on upload.php line 40, you have used some kind of output statements, such as echo or print. You can fix that and try again. it shoudl work, more info on header already sent https://stackoverflow.com/a/8028987/2693543 – Shobi Feb 25 '19 at 15:25
  • I'll check it out. Thanks! – Yarseo Feb 26 '19 at 01:58