本文实例讲述了php实现编辑和保存文件的方法。分享给大家供大家参考。具体如下:
save_file.php:
<?php
session_start()
$handle = fopen($_POST['original_file_name'], "w")
$text = $_POST['file_contents']
if(fwrite($handle, $text) == FALSE){
$_SESSION['error'] = '<span class="redtxt">There was an error</span>'
}else{
唤铅返 $_SESSION['error'] = '<span class="redtxt">File edited successfully</span>'
}
fclose($handle)
header("Location: ".$_POST['page'])
?>
read_file.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<form action="savecontents.php" method="post">
<textarea name="file_contents" style="width:700pxheight:600px">
<?php
$fileName = "location/of/orignal/file/my_file.php"
$handle = fopen($fileName, "r")
while (!feof($handle)){
$text = fgets($handle)
echo $text
}
?>
激态 </textarea>
和饥 <input type="hidden" value=" <? echo $fileName?>" name="original_file_name" />
</form>
<body>
</body>
</html>
希望本文所述对大家的php程序设计有所帮助。
$data = file_get_contents("data/3.txt")$ary = split(',',$data)
$test2=serialize($ary)
saveFile("data/4.txt", $test2)
/**
* 保存核碧文件
*
* @param string $fileName 文件名(含相对路径)
* @param string $text 文件内容
* @return boolean
*/
function saveFile($fileName, $text) {
if (!$fileName || !$text)
return false
if (makeDir(dirname($fileName))) {
if ($fp = fopen($fileName, "w")) {
if (@fwrite($fp, $text)) {
fclose($fp)
return true
} else {
fclose($fp)
return false
}
}
}
return false
}
/**
* 连续创建目录
*
* @param string $dir 目录首渗字符串
* @param int $mode 权限数字
* @return boolean
*/
function makeDir($dir, $mode = "0777") {
if (!dir) return false
if(!file_exists($dir)) {
return mkdir($dir,$mode,true)
} else {
return true
}
}
/*
echo $data
$str = split(',',$data)
echo "<改芹举br/>"
for($i=0$i <count($str)$i++)
{
$new[] = $str[$i]
echo "<br/>"
echo $str[$i]
}
$value = rand(0, 3)
echo "<br/>"
echo $str[$value]
*/
if (!$_FILES['file']['error']) {
//判断是否有uploads目录,没有则创建这个目录
if (!is_dir('appdownload')) {
mkdir('appdownload')
}
$name= isset($_FILES['file']['name'])?$_FILES['file']['name']:""
$size=isset($_FILES['file']['size'])?$_FILES['file']['size']:''
$type=isset($_FILES['file']['type'])?$_FILES['file']['type']:''
$ext = substr($name, strrpos($name, '.')+1)
$up_name=date("Ymdhis").'.'.$ext
$path_in_folder = 'appdownload/'.$up_name
move_uploaded_file($_FILES['file']['tmp_name'], $path_in_folder)
//获取文件名,创建文件放置的位置
//$filepath = 'appdownload/斗迅' . $_FILES['file']['name']
//将临时文件移动到指定位置
空凯此 //move_uploaded_file($_FILES['file']['tmp_name'], $filepath)
$file = fopen($path_in_folder, "r")
$userdata = []
while (!feof($file)) {
$tempdata = fgetcsv($file)
$userid = $tempdata[0]
$userid = intval($userid)
if ($userid >0) {
array_push($userdata, $userid)
}
}
孙唤 fclose($file)
$usernum = count($userdata)
$apidata = ['code' =>200, 'msg' =>'成功', 'data' =>['url' =>'http://'.$_SERVER['HTTP_HOST'].'/'.$path_in_folder, 'usernum' =>$usernum]]
return json($apidata)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)