1. php操作TXT文本,一個文本框,一個提交按鈕,點擊提交按鈕之後,把這個文本框的內容寫進data/
很簡單,使用file_put_contents()函數
<?php
if(isset($_GET["name"])){
file_put_contents("data/text1.txt",$_GET["name"]);
}
?>
如果是post提交,就把$_GET修改成$_POST
2. PHP 修改文本文件內容
<?php
//從文件中讀取
$path="1.txt";
$fp=file($path);
$arr=array();
foreach($fpas&$line){
$data=explode("=",$line);
if(count($data)>1)
{
$arr[]=array($data[0]=>$data[1]);
}else{
$arr[]=$line;
}
}
//假設要修改ProctType為10
setValue("ProctType","10",$arr);
//var_mp($arr);
//重新保存到文件
$fp=fopen("2.txt","w");
foreach($arras$row){
if(is_array($row)){
foreach($rowas$key=>$r){
fwrite($fp,$key."=".$r);
}
}else{
fwrite($fp,$row);
}
}
fclose($fp);
functionsetValue($name,$value,&$arr){
foreach($arras$key=>$row){
if(is_array($row)&&isset($row[$name])){
$arr[$key][$name]=$value;
//修改後記得加上換行
$arr[$key][$name]=$arr[$key][$name]." ";
}
}
}
?>
我測試了可以使用,如果可以請將兩個問題都採納下,謝謝。
3. 用PHP,怎麼修改txt文本內的內容
<?php
header("Content-type:text/html;charset=gbk");
if(isset($_POST['submit'])){
$editContent=$_POST['editContent'];//獲取輸入框的內容
$res=file_put_contents("test.txt",$editContent);//執行修改
if($res){
echo'內容修改成功!';
}else{
echo'內容修改失敗!';
}
}else{
echo'請做出修改....';
}
?>
<formmethod="post"action="">
<textareaname="editContent"cols="100"rows="15">
<?phpechofile_get_contents("test.txt")?>
</textarea>
<buttonname="submit">確認</button>
</form>
4. php高手進(關於php操作文本數據問題)
影響,那是毫無疑問的.
因為php讀取文件時文件指針從前向後移動,直至訪問到「\0"時才停止.
大的文件,小的文件讀取方式都是一樣,只是路徑(文本長度)不一樣.
所以花的時間也不一樣.
花點錢升級下mysql吧不貴的,樓上說的access不通用,
可能虛擬空間不支持,也可能是在linux系統下.
注:文件存儲信息(安全級別)非常不安全,謹用!!!
5. 如何使用PHP讀取文本文件內容
利用PHP讀取文本文件的內容,其實很簡單,我們只需要掌握函數「file_get_contents();」的使用就可以了。下面,小編將作詳細的介紹。
工具/原料
電腦一台
WAMP開發環境
方法/步驟
file_get_content()函數介紹。使用file_get_contents()獲取txt文件的內容,具體參數說明如下:
2
具體實例說明。從文本文件tst.txt中讀取裡面的內容並顯示在瀏覽器中,具體代碼和圖示如下:
<?php
$file = 'tst.txt';
$content = file_get_contents($file); //讀取文件中的內容
echo $content;
?>
6. php如何讀取文本指定的內容
php讀取文件內容:
-----第一種方法-----fread()--------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定讀取大小,這里把整個文件內容讀取出來
echo $str = str_replace("\r\n","<br />",$str);
}
?>
--------第二種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//將整個文件內容讀入到一個字元串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-----第三種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次讀取 1024 位元組
while(!feof($fp)){//循環讀取,直至讀取完整個文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-------第四種方法--------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行讀取文件內容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>
----第五種方法--------------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行讀取。如果fgets不寫length參數,默認是讀取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
7. php怎麼把數據寫入文本文件
php數據寫入文本文件的具體操作步驟如下:
1、使用touch命令建立一個a.php的文件。
8. PHP操作txt,寫入文本問題
不多說,手打上代碼。我寫的是php代碼啊,不過php需要運行環境,但是js我不會
<?php
echo"<metacharset=utf-8> ";
$content='';
//生成靜態文件代碼
if(isset($_POST['btn'])){
if(!is_dir('data'))mkdir('data');
$file='data/text11.txt';//路徑,可以自己修改
$content=$_POST['text'];
$fp=fopen($file,w);
fwrite($fp,$content);
fclose($fp);
echo"<scriptlanguage='javascript'> alert('寫入成功') </script> ";
}
//下面是前台顯示
$str="<formmethod='post'action=''> <inputtype='text'name='text'value='$content'> <inputtype='submit'name='btn'value='提交'> </form>";
echo$str;
?>
9. 如何在php中對文件進行讀寫操作
嗯,很簡單,不過這次不是用file_put_contents()函數了,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
// 假設你的文件b.php已經創建,並且有權操作
// 但還是加上許可權設定的語句,比較保險
chmod(dirname(__FILE__), 0777); // 以最高操作許可權操作當前目錄
// 打開b.php文件,這里採用的是a+,也可以用a,a+為可讀可寫,a為只寫,如果b.php不能存在則會創建它
$file = fopen('b.php', 'a+'); // a模式就是一種追加模式,如果是w模式則會刪除之前的內容再添加
// 獲取需要寫入的內容
$c = '我是要被追加的內容!';
// 寫入追加的內容
fwrite($c, $file);
// 關閉b.php文件
fclose($file);
// 銷毀文件資源句柄變數
unset($file);