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
how can I upload a text file and read its contents
Moderator: General Moderators
-
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
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();
?>
Re: how can I upload a text file and read its contents
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
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
Re: how can I upload a text file and read its contents
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.
http://www.webtoolkit.info/ajax-file-upload.html
that I have been able to tweak to do what I need to do. Thanks again.