★ wanayoo — archive 1999 http://fr.php.net/manual/fi/function.file.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousfgetssfile_existsnext
Last updated: Tue, 28 May 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Slovak | Spanish | Swedish | Turkish

file

(PHP 3, PHP 4 >= 4.0.0)

file -- Reads entire file into an array

Description

array file ( string filename [, int use_include_path])

Identical to readfile(), except that file() returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached.

Huomaa: Each line in the resulting array will include the line ending, so you still need to use trim() if you do not want the line ending present.

You can use the optional second parameter and set it to "1", if you want to search for the file in the include_path, too.

<?php
// get a web page into an array and print it out
$fcontents = file ('http://www.example.com/');
while (list ($line_num, $line) = each ($fcontents)) {
    echo "<b>Line $line_num:</b>; ", htmlspecialchars ($line), "<br>\n";
}

// get a web page into a string
$fcontents = implode ('', file ('http://www.example.com/'));
?>

Huomaa: As of PHP 4.3.0 you can use file_get_contents() to return the contents of a file as a string in a binary safe manner.

Varoitus

Tämä funktio ei (vielä) osaa käsitellä binäärimuotoista dataa! (esim. kuvia)

Vihje: Tämän funktion kanssa voit käyttää myös URLia tiedostonimen sijaan jos "fopen wrappers" on kytketty päälle. Katso myös fopen().

See also readfile(), fopen(), fsockopen(), and popen().

User Contributed Notes
file
add a note about notes
php at do_not_spam_me_pierreseguin dot ca
05-Feb-2002 10:00

The file() function doesn't recognize Macintosh line breaks as delimiters. You'll need to convert them to UNIX newlines characters before using this function.
php@don\'t_spam_me
09-Feb-2002 07:56

It appears that the file() function causes file access problems for perl cgi scripts accessing the same files.  I am using Perl v5.6.0 in linux with PHP/4.0.4pl1.  After running a php app using the file() function, any perl cgi trying to access the same file randomly dies returning an internal server error: premature end of script headers.

The simple fix is to use fopen(), fgets() and fclose() instead of file().

sjohnson at fuzzygroup dot com
14-Mar-2002 02:32

Here is a simple way to find the size of an html page that is a url, not a file:

$myurl = "http://www.fuzzygroup.com/";
$content_array = file($myurl);
$content = implode("", $content_array);
$mysize = strlen($content);

print $mysize;

andrea at brancatelli dot it
16-Mar-2002 06:16

file() has a strange behaviour when reading file with both \n and \r as line delimitator (DOS files), since it will return an array with every single line but with just a \n in the end. It seems like \r just disappears.

This is happening with PHP 4.0.4 for OS/2. Don't know about the Windows version.

philip at cornado dot com
02-May-2002 10:27

file() works fine on macs (\r) as of PHP 4.3.0  Here's the bug report:

http://bugs.php.net/bug.php?id=16521

andre at urbanbuffalo dot com
28-May-2002 04:31

You can always directly insert a carriage return and line feed:

chr(13).chr(10)

Seems to work fine for me. Remember to use them together as some text readers wont recognise these separately. e.g. chr(13) (carriage return) in some cases will display an invalid character at the point where you wanted a new line. Adding chr(10) solves this.

I find it handy to make it a global var.

I know it's small but hopefully this helps someone who has to develop script for a few different platforms. It helps me.

Cheers.

e dot maccarthy at csuohio dot edu
02-Jun-2002 10:04

Here is a quick snippet that will read in N number of lines of a file, then print them.

$n=10
$fp = file(/path/to/your/file);
$i=0
while($i < $n){
          echo "$fp[$i]";
           $i++;
}

I am using this right now to display the current progress of the seti@home client working on my server, instead of displaying the whole thing, which isn't web page friendly.
Because sometimes short really is sweet...

senserarray at hotmail dot com
08-Jun-2002 03:14

A binary-safe file() :

function bs_file($filename) {
$fp = @fopen($filename, "r");
if (!($fp)) {
return 0;
}
while (!feof($fp)) {
$temp .= fread($fp, 4096);
}
$arr = explode("\n", $temp);
return $arr;
}

But too bad no \n attached :(

omar dot speranza at argomedia dot it
07-Sep-2002 01:52

need a random line stamp?
useful for humour stories :-)

<?
$file = file("/path/to/your/file");

mt_srand ((double) microtime() * 1000000);
$line = mt_rand(0,count($file));

echo $file[$line];
?>

jens at schriver dot com
11-Nov-2002 12:39

Note that file does not work over https.
19-Nov-2002 01:04
If you have formated a text file using spaces, the function file(and readfile too) deletes them end let only one space between words.
add a note about notes
previousfgetssfile_existsnext
Last updated: Tue, 28 May 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Nov 23 04:10:11 2002 CET