PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
hey so i have a working PHP code for a upload form, but i wana make a progress bar for it. so like if it's a pretty big file, people will know how much longer it'l take. here is my current code:
<?php
$bad_types = array('application/octet-stream','application/x-msdos-program','application/x');
if( in_array( $_FILES['uploadedfile']['type'], $bad_types ) )
{
echo "That File type is not supported.";
}
else
{
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded. here is the link to your file: <a href=uploads/". basename( $_FILES['uploadedfile']['name']). ">". basename( $_FILES['uploadedfile']['name'])."</a>";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
If move_uploaded_file() could take the source file as a small chunk and we could loop till the file size and pass the small chunk to the function and display the progress bar. But move_uploaded_file() function takes the source files and do it all and return just a boolean value. I think php.net community should make the function parameters customizable .
PHP processes on the server. Status bars report on the client. Unless you plan on sending requests to the server during different stages of the upload (which I don't think is possible) you are going to have to use a client side solution. This is probably a job for AJAX or some other DHTML type of technology.
dibyendrah wrote:If move_uploaded_file() could take the source file as a small chunk[...]
move_uploaded_file already works on the temporary file already created on the server's local filesystem, i.e. the actual upload is finished when move_uploaded_file is invoked.
I always throw this solution out there just because it's easy... you could just display an animated gif that says "Loading..." and maybe some sort of loading-type animation. This way at least the user sees that SOMETHING is happening.
I posted that as a follow-up joke on your line. but seriously if you ask me, then I would change the line a bit, may be to 'Please wait while your file is being uploaded...' and animating something beyond that to achieve the same effect you were talking about. Afterwards, if loading fails then you can show the error to the user — "Your File was not uploaded because <insert reason here>".
feyd wrote:We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
This is about as good an explanation as you can expect. Several of us have posted on this subject before. PHP will not take care of status bars. AJAX can, JavaScript can, client-side technologies can. Server-side technologies will not do this for you.
feyd wrote:We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
Perl?! Really how? What does Perl have that PHP doesn't!
ole wrote:Perl?! Really how? What does Perl have that PHP doesn't!
Uh ... Larry Wall ... and the ability to monitor uploading files.
The reason it cannot be done in PHP is that PHP is currently unable to comminicate to the submitting script any sort of handle or identifier to access the temporary file. It therefor cannot monitor the upload or pass the info to a monitoring script. With PHP you have to wait until the receiving script runs to check the uploaded file -- and by that time the file is already uploaded.
There was/is a patch for PHP that adds this feature. There are also a long running series of requests and patches to add this functionality to PHP -- one by the author of that patch. None have been accepted so far that I know of. I have not followed it for a while so do know if there are any plans in 6.0 for this.