PHP入门学习笔记五

一、文件操作

1.1 字符串写入文件中

file_put_contents('filename, content)
例:

<?php
    $str='大家好,我是Counterrr';
    file_put_contents('text.txt', $str);

效果:
在这里插入图片描述
再原有基础,我们再次执行:

<?php
    $str='大家好,我是编程华仔';
    file_put_contents('text.txt', $str);

效果:
在这里插入图片描述
可以知道只要运行一层必定是清空重写。
在记事本中如果要实现换行,如下写法:

<?php
    $str="大家好,\r\n我是编程华仔";
    file_put_contents('text.txt', $str);

效果:
在这里插入图片描述

1.2 从文件中读取

file_get_contents( filename,);readfile(filename)
例:

<?php
    echo file_get_contents('./text.txt');

效果:
在这里插入图片描述
例:

<?php
    readfile('./text.txt');

效果:
在这里插入图片描述

1.3 文件操作

fopen(地址, 模式);常见3中模式:r:读,w:写,a:追加

  1. w:写
<?php
    $fp = fopen('./text.txt', 'w'); // 打开文件返回文件指针
    var_dump($fp); // 文件指针类型,属于资源类型
    fputs($fp, '大河之水天上来'); // 写数据
    fclose($fp); // 关闭文件

效果:
在这里插入图片描述
2. r:读

<?php
    $fp =  fopen('./text.txt', 'r'); // 返回文件指针
    echo fgets($fp); // 读一行

效果:
在这里插入图片描述
3. a:追加

<?php
    $fp = fopen('./text.txt', 'a'); // 返回文件指针
    fputs($fp, '奔流到海不复还');
    

效果:
在这里插入图片描述

1.4 是否是文件(is_file())

例:

<?php
    echo is_file('./text.txt') ? '是文件' : '不是文件';
    

效果:
在这里插入图片描述

1.5 判断文件或者文件夹是否存在(file_exists())

例:

<?php
    echo file_exists('./text.txt') ? '文件存在' : '文件不存在';

效果:
在这里插入图片描述

1.6 删除文件(unlink())

例:

<?php
    $path = './tex.txt';
    if (file_exists($path)) {
        if (is_dir($path)) { // 如果是文件夹
            rmdir($path);
        }
        elseif (is_file($path)) { // 如果是文件
            unlink($path);
        }
    }
    else {
        echo '文件不存在';
    }
    

效果:
在这里插入图片描述

1.7 二进制读取(fread(resource, length))

例:

<?php
    $path = './favicon.ico';
    $fp = fopen($path, 'r');
    echo fread($fp, filesize($path));

效果:
在这里插入图片描述
可以使用header()告诉浏览器接下来解析的内容按照这个格式解析:

<?php
    $path = './favicon.ico';
    $fp = fopen($path, 'r');
    header('content-type:image/png');
    echo fread($fp, filesize($path));

效果:
在这里插入图片描述
file_get_contents( )也可以进行二进制的读取。
例:

<?php
    header('content-type:image/png');
    echo file_get_contents('./favicon.ico');

效果:
在这里插入图片描述

1.8 小结

  1. 文本流有明确的结束符,二进制没有,只有通过文件大小字节判断文件是否读取完毕。
  2. file_get_contents既可以进行字符的读取,也可以进行二进制读取。

二、表单数据提交的两种方式

  1. get
    例:
<!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">
        密码:<input type="password" name="password">
        <button type="submit">确认</button>
    </form>
</body>
</html>

效果:
在这里插入图片描述
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">
        密码:<input type="password" name="password">
        <button type="submit">确认</button>
    </form>
</body>
</html>

效果:
在这里插入图片描述
get和post的区别:

  • 外观上get请求可以在url地址栏上看到参数,post在地址栏上看不到参数。
  • 安全性上post的安全性高于get。
  • 提交原理,get提交是参数一个个的提交,post提交是将所有参数作为一个整体一起提交。
  • 提交数据大小,一般get提交一般不超过255个字节,post的大小区别于服务器,post提交数据的大小可以在php.ini中配置(post_max_size = 8M)。
  • 灵活性,get提交只要有页面的跳转就可以传递参数。post不灵活,提交需要有表单的参与。

get的三种跳转:

  1. html跳转
<a href="./test.php?name=counter&age=18">跳转</a>
  1. js跳转
location.href='./test.php?name=counter&age=18';
        location.assign('./test.php?name=counter&age=18');
        location.replace('./test.php?name=counter&age=18');
  1. php跳转
header('location:test.php?name=counter&age=18');

三、服务器接收数据

3.1 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>
    <form action="./test.php" method="post">
        账号:<input type="text" name="user">
        密码:<input type="password" name="password">
        <button type="submit">确认</button>
    </form>
</body>
</html>

test.php代码如下:

<?php
    if (!empty($_POST)) {
        echo '账号:'.$_POST['user'],'<br/>';
        echo '密码:'.$_POST['password'],'<br/>';
    }
?>

效果:
在这里插入图片描述

3.2 get接收数据

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>
    <form action="./test.php" method="get">
        账号:<input type="text" name="user">
        密码:<input type="password" name="password">
        <button type="submit">确认</button>
    </form>
</body>
</html>

test.php代码如下:

<?php
    if (!empty($_GET)) {
        echo '账号:'.$_GET['user'],'<br/>';
        echo '密码:'.$_GET['password'],'<br/>';
    }
?>

效果:
在这里插入图片描述
get的三种跳转方式都可以,这边就不演示了。

3.3 request接收数据

$_REQUEST() 保存GETPOST提交的值。技能获取get的数据又能post提交的数据。
$_REQUEST()有个问题,就是当getpost的参数名一样时,那么$_REQUEST()获取的值取决于php的配置文件php.ini,如下:

在这里插入图片描述
先获取GET,再获取POST。那么POST会将GET给覆盖了。
如下代码,即有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>
<?php
    if (!empty($_POST)) {
        echo '账号:'.$_REQUEST['user'],'<br/>';
    }
?>
    <form action="?user=justin" method="post">
        账号:<input type="text" name="user">
        <button type="submit">确认</button>
    </form>
</body>
</html>

效果:
在这里插入图片描述
可以看到GET的数据被POST的给覆盖掉了,接下来我们修改下配置文件如图:
在这里插入图片描述
先获取POST,再获取GET。那么GET会将POST给覆盖了。
还是同样的代码,效果如下:
在这里插入图片描述

在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你华还是你华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值