想做一個圖片計數器,為什麼小弟My Code會報錯

來源:互聯網
上載者:User
想做一個圖片計數器,為什麼My Code會報錯
跟著視頻學習
以下是 講圖片計數器前的鋪墊,文字計數器
if (!@$f=fopen("num.txt", "r")){
echo "檔案不存在啊";
$num=0;
}else {
fgets($f,10);
fclose($f);
}
$num++;
$ff = fopen("num.txt", "w");
fwrite($ff, $num);
fclose($ff);
echo $num;
?>
-------------------------------------------------------遇到的情況------------------------------------------------
運行第一次正常,可以建立新的num.txt檔案,且計數器跳到1
但是再次運行,理應顯示數字2
但顯示如下
( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined variable: num in C:\wamp\www\PHP100\PHP07-GraphicalCounter.php on line 42
Call Stack
#TimeMemoryFunctionLocation
10.0006253472{main}( )..\PHP07-GraphicalCounter.php:0
1


------解決方案--------------------
先用file_exists函數判斷檔案是否存在
不存在:建立檔案,並寫入1
存在:開啟檔案,讀出檔案內容,加1後寫入
------解決方案--------------------
if (!@$f=fopen("num.txt", "r")){
echo "檔案不存在啊";
$num=0;
}else {
$num=fgets($f,10);
fclose($f);
}
$num++;
$ff = fopen("num.txt", "w");
fwrite($ff, $num);
fclose($ff);
echo $num;
------解決方案--------------------
$filename='num.txt';
if(file_exists($filename)){
//檔案存在
$f=fopen($filename,'r+');
$num=(int)fgets($f);
$num++;
rewind($f);
}else{
//檔案不存在
$f=fopen($filename,'w');
$num=1;
}
fputs($f,$num);
echo '你是第'.$num.'個訪問該頁面的人';
fclose($f);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.