PHP Learning 1-php file processing

Source: Internet
Author: User

Include and require in PHP

The Php include (or require) statement gets all the text/code/tags that exist in the specified file and is copied to the file that uses the include statement.

This means that you can create a standard header, footer, or menu file for all pages. Then, when the page header needs to be updated, you only need to update the header include file.

Can be used to write standard menus (menus with multiple pages) and define global variables.

The difference between include and require:

Require: When the file is requested by the application.

Include: When a file is not required, and the application should continue to run when the file is not found.

To refer to the footer.php footer file in one page, use the Include statement:

1  2 <body> 3  4  5 <p> A paragraph of text. </p> 6 <p> A paragraph of text. </p> 7include ' footer.php ';? > 8  9 </body> 

PHP file Processing:

    • ReadFile (), the fopen () function reads the file and writes it to the output buffer.

The first parameter of fopen () contains the file name that is opened, and the second parameter specifies the mode in which the file is opened.

    • The Fread () function reads open files or creates files.

The first parameter of Fread () contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read.

    • The fclose () function is used to close open files.

    • The fgets () function is used to read a single line from a file.

After you call the Fgets () function, the file pointer moves to the next line.

    • The feof () function checks to see if "End-of-file" (EOF) has been reached.

    • The fgetc () function is used to read a single character from a file.

After calling the Fgetc () function, the file pointer moves to the next character.

    • The fwrite () function is used to write to a file.

The first parameter of Fwrite () contains the file name of the file to be written, and the second argument is the string to be written.

Re-opening the file Fwritr () overwrites the last content (overwriting).

Instance:

1<?PHP2 $myfile=fopen("Webdictionary.txt", "R") or die("Unable to open file!");3 //output single line until End-of-file4  while(!feof($myfile)) {5   Echo fgets($myfile) . "<br>";6 }7 fclose($myfile);8?>

PHP File Upload

1<?PHP2 if($_files["File"] ["error"] > 0)3   {4   Echo"Error:".$_files["File"] ["Error"]. "<br/>";5   }6 Else7   {8   Echo"Upload:".$_files["File"] ["Name"]. "<br/>";9   Echo"Type:".$_files["File"] ["Type"]. "<br/>";Ten   Echo"Size:". ($_files["File"] ["Size"]/1024). "Kb<br/>"; One   Echo"Stored in:".$_files["File"] ["Tmp_name"]; A   } -?>

By using PHP's global array $_files, you can upload files from a client computer to a remote server.

The first parameter is the input name of the form, and the second subscript can be "name", "type", "Size", "Tmp_name" or "error". :

$_files["File" ["Name"]-the name of the file being uploaded

$_files["File" ["type"]-the type of file being uploaded

$_files["File" ["Size"]-the size of the uploaded file, measured in bytes

$_files["File" ["Tmp_name"]-the name of the temporary copy of the file stored on the server

$_files["File" ["Error"]-error code caused by file upload

Upload files can be restricted by $_files["file" ["type"] or $_files["file" ["Size"].

The above code simply uploads a temporary copy, if you want to save it:

1 if(file_exists("upload/".$_files["File"] ["Name"]))2       {3       Echo $_files["File"] ["Name"]. "already exists.";//prompt if the file already exists4       }5     Else6       {7       Move_uploaded_file($_files["File"] ["Tmp_name"],8"Upload/".$_files["File"] ["name"]);//Save File9       Echo"Stored in:". Upload/".$_files["File"] ["Name"];Ten}

(* Summary from W3school)

PHP Learning 1-php file processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.