Page 1 of 1

text file

Posted: Wed Apr 13, 2005 4:03 am
by snicolas
I need to be able to read the content of a text file and display this content into a text area.
Now, I have a script working that allows a user to upload a .txt file onto the server and I successfully read this content and redisplay it.

I was wondering if it is possible to do the same without uploading the file to the server.
1/User load his file using a browse button
2/Script read the file content and redisplay it on the page?

Sorry if it has been previously covered , but I am stuck.

s

Posted: Wed Apr 13, 2005 4:19 am
by Wayne
if you are talking about reading the text from the file without uploading the file to the server at all, then you will need to use a scripting language like javascript. This needs to be moved to the client side forum!

Posted: Wed Apr 13, 2005 4:23 am
by Chris Corbyn
Sure is....

Instead of reading the file contents from a directory you give it, you read the file from:

$_FILES['form_field_name']['tmp_name']

instead (That's it's temporary location (and it will/should be removed when the sript is done with it)).

So

Code: Select all

$contents = file_get_contents($_FILES['form_field_name']['tmp_name']);

echo nl2br($contents);

Posted: Wed Apr 13, 2005 4:25 am
by Chris Corbyn
Wayne wrote:if you are talking about reading the text from the file without uploading the file to the server at all, then you will need to use a scripting language like javascript. This needs to be moved to the client side forum!
Microsoft do have some support for working with text files in JS but it's non standard and I'm not sure how well it actually works. Just allow it be uploaded but don't save it... like I showed.

Posted: Wed Apr 13, 2005 4:47 am
by snicolas
d11wtq wrote:Sure is....

Instead of reading the file contents from a directory you give it, you read the file from:

$_FILES['form_field_name']['tmp_name']

instead (That's it's temporary location (and it will/should be removed when the sript is done with it)).

So

Code: Select all

$contents = file_get_contents($_FILES['form_field_name']['tmp_name']);

echo nl2br($contents);
Thanks D11.
I should have thought of something like.
Now I get a open_basedir restriction in effect error, as the temp file is uploaded in C:\PHP\uploadtemp\ and my files are in a different folder.

s

Posted: Wed Apr 13, 2005 5:31 am
by snicolas
Ok managed to read the file properly now.
I have a new question.
I upload a text file containing a list of email address.
How can i check that each email address is in a correct format?

s

Posted: Wed Apr 13, 2005 6:05 am
by Chris Corbyn
snicolas wrote:Ok managed to read the file properly now.
I have a new question.
I upload a text file containing a list of email address.
How can i check that each email address is in a correct format?

s
These are my favourite questions.... regexp...

The preg_...() functions will help but if you post an exmaple of the text file I'll give you some pointers ;-)

Posted: Wed Apr 13, 2005 6:11 am
by n00b Saibot
d11wtq wrote:These are my favourite questions.... regexp...
Lol :lol: Boy! do you seem happy LOL