943,694 Members | Top Members by Rank

Ad:
Jul 3rd, 2009
0

Update div for filelisting after file upload using ajax

Expand Post »
I'm using a script called Ajax Upload to upload files. I have a div that has a php script inside so that when the page loads I can see the listing of all the files. My problem is that I want to update this list of files without having to refresh the page.

My code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
  3. <head profile="http://gmpg.org/xfn/11">
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5.  
  6. <script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
  7. <script type="text/javascript" src="js/ajaxupload.3.5.js" ></script>
  8. <script type="text/javascript" src="js/prototype-1.6.0.3.js"></script>
  9. <script type="text/javascript" >
  10. $(function(){
  11. var btnUpload=$('#upload');
  12. var status=$('#status');
  13. new AjaxUpload(btnUpload, {
  14. action: 'upload-file.php',
  15. name: 'uploadfile',
  16. onSubmit: function(file, ext){
  17. /*if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
  18.   // extension is not allowed
  19. status.text('Only JPG, PNG or GIF files are allowed');
  20. return false;
  21. }*/
  22. status.text('Uploading...');
  23. },
  24. onComplete: function(file, response){
  25. //On completion clear the status
  26. status.text('');
  27. //Add uploaded file to list
  28. if(response==="success"){
  29. $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
  30. } else{
  31. $('<li></li>').appendTo('#files').text(file).addClass('error');
  32. }
  33. }
  34. });
  35.  
  36. });
  37. new Ajax.Updater('fileList', 'fileList.php', {parameters: { text: $F('text') }});
  38. </script>
  39.  
  40. <link rel="stylesheet" type="text/css" href="./styles.css" />
  41. </head>
  42.  
  43. <body>
  44. <div id="main" >
  45.  
  46.  
  47. <div class="content">
  48.  
  49. <?php include 'uploader.php';?>
  50.  
  51. </div>
  52.  
  53. </div>
  54.  
  55. </body>
and the uploader.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="fileListing">
  2. <div class="fileHeader">
  3. Your files
  4. </div>
  5. <div class="fileList" id="fileList">
  6. <?php include 'fileList.php';?>
  7. </div>
  8. </div>
  9. <div class="uploaded">
  10. <!-- Upload Button, use any id you wish-->
  11. <!--<div id="upload" ><span>Upload File<span></div>-->
  12. <input type="button" value="Upload" id="upload"/>
  13. <div id="status" ></div>
  14. </div>
and fileList.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. $dir = "uploads"; //You could add a $_GET to change the directory
  3.  
  4.  
  5. function getDirectoryTree( $outerDir){
  6. $dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
  7. $dir_array = Array();
  8. foreach( $dirs as $d ){
  9. if( is_dir($outerDir."/".$d) ) $dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d );
  10. else $dir_array[ $d ] = $d;
  11. }
  12. return $dir_array;
  13. }
  14. function getFileList($tree,&$subLevel,$dir){
  15. $itemNumber = 0;
  16. $itemCount = count($tree);
  17. foreach($tree as $key => $item){
  18. $itemNumber++;
  19.  
  20. if(!is_array($item)){
  21.  
  22. echo $subLevel.'<span style="margin-left:'.($subLevel*15).'px;">'.$item.'</span><br/>';
  23. if($itemNumber == $itemCount){
  24. --$subLevel;
  25. }
  26. }
  27. else{
  28. if(is_dir($dir."/".$key))
  29. {
  30. $subLevel = substr_count($dir."/".$key,"/");
  31. }
  32. echo $key.'<br/>';
  33. unset($tree[$key]);
  34. array_push($tree,$item);
  35. getFileList($item,$subLevel,$dir."/".$key);
  36. }
  37.  
  38.  
  39. }
  40. }
  41. $tree = getDirectoryTree($dir);
  42. $subLevel = 0;
  43. getFileList($tree,$subLevel,$dir);
  44. /*$files = scandir($dir);
  45. foreach($files as $key => $value){
  46. if(substr($value,0,1) != '.')
  47. echo "<a href='./uploads/$value' title='Download $value'>".$value."</a><br/>";
  48. //You could add an icon in here maybe, a link to the file/directory, or a link to list files in that directory
  49. }*/

Thanks for any help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Magicianer is offline Offline
12 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Reload a element with swf
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: FAVOURITE & HOME ICON on WEB PAGE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC