★ wanayoo — archive 1999 http://www.php.net/manual/ja/function.fread.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to ファイルシステム
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
ファイルシステム
* basename
* chgrp
* chmod
* chown
* clearstatcache
* copy
* delete
* dirname
* diskfreespace
* fclose
* feof
* fgetc
* fgetcsv
* fgets
* fgetss
* file
* file_exists
* fileatime
* filectime
* filegroup
* fileinode
* filemtime
* fileowner
* fileperms
* filesize
* filetype
* flock
* fopen
* fpassthru
* fputs
* fread
* fscanf
* fseek
* fstat
* ftell
* ftruncate
* fwrite
* set_file_buffer
* is_dir
* is_executable
* is_file
* is_link
* is_readable
* is_writeable
* link
* linkinfo
* mkdir
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* touch
* umask
* unlink
Manual: fread
View the source code for this pageSearch the site



Previous page
 fputs
 Updated
Sat, 12 Aug 2000
fscanf 
Next page


fread

(PHP3 , PHP4 )

fread -- バイナリ・モードでファイルを読み込む

説明

string fread (int fp, int length)

fread()fpが指す ファイルポインタから最高lengthバイト 読み込みます。読み込みは、lengthバイト分 読み込まれたか、EOFに達したかのいずれか早い方の事象により中止 されます。


// ファイルの中身を読んで文字列に格納する
$filename = "/usr/local/something.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
      

fwrite(), fopen(), fsockopen(), popen(), fgets(), fgetss(), fscanf(), file(), fpassthru() も参照下さい。


User Contributed Notes: fread


"Elliot English"<webmaster@mlplus.com>
17-Aug-1999 12:44
make it so that the command does not require a length specifier instead it reads the whole file. Also make it so that in a third property it can read froma specified position.


studioq@chorus.net
29-Aug-1999 07:37
Yes you can read an entire file with fread(). Just use the string "filesize" as an argument. Like this:
$body = fread($fhandle,filesize($textfile));
Be sure to open the file with fopen() and close it with fclose(). Else massive memory use will result.



sRp@portico.org
05-Sep-1999 06:50
The above doesn't work for url based files though, which would be a very handy thing to have. (not the filesize, but the grabbing everything with one fread.)


ben1@atlantys.net
11-Sep-1999 06:15
I want make a script of comparison for a login and a pass for enter in a Member Page. But when I add most of one login and one pass the script doesn't found the first logib and pass or the new login and pass ... How can I put a lot of login and pass ? (it's on two file : login.txt and pass.txt)
I put my script here for know if It's wrong.
$filename="login.txt"
$filename2="pass.txt"
$fd=fopen("$filename","r")
$login2=fread($fd,filesize($filename))
$fd2=fopen("filename2","r")
$pass2=fread($fd2,filesize($filename2))
if(strstr($login,"$login2")) {
if(strstr($pass,"$pass2")) {
echo "Welcome to the Member Page"
} else {
echo "Bad Login Go Back"
}
} else {
echo "Bad Login Go Back"
}



ray7th@teleport.com
09-Nov-1999 02:18
I am trying to use the fread() function to read an XML-like file, sort of a homegrown parser. The files and code samples are below.
The output is stripped of the "tags" I had planned to use to parse for and grab the values between. When I change the file name to a olumnar-
formatted text file, the output is has white spaces truncated, and appears as it should be. HOW CAN I AVOID THE TAG STRIPPING USING fread()?

>>> SAMPLE CODE below:
$CongressXml = "congress.xml";
if ( file_exists ( $CongressXml ) ) {
$fp = fopen ( $CongressXml, 'r' ) or die( "Cannot open congress.xml" );
$buffer = fread( $fp, filesize( $CongressXml ) );
fclose( $fp );
$LenOfBuffer = StrLen( $buffer );
$iEndPos = 0;
$iStartPos = 0;
} else {
echo "Missing congress.xml";
}

>>> congress.xml sample below:
<REPRESENTATIVE>
<STATE>AK</STATE>
<FULLNAME>Alaska</FULLNAME>
<LASTNAME>Young</LASTNAME>
<FIRSTNAME>Don</FIRSTNAME>
<DISTRICT>AL</DISTRICT>
<PARTY>R</PARTY>
<EMAIL>Don.Young@mail.house.gov</EMAIL>
</REPRESENTATIVE>

>>> Sample output from fread() of congress.xml:
AK Alaska Young Don AL R Don.Young@mail.house.gov AL Alabama
Callahan Sonny 1 R sonny.callahan@mail.house.gov AL Alabama Everett Terry 2
R Terry.Everett@mail.house.gov AL Alabama Riley Bob 3 R

>>> congressemails.txt sample below:
AK R Stevens, Ted Senator_Stevens@stevens.senate.gov
*AK R Murkowski, Frank http://murkowski.senate.gov/webmail.html
AL R Sessions, Jeff senator@sessions.senate.gov
AL R Shelby, Richard senator@shelby.senate.gov

>>> Sample output from fread() of congressemails.txt:
AK R
Stevens, Ted Senator_Stevens@stevens.senate.gov *AK R Murkowski, Frank
http://murkowski.senate.gov/webmail.html AL R Sessions, Jeff
senator@sessions.senate.gov AL R Shelby, Richard senator@shelby.senate.gov
AR D Lincoln, Blanche blanche_lincoln@lincoln.senate.gov AR R Hutchinson,



jeichorn@mail.com
02-Dec-1999 01:36
For url based files try using implode for example
$string = implode("\n", file("http://www.foobar.com"));



ethanh@pushgroup.net
14-Jan-2000 06:49
For reading from a specified point in the file, use:
<pre>
$fp=fopen("$filename","r");
fseek($fp,$numpoint);
$content=fread($fp,filesize("$filename"));
fclose($fp);
</pre>
For doing the same with an url-based file, use jeichorn@mail.com's solution and <pre>substr</pre> it.



nv@mindimage.net
03-Feb-2000 03:54
how do i extract a specified position, like i am opening /etc/namedb/header and want to extract the Serial to $ser can anyone help me what would be the best way todo that ?


waldemar.moll@mail.terranet.de
23-Feb-2000 06:40
Does anyone know a solution for reading a binary file and convert bytes 48 up to 51 (4 Bytes) into a decimal number?
Rembering that decimal numbers are stored in the order: LowByte - Highbyte which means that the hex number 00000C48 ist stored 48 0c 00 00.
Thanks for every hint!



kirbyt@yahoo.com
01-Apr-2000 08:31
Use -1 as the length arg if you want to read until EOF. This work for remote reads also.


shaolin_p@pi.be
23-Apr-2000 07:00
If I write somethins to a file from a form, then it writes on every line. But when I want to read the file and display it, all the text is shown in one line, how can I display the file like it really is?


zorttroz78@hotmail.com
27-Apr-2000 10:25
Could somebody write an example of how to find out the number of bytes in an URL-based file?

For example, I want to find out if a file is empty or if it has characters in it. If the file is not empty, I would like to output the file. If the file is empty (or what I believe is a byte size of 1) then do nothing.



joe@koontz.net
19-Jul-2000 09:57
try this:
<PRE>
&lt;?
$fp = fopen("file.x","r");
$buff = fread($fp,1000000);
Header("Content-Type: application/x-octet-stream");
// you can put the header file-length here but I never use it!
Header( "Content-Disposition: attachment; filename=evaluation.fdf");
echo $buff;
?>
</PRE>
this will trigger the download action.
joe



 About Notes


Previous page
 fputs
 Updated
Sat, 12 Aug 2000
fscanf 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.