PHP/File Directory/Text File Read
Содержание
Display XML file
<?php
$xml_string = file_get_contents("contact.xml","rb");
echo $xml_string;
?>
Read file content with only one command
<html>
<head>
<title>Tip of the day</title>
</head>
<body>
<center>
<h1>Tip of the day</h1>
<div style = "border-color:green; border-style:groove; border-width:2px">
<?
readfile("tips.txt");
?>
</div>
</center>
</body>
</html>
Read in and perform operations on a file line by line
<?
$fd = fopen("test.txt", "r");
while(!feof($fd)) {
$line = fgets($fd, 4096);
if(strcmp($line,$targetline) == 0) {
echo "A match was found!";
}
}
fclose($fd);
?>
Read text file into an array in one function
<?
$line_array = file("test.txt");
echo $line_array[0];
?>
Read text file into array and output
<?php
$userfile= file_get_contents("./text.txt");
// Place each line of $userfile into array
$users = explode("\n",$userfile);
foreach ($users as $user) {
list($name, $email) = explode(" ", $user);
echo "<a href=\"mailto:$email\">$name/a> <br />";
}
?>
Read text file with format
<?php
$fh = fopen("intData.txt", "r");
/* Parse each SSN in accordance with integer-integer-integer format. */
while ($user = fscanf($fh, "%d-%d-%d")) {
list ($part1,$part2,$part3) = $user;
echo "Social Security: $part1-$part2-$part3 <br />";
}
fclose($fh);
?>
<!-- intData.txt
1-3-4
-->
Try to open a text file
<?php
$openfile ="./test.txt";
$result = fopen ($openfile,"r") or die ("Couldn"t open the file");
echo "File opened successfully";
?>
Viewing the Source of a Document
<html>
<head>
<title>Viewing the source of a document</title>
</head>
<body>
<form action="<?php print $PHP_SELF ?>" method="get">
Enter file name (test.txt):
<input type="text" name="file" value="<?php print $file; ?>">
<p></p><hr><p></p>
<?php
if ( isset( $file ) )
show_source( $file ) or print "couldn"t open \"$file\"";
?>
</body>
</html>