I. File operation
1.1 A character string is written to a file
File_put_contents (‘ filename, content); Ex. :
$str='Hi, I'm Counterrr.';
file_put_contents('text.txt'.$str);
Copy the code
Effect:On the original basis, we execute again:
$str='Hi, I'm Howie programming.';
file_put_contents('text.txt'.$str);
Copy the code
Effect:You can see that running only one layer is always a clean rewrite. To implement line breaks in Notepad, write as follows:
$str="Hello, \r\n, I'm Programming Howie.";
file_put_contents('text.txt'.$str);
Copy the code
Effect:
1.2 Reading Data from a File
File_get_contents (filename); Readfile (filename); Ex. :
echo file_get_contents('./text.txt');
Copy the code
Effect:Ex. :
readfile('./text.txt');
Copy the code
Effect:
1.3 File Operations
Fopen (address, mode); There are three common modes: R: read, W: write, and A: append.
- W: write
$fp = fopen('./text.txt'.'w'); // Open the file to return the file pointer
var_dump($fp); // The file pointer type belongs to the resource type
fputs($fp.'River water from heaven'); / / write data
fclose($fp); // Close the file
Copy the code
Effect:2. R: read
$fp = fopen('./text.txt'.'r'); // Returns a file pointer
echo fgets($fp); / / read a line
Copy the code
Effect:3. A: Additional
$fp = fopen('./text.txt'.'a'); // Returns a file pointer
fputs($fp.'Run to the sea and never return.');
Copy the code
Effect:
1.4 File or not (is_file ())
Ex. :
echo is_file('./text.txt')?'Yes file' : 'Not a file';
Copy the code
Effect:
1.5 Checking whether a file or folder exists(file_exists())
Ex. :
echo file_exists('./text.txt')?'File exists' : 'File does not exist';
Copy the code
Effect:
1.6 Deleting a File (unlink())
Ex. :
$path = './tex.txt';
if (file_exists($path)) {
if (is_dir($path)) { // If it is a folder
rmdir($path);
}
elseif (is_file($path)) { // If it is a file
unlink($path); }}else {
echo 'File does not exist';
}
Copy the code
Effect:
1.7 Binary Reading (fread(Resource, length))
Ex. :
$path = './favicon.ico';
$fp = fopen($path.'r');
echo fread($fp, filesize($path));
Copy the code
Effect:You can use header() to tell the browser what to parse next in this format:
$path = './favicon.ico';
$fp = fopen($path.'r');
header('content-type:image/png');
echo fread($fp, filesize($path));
Copy the code
Effect:File_get_contents () can also be read in binary. Ex. :
header('content-type:image/png');
echo file_get_contents('./favicon.ico');
Copy the code
Effect:
1.8 summary
- A text stream has an explicit terminator, while a binary stream does not.
- File_get_contents can be used to read both characters and binary contents.
Two ways of submitting form data
- get
Ex. :
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
<form action="" method="get"<input type="text" name="user"> Password: <input type="password" name="password">
<button type="submit"</button> </form> </body> </ HTML >Copy the code
Effect: 2. post
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post"<input type="text" name="user"> Password: <input type="password" name="password">
<button type="submit"</button> </form> </body> </ HTML >Copy the code
Effect:Get and POST:
- Ostensibly, get requests have parameters in the URL address bar, whereas POST has no parameters in the url address bar.
- Security Post is more secure than GET.
- The principle of submission: GET submission is the submission of parameters one by one, while POST submission is the submission of all parameters as a whole.
- The size of the data submitted by GET is usually less than 255 bytes. The size of the data submitted by POST is different from that of the server. The size of the data submitted by POST can be configured in php.ini (post_max_size = 8M).
- Flexibility: GET submissions can pass parameters as long as there is a jump to the page. Post is inflexible, and submission requires the participation of a form.
Three jumps to get:
- HTML jump
<a href="./test.php? name=counter&age=18"> jump < / a >Copy the code
- Js jump
location.href='./test.php? name=counter&age=18';
location.assign('./test.php? name=counter&age=18');
location.replace('./test.php? name=counter&age=18');
Copy the code
- PHP jump
header('location:test.php? name=counter&age=18');
Copy the code
3. The server receives data
3.1 Post Receiving Data
Demo.php looks like this:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
<form action="./test.php" method="post"<input type="text" name="user"> Password: <input type="password" name="password">
<button type="submit"</button> </form> </body> </ HTML >Copy the code
Test.php looks like this:
if (!empty($_POST)) {
echo 'Account number:'.$_POST['user'].'<br/>';
echo 'Password:'.$_POST['password'].'<br/>';
}
? >
Copy the code
Effect:
3.2 GET Receives data
Demo.php looks like this:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
<form action="./test.php" method="get"<input type="text" name="user"> Password: <input type="password" name="password">
<button type="submit"</button> </form> </body> </ HTML >Copy the code
Test.php looks like this:
if (!empty($_GET)) {
echo 'Account number:'.$_GET['user'].'<br/>';
echo 'Password:'.$_GET['password'].'<br/>';
}
? >
Copy the code
Effect:I’m not going to show you any of the three ways to jump to get.
3.3 Request Receiving Data
$_REQUEST() holds the values submitted by GET and POST. The ability to get data and post submitted data. The problem with $_REQUEST() is that when the get and post parameters are the same, the value obtained by $_REQUEST() depends on the PHP configuration file php.ini, as follows:
GET first, then GET POST. So POST overwrites GET. Demo.php: GET/POST/demo.php:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
if (!empty($_POST)) {
echo 'Account number:'.$_REQUEST['user'].'<br/>';
}
? >
<form action="? user=justin" method="post"<input type="text" name="user">
<button type="submit"</button> </form> </body> </ HTML >Copy the code
Effect:It can be seen that the data of GET is overwritten by the data of POST. Next, we modify the configuration file as shown in the figure below:First GET POST, then GET. So GET overwrites the POST. The same code looks like this:
If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.