text file

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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

text file

Post 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
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

d11wtq wrote:These are my favourite questions.... regexp...
Lol :lol: Boy! do you seem happy LOL
Post Reply