zl程序教程

您现在的位置是:首页 >  其他

当前栏目

php案例 往某个目录下创建文件并写入数据

案例PHP文件数据 创建 目录 写入 某个
2023-06-13 09:13:31 时间

作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人) 阿里云专家博主

文章目录


代码

index.php

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<form action="change.php" method="POST">
		您要增加文件的绝对路径: <input type="text" name="path" id="">
		您要增加什么文件呢?<input type="text" name="file" id="">
		<input type="submit" value="提交">
	</form>
</body>
</html>

change.php

<?php
$path=$_POST['path'];//要创建文件地方.某个目录下我这里是D:/phpstudy_pro/WWW/cyg/
$myfile = fopen($path.$_POST['file'],"w");//w可以往文件里面写东西
$txt = "Bill Gates\n";//写入这个变量中的数据。在666.txt文件中
fwrite($myfile, $txt);
fclose($myfile);//关闭文件
print_r($myfile);//运行文件

之前的

之后的:

自己输入的:

注意

输入的绝对路径是不是这样的哦 /