Reputation: 449
I want to echo a message reading "file uploading" while the file is uploading but the message is only being echoed after it has finished uploading, is there another way of dong this?
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "file uploading";
header("location:somewhere.php?uploaded")
}
else echo "oops!";
Upvotes: 0
Views: 901
Reputation: 16495
First thing. The location header();
function should be L
(in upper case in Location:
) then, Always use absolute URI's inside a header(); like..
header("Location: the_location_url");
OK? Good!
Now, about displaying Loading..
you should use the sleep();
function to delay any ongoing process by entering number of seconds inside it, as show heresleep(05);
Now, about your code:
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "file uploading";
sleep(05);
header("Location: the_location_url")
}
else echo "oops!";
Upvotes: 1
Reputation: 14285
What you are encountering right now is the necessity to do something asynchronously. Synchronous is what you are doing right now, i.e. firing a new request and thereby uploading the picture, which only allows showing the message after the request.
You can upload the image in the background and display a loading gif or something like that on the page using AJAX and jQuery. This allows you to show the message while uploading.
For a good introduction to jQuery, check these videos from the new boston. Specifically for AJAX, check out episode 101 and following.
Upvotes: 3
Reputation: 59699
It only prints after the file is uploaded because that's when you're PHP script is executed, after the file is uploaded.
A solution is to use the PECL upload progress package, though this means installing an extension to PHP, which you may or may not be able to do with your host.
Upvotes: 3