提示找不到文件
PHP入門篇-【3-13】PHP第一種特殊類型-資源
【嘗試1】
我在本地網(wǎng)站根目錄放了一個f.txt文件,
任務(wù)代碼如下
$file_handle?=?fopen("http://localhost/f.txt","r");
報(bào)錯找不到文件,提示信息:
Fatal error: ?Uncaught exception 'Yaf_Exception_LoadFailed_Controller' with message 'Failed opening controller script /webroot/app/controllers/F.txt.php: No such file or directory' in /webroot/index.php:12
Stack trace:
#0 /webroot/index.php(12): Yaf_Application->run()
#1 {main}
?thrown in /webroot/index.php on line 12
——————————————————————————————————————
【嘗試2】
直接用本地路徑取,代碼段
$file_handle?=?fopen("D:/PHP_coding/f.txt","r");
還是報(bào)錯,報(bào)錯信息如下
Warning: fopen(D:/PHP_coding/f.txt): failed to open stream: No such file or directory in /54/754/InX3/index.php on line 3
Warning: fclose() expects parameter 1 to be resource, boolean given in /54/754/InX3/index.php on line 12
——————————————————————————————————————
【嘗試3】
我的apache是修改過端口的,嘗試用代碼段
$file_handle?=?fopen("localhost:8080/f.txt","r");
還是報(bào)錯,報(bào)錯信息如下
Warning: fopen(localhost:8080/f.txt): failed to open stream: No such file or directory in /54/754/InX3/index.php on line 3
Warning: fclose() expects parameter 1 to be resource, boolean given in /54/754/InX3/index.php on line 12
請各位分析一下我應(yīng)該怎么修改才能正常顯示文件內(nèi)容= =?
2016-12-19
你應(yīng)該把代碼拿到本地環(huán)境(wamp/lamp)上跑,然后訪問你本地的網(wǎng)頁
如果想在瀏覽器跑,可以試試訪問index.php
<?php?
//首先采用“fopen”函數(shù)打開文件,得到返回值的就是資源類型。
$file_handle = fopen("index.php","r");
if ($file_handle){
? ? //接著采用while循環(huán)(后面語言結(jié)構(gòu)語句中的循環(huán)結(jié)構(gòu)會詳細(xì)介紹)一行行地讀取文件,然后輸出每行的文字
? ? while (!feof($file_handle)) { //判斷是否到最后一行
? ? ? ? $line = fgets($file_handle); //讀取一行文本
? ? ? ? echo $line; //輸出一行文本
? ? ? ? echo "<br />"; //換行
? ? }
}
fclose($file_handle);//關(guān)閉文件
?>
輸出結(jié)果:
if (phpversion() >= "5.3") {
? ?$root = __DIR__;
} else {
? ?$root = dirname(__FILE__);
}
define("APP_PATH", $root);
date_default_timezone_set('Asia/Chongqing');
$app ?= new Yaf_Application(APP_PATH."/conf/application.ini");
$app->bootstrap()->run();