判断远程文件是否存在,直接用网站路径+文件,file_exists好像无反应,
使用$_SERVER['DOCUMENT_ROOT']+网站路径+文件判断成功。
$file ="/d/file/a.jpg";
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$file)) {
$file ="/d/file/no.jpg";
}
php:打开一个文件替换指定内容后再次写入,最节省资源的方法:
<?php
$f='带路径文件名';
file_put_contents($f,str_replace('原待替换字符串','替换后的字符串',file_get_contents($f)));
?>
第一种,使用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);
fclose($fp);
}
?>
第二种,用file_get_contents函数:
<?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;
}
?>
2 1 2 下一页 尾页 |