how can I upload a text file and read its contents

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
jaschulz
Forum Newbie
Posts: 8
Joined: Sun Feb 08, 2009 4:46 am

how can I upload a text file and read its contents

Post by jaschulz »

Can anyone point me to a simple script that will allow me to upload a txt file and read its contents into a <div>? The uploaded file need not and should not be saved on the server.

Thanks
Frank Smith
Forum Newbie
Posts: 7
Joined: Mon Mar 01, 2010 8:57 pm

Re: how can I upload a text file and read its contents

Post by Frank Smith »

Code: Select all

 
 
<?php
 function Main()
  {
    if ($_FILES)
    {
      if (@move_uploaded_file($_FILES['userfile']['tmp_name'], realpath('.') . '/' . $_FILES['userfile']['name']))
      {
        echo 'ok<br>';
        echo 'name: ', $_FILES['userfile']['name'], '<br>';
        echo 'type: ', $_FILES['userfile']['type'], '<br>';
        echo 'tmp_name: ', $_FILES['userfile']['tmp_name'], '<br>';
        echo 'error: ', $_FILES['userfile']['error'], '<br>';
        echo 'size: ', $_FILES['userfile']['size'];
      }
    }
    else
    {
      $strHtml = '<form ENCTYPE="multipart/form-data" method="post" action="index.php">' .
                 '<input type="file" name="userfile">' .
                 '<input type="submit" value="Upload">' .
                 '</form>';
      echo $strHtml;
    }
  }
 
  Main();
?>
 
 
 
jaschulz
Forum Newbie
Posts: 8
Joined: Sun Feb 08, 2009 4:46 am

Re: how can I upload a text file and read its contents

Post by jaschulz »

OK I can make that work. But, if I weren't such a beginner, I would have explained more clearly that what I really want to do is have the server send the contents of the file back to the (javascript) client (as a string)-- after which the file can be deleted from the server.

From what I can gather, the right strategy is to get the file contents via an AJAX call, but I don't understand how the client knows when the upload has finished, and how the client gets the location and temp file name to use in the AJAX call?

Any help would be appreciated.

Thanks
jaschulz
Forum Newbie
Posts: 8
Joined: Sun Feb 08, 2009 4:46 am

Re: how can I upload a text file and read its contents

Post by jaschulz »

Thanks to everyone in this thread. I found something at:

http://www.webtoolkit.info/ajax-file-upload.html

that I have been able to tweak to do what I need to do. Thanks again.
Post Reply