首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >php案例:用代码的方式创建目录+文件+写入数据(都由你定)

php案例:用代码的方式创建目录+文件+写入数据(都由你定)

作者头像
贵哥的编程之路
发布于 2022-10-24 18:02:14
发布于 2022-10-24 18:02:14
1.1K0
举报

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

文章目录


前言

学习学习怎么用代码的方式创建目录+文件+写入数据

1.php

代码语言:javascript
复制
<!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="1.php" method="POST">
		请输入创建目录名: <input type="text" name="dir" id="">
		请输入创建文件名:<input type="text" name="file" id="">
		请输入创建文件里面的文字:<input type="text" name="data" id="">
		<input type="submit" value="提交">
	</form>
</body>
</html>

<?php
$file=dirname(__FILE__)."/";
$file1=str_replace('\\', '/',$file);
//获取绝对路径D:/phpstudy_pro/WWW/
if(!$_POST['dir']||!$_POST['file']||!$_POST['data'])//为空的话。执行if里面的代码
{
	exit();
}
mkdir($_POST['dir'],0777);
$myfile = fopen($file1.$_POST['dir']."/".$_POST['file'],"w");//w可以往文件里面写东西
fwrite($myfile, $_POST['data']);
fclose($myfile);//关闭文件
print_r($myfile);//运行文件

效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 文章目录
  • 前言
  • 1.php
  • 效果
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档