[英]php. write to file
我正在學習php。 我想將字符串寫入文件,但是沒有任何反應。 我該如何調試? 我有python的經驗。 那里我可以使用終端嘗試小的代碼片段,但我不知道如何在php中檢查代碼。
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
$stringData = "Bobby Bopper\n";
$myvar = fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
除非您確實需要使用文件處理程序,否則請執行以下操作:
$myFile = "testFile.txt";
$stringData = "Bobby Bopper\nTracy Tanner\n";
file_put_contents($myFile,$stringData);
在php中進行調試總是從顯示錯誤報告開始
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
$stringData = "Bobby Bopper\n";
$myvar = fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
您現在很可能會看到描述性的錯誤消息。 谷歌搜索php錯誤消息通常很有幫助。
這看起來像正確的代碼。 嘗試檢查以確保您具有正確的文件路徑。 一個簡單的方法是
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
$fh = fopen($myFile, 'r');
$value = fgets($fh);
echo $value;
fclose($fh);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.