Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a text file into a script

Status
Not open for further replies.

Bjelleklang

Programmer
Sep 2, 2003
8
NO
How can you read the contents of a text file into a script???

I tried to read it using fread, but got an error message saying that i didn't use a valid File-Handle resource on the fread line.
 
u have to use fopen before reading/writing to a file
Code:
$filename = "test.txt" ;
$FH = fopen($filename,"r") ; // opens a file in read mode
$content = fread($FH,1000) ;

U can have a look at for modes a file can be open with.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I get the whole using fopen to read the text in a file, but what if the file I want to read is not located on my server - what if the file i want to read is a file that a user is uploading with a previous page's <input type=&quot;file&quot; form?

Say I upload &quot;afile.txt&quot; using a HTML form, and in the processing page, I want to take the text in the newly uploaded file and use it in a script with fopen.

How to access it?

Thanks...
 
<?php
if (@is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

$up_file=$HTTP_POST_FILES['userfile']['tmp_name'];

$realfile=$HTTP_POST_FILES['userfile']['name'];

@$fcontents=file($up_file);
// work on th efile line by line

while (list ($line_num, $line) = each ($fcontents)) {

// do your stuff

echo &quot;[ $line_num ] $line<br>&quot;;

}
//end the if - create a form to upload a file cos we dont have one yet

}else{
echo &quot;
<form enctype=\&quot;multipart/form-data\&quot; action=\&quot;$PHP_SELF\&quot; method=\&quot;POST\&quot;>
<table width=\&quot;100%\&quot; border=\&quot;0\&quot; cellspacing=\&quot;0\&quot; cellpadding=\&quot;0\&quot; align=\&quot;center\&quot;>
<tr>
<td>
<div align=\&quot;center\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;MAX_FILE_SIZE\&quot; value=\&quot;10000000\&quot;>
<font color=\&quot;white\&quot;>
</font>
<input name=\&quot;userfile\&quot; type=\&quot;file\&quot;><br>
<br>
<input type=\&quot;submit\&quot; value=\&quot;Send File\&quot; name=\&quot;submit\&quot;>
</div>
</td>
</tr>
</table>
</form>
&quot;;
}

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
and not an fopen in sight .... :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
So, the question is really not about reading a file but handling an uploaded file. The uploading process instantiates a filehandle for you, that's why there is no fopen() necessary. The code
Code:
$fcontents=file($up_file);
reads the contents into a variable.

but what if the file I want to read is not located on my server
When a file is uploaded it is on your server. The Web server loads the file into a temporary location that is defined in the server configuration.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top