Jump to content

Import .txt directly to <textarea>


soma56

Recommended Posts

I hope someone can help me out with this. What I would like to do is import a text file dircetly to a text area. I can't seem to find any examples on this (Yes I did search Google). I know how to upload but I'm not interested in storing files on the server - but rather just importing them directly to a textarea.

 

Can anyone point me in the right direction?

 

I can upload:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 

I can read:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w')

 

But I'm not interested in either of these as they are unnecessary steps (or are they?). Is there not a way to simply browse/import a text file directly to a textarea  (or anywhere for that matter) without having to actually save the file to the server?

 

Something like this:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 

//uploader.php

if (isset($_POST['submit'])) {

        $content = $_POST['"grab the contents of the file"'];
        echo "<textarea>";
        echo $content;
        echo "</textarea>";
}

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/222845-import-txt-directly-to/
Share on other sites

Can you use file_get_contents?

 

Yes, I suppose I could. I was trying to avoid it. Here's the traditional logic:

 

> Browse and upload file to server

> Grab contents from uploaded file

 

This is what I want to do:

 

> Browse and upload contents of file to textarea

 

Is this possible?

Can't just do an include?

 

<form enctype="multipart/form-data" action="" method="POST">
Upload: <input name="file" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<?
$my_file = $_FILES["file"]["tmp_name"];
if (file_exists($my_file)) {
include($my_file);
} else {
echo "No file to display";
}
?>

This would work better because can do returns for each line.

 

<form enctype="multipart/form-data" action="" method="POST">
Upload: <input name="file" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<?
$my_file = $_FILES["file"]["tmp_name"];
if (file_exists($my_file)) {
$data = file($my_file);
$total = count($data);
echo "<br />Total lines: $total<br />";
foreach ($data as $line) {
echo "$line<br />";
}
} else {
echo "No file to display";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.