English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The content of this article will tell you how to use PHP to read the file on the server and display it on the web page.
There is a file named orders.txt stored on the server, its content is as follows:
Now create the vieworder.PHP file, read it out and display it;
<?php $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Customer Order</title> </head> <body> <h1>Our Store</h1> <h2>Customer Order</h2> <?php //Open file, (read-only mode+binary mode) @$fp = fopen("$DOCUMENT_ROOT/L02/files/orders.txt flock($fp, LOCK_SH); if (!$fp) { echo "<p><strong>Order did not load, please try again./strong></p>"; exit; } while(!feof($fp)){ $order=fgets($fp,999); echo $order."<br/>"; } //Release the existing lock flock($fp,LOCK_UN); //Close the file stream fclose($fp); ?> </body> </html>
The final page presented is:
Supplementary knowledge points of file read and write:
feof() - Know when to finish reading the file;
fgets(), fgetss(), fgetcsv() - Read a line of data at a time;
readfile(), fpassthru(), file(), file_get_contents() - Read the entire file;
fgetc() - Read a character;
fread() - Read any length;
file_exists() - Check if the file exists;
filesize() - Determine file size;
unlink() - Delete a file;
rewind(), fseek(), ftell() - Positioning in the file;
flock() - File locking;
That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yell Tutorial more.
Statement: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)