Upload Progress

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!

Moderator: General Moderators

Post Reply
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Upload Progress

Post by zxkelxz »

So I've got my full upload code complete and usable. One problem I cannot figure out how to allow the user not to "close" the browser before the upload completes. I tried using a while loop with the file_exist function, but I had a few issues with it. Anyone willing to supply some information and hints on how I should go about doing this? I don't need a "progress bar" even though that would be awesome, just some code that lets the user understand upload is still in progress.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Upload Progress

Post by Neilos »

What does your upload code look like so far?
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Re: Upload Progress

Post by zxkelxz »

Code: Select all


<html>
  <head>
    <title>Uploading a File</title>
   
  </head>
  <body>


<?php

if ( isset( $_POST["sendFile"] ) ) {
  processForm();
} else {
  displayForm();
}

function processForm() {
  if ( isset( $_FILES["file"] ) and $_FILES["file"]["error"] == UPLOAD_ERR_OK ) {
	
     if ( !move_uploaded_file( $_FILES["file"]["tmp_name"], "Admin/" . basename( $_FILES["file"]["name"] ) ) ) {
      echo "<p>Sorry, there was a problem uploading that file.</p>" . $_FILES["file"]["error"] ;
    } else {
      displayUploading();
    }
  } else {
    switch( $_FILES["file"]["error"] ) {
      case UPLOAD_ERR_INI_SIZE:
        $message = "The file is larger than the server allows.";
        break;
      case UPLOAD_ERR_FORM_SIZE:
        $message = "The file is larger than the script allows.";
        break;
      case UPLOAD_ERR_NO_FILE:
        $message = "No file was uploaded. Make sure you choose a file to upload.";
        break;
      default:
        $message = "Please contact your server administrator for help.";
    }
    echo "<p>Sorry, there was a problem uploading that file. $message</p>";
  }
}

function displayForm() {
?>
 

    <form action="file_upload.php" method="post" enctype="multipart/form-data">
      <div style="width: 30em;">
       

        
        <input type="file" name="file" id="file" value="" />

        <div style="clear: both;">
          <input type="submit" name="sendFile" value="Upload File" />
        </div>

      </div>
    </form>
<?php
}

function displayUploading() {
?>
    <center><h4>Uploading Please Wait</h4></center>

		<?php
		//Maybe the code should go some where in here when this function is called, it least I'm hoping.
		echo "<center><img src='bar.gif'> <br></center>";
		$fileName = $_FILES["file"]["name"];
		
		
	
		?>
    
    
<?php
}
?>

  </body>
</html>
That's it.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Upload Progress

Post by Neilos »

I was thinking about this and I realised that maybe this is unable to be done in php.

The nature of php is that it runs on the server and only provides feedback to the user in the form of new pages etc...

Therefore you would only be able to do this in php by getting new POST data, ie refreshing the page, which will look horrible for the user.

I think therefore that your best option is to use AJAX or something similar. I don't know what anyone else thinks but I cannot see a clear way of doing this in php. I've never had to worry about uploading so far (however it is coming up in a project soon) so I am no expert but I think that this is what you are stuck with.
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Re: Upload Progress

Post by zxkelxz »

I mean you can get the FileSize from the users file then get the FileSize that resides on the server and compare them. I'm guessing a recursive function could come into play here with javascript....If I figure it out this way I'll be sure to post the code back for you and your project. I use Linux if this make any difference as well to you and your upcomming project.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Upload Progress

Post by Neilos »

If you would provide code I'd be grateful as this is almost identical to something that I'll be doing in the next few weeks.

Well AJAX would be your best bet I think. A quick google search gave tons of results;

http://www.webdavsystem.com/server/docu ... ogress_bar
http://blog.joshuaeichorn.com/archives/ ... r-updates/ couldn't find source but there is a demo, source cannot be far.
http://webdeveloperplus.com/jquery/mult ... ng-jquery/ a jQuery one, looks nice.

Also there is some commercial ones you might wanna check, example;

http://sibsoft.net/xupload.html

There is tons to look at, check them out.
Post Reply