brucehohl Posted February 1, 2009 Share Posted February 1, 2009 Hello All, I have the following bit of code which I use to list and update some files. After a new file is uploaded I want the file list to update. Can anyone tell me how to do this? Thanks, BH <?php // How to get form file list to refresh immediately after file add? //============================================================================= // Page header and main page logic print <<<_HTML_ <b>Test File Management Script</b><br> <a href=".">To Menu</a><br><br> _HTML_; $file_location = "/var/www/php/scripts_test/file_area/"; $file_list = get_file_list($file_location); upload_file($file_location); display_files($file_location, $file_list); //============================================================================= // Function: to upload a file function upload_file($file_location) { print <<<_HTML_ File to upload:<br> <form action="$_SERVER[php_SELF]" method="POST" enctype="multipart/form-data"> <input type="file" name="up_file" size="30"> <input type="submit" value="Upload"> </form> _HTML_; if(isset($_FILES['up_file']['name'])) { echo "Uploading: ".$_FILES['up_file']['name']."<br>"; $tmpName = $_FILES['up_file']['tmp_name']; $newName = $file_location . $_FILES['up_file']['name']; if(!is_uploaded_file($tmpName) || !move_uploaded_file($tmpName, $newName)) { echo "FAILED TO UPLOAD " . $_FILES['up_file']['name'] . "  Temporary Name: $tmpName<br><br>"; } else { echo "File has been uploaded.<br>"; echo "<br><br>"; } } } //============================================================================= // Function: to get file list function get_file_list($directory) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $file_list[] = $file; } } closedir($handle); } sort($file_list); return $file_list; } //============================================================================= // Function: to display contents of upload directory function display_files($file_location,$file_list) { print "Contents of: $file_location"; print "<br>"; foreach ($file_list as $choice) { print("<a href=\"../scripts_test/file_area/$choice\"> ".$choice."</a><br />\n"); } } ?> Link to comment https://forums.phpfreaks.com/topic/143336-how-to-refresh-form-data/ Share on other sites More sharing options...
jscix Posted February 1, 2009 Share Posted February 1, 2009 if your wanting the file list to update without reloading the page, you'l need to use AJAX. You setup your script that reads the files to output them in html list format, and print them to the page. From your form, on submit, or whatever event handler you want -- make an ajax request to that script Link to comment https://forums.phpfreaks.com/topic/143336-how-to-refresh-form-data/#findComment-751767 Share on other sites More sharing options...
brucehohl Posted February 1, 2009 Author Share Posted February 1, 2009 Thanks for that reply. I am not familiar with AJAX. Is there a way PHP/HTML way to get new file list to display? I don't care if a reload / refresh is required but all my attempts along those lines failed. Link to comment https://forums.phpfreaks.com/topic/143336-how-to-refresh-form-data/#findComment-751773 Share on other sites More sharing options...
brucehohl Posted February 1, 2009 Author Share Posted February 1, 2009 I solved the problem by splitting the script into two scripts - input_form_script and action_script. After adding a file the user goes to the action_script. Upon return to the input_form_script the file list updates. This is adequate but what I really wanted was a single script. Link to comment https://forums.phpfreaks.com/topic/143336-how-to-refresh-form-data/#findComment-751953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.