Jump to content

How to refresh form data


brucehohl

Recommended Posts

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'] .

              "&nbsp 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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.