0

I have two PHP files.

index.php allows users to upload an image. The uploaded data is sent to another page i.e. upload.php, where I check certain conditions for example if the uploaded image is really an image and not some other file and if the image already exists on the server etc;

I want to see that my upload.php page redirects back to the index.php page in false conditions. I have seen many of the posts on redirecting, e.g., by using header, which I did. Well, It does redirect. However, there is some part of javascript code, which does not execute. I want to see that alert box on my upload.php page, which upon user interaction redirects to index.php.

Alert box does not appear at the moment, but the page does redirect

My question might be very trivial, but I am a PHP newbie.

index.php

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

upload.php

$target_dir = "uploads/";
$file_name=basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . $file_name;
$uploadOk = 1;

if (file_exists($target_file)) {
    $uploadOk = 0;
    echo "<script language='javascript'>
            alert('Sorry, file already exists.Consider Renaming or upload new file');
            </script>";
    header("Location: http://xx.yyy.zz");
    exit;
}
3
  • 1
    Your requirement makes no sense. The whole point of a redirect command is that it tells the browser to ignore the current output and go to the newly specified URL instead. When exactly would you be expecting this alert to appear, during that process? The browser redirects the moment it reads the header, it doesn't wait for any other info. If you want to display something as the result of the current script, and then redirect, you'd have to do a client-side navigation using Javascript's window.location command, which you would run after the alert is closed. Commented Jun 7, 2021 at 11:48
  • 1
    But actually it would be much more user-friendly to simply display any errors on the page where the redirect goes to, then the user doesn't have to remember them after they've dismissed the alert. You could put the error messages into the session perhaps, or pass error codes as querystring variables in the redirect URL, so the receiving page would know what error message(s) to display, from a pre-determined list. Commented Jun 7, 2021 at 11:49
  • @ADyson Thanks for the valuable feedback Commented Jun 7, 2021 at 20:38

1 Answer 1

1

You can replace the 'HTTP header redirect' with a redirection done in javascript.

Remove the code

header("Location: http://xx.yyy.zz");

And replace the echo command with

echo "<script language='javascript'>
        alert('Sorry, file already exists.Consider Renaming or upload new file');
        window.location = "http://xx.yyy.zz";
        </script>";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.