php实现编辑和保存文件的方法

php实现编辑和保存文件的方法,第1张

文章主要介绍了php实现编辑保存文件方法,涉及php针对文件的读取、编辑和保存 *** 作的'相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了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程序设计有所帮助。

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)

        }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12284999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存