Ⅰ php读取目录下所有文件内容并显示
<?php
function printFile($filepath)
{
//substr(string,start,length)函数返回字符串的一部分;start规定在字符串的何处开始 ;length规定要返回的字符串长度。默认是直到字符串的结尾。
//strripos(string,find,start)查找 "php" 在字符串中最后一次出现的位置; find为规定要查找的字符;start可选。规定开始搜索的位置
//读取文件后缀名
//$filetype = substr ( $filename, strripos ( $filename, "." ) + 1 );
//判断是不是以txt结尾并且是文件
#if ($filetype == "txt" && is_file ( $filepath . "/" . $filename ))
if ( is_file ( $filepath))
{
$filename=iconv("gb2312","utf-8",$filepath);
echo $filename."内容如下:"."<br/>";
$fp = fopen ( $filepath, "r" );//打开文件
#while (! feof ( $f )) //一直输出直到文件结尾
$i = 1;
while ($i < 10)
{
$line = fgets ( $fp );
echo $line."<br/>";
$i = $i +1;
}
fclose($fp);
}
}
(此处空一行)
function readFileRecursive($filepath)
{
if (is_dir ( $filepath )) //判断是不是目录
{
$dirhandle = opendir ( $filepath );//打开文件夹的句柄
if ($dirhandle)
{
//判断是不是有子文件或者文件夹
while ( ($filename = readdir ( $dirhandle ))!= false )
{
if ($filename == "." or $filename == "..")
{
//echo "目录为“.”或“..”"."<br/>";
continue;
}
//判断是否为目录,如果为目录递归调用函数,否则直接读取打印文件
if(is_dir ($filepath . "/" . $filename ))
{
readFileRecursive($filepath . "/" . $filename);
}
else
{
//打印文件
printFile($filepath . "/" . $filename);
echo "<br/>";
}
}
closedir ( $dirhandle );
}
}
else
{
printFile($filepath . "/" . $filename);
return;
}
}
(此处空一行)
header("content-type:text/html;charset=utf-8");
#echo "Hello World"."<br/>";
$filepath = "C:/phpStudy/PHPTutorial/WWW/test/results"; //想要读取的目录
readFileRecursive($filepath )
?>
php还可以读取文件夹下所有图片,方法如下
hostdir=dirname(__FILE__).'/data/upload/admin/20170517/'; //要读取的文件夹
(此处空一行)
$url = '/data/upload/admin/20170517/'; //图片所存在的目录
(此处空一行)
$filesnames = scandir($hostdir); //得到所有的文件
(此处空一行)
// print_r($filesnames);exit;
//获取也就是扫描文件夹内的文件及文件夹名存入数组 $filesnames
(此处空一行)
$www = 'http://www.***.com/'; //域名
(此处空一行)
foreach ($filesnames as $name) {
$aurl= "<img width='100' height='100' src='".$www.$url.$name."' alt = '".$name."'>"; //图片
echo $aurl . "<br/>"; //输出他
Ⅱ PHP读取当前网络路径目录
<?php
//file:http://localhost/1/1.php
//local:E:\AppServ\www\1\1.php
//两种 第一种是你需要的
echo $url = substr($_SERVER['SCRIPT_URI'],0,strrpos($_SERVER['SCRIPT_URI'],'/'))."<br>";
echo getcwd();
/*
输出:
http://localhost/1
E:\AppServ\www\1
*/
?>
Ⅲ PHP远程读取excel文件,怎么读取
PHPExcel 通过 PHPExcel_Shared_OLERead 类的 read 方法读取文件
但 read 方法里使用了 is_readable 函数来确认文件是否存在,而 is_readable 不能作用于 url
所以不可直接远程读取
但若绕过 is_readable 函数的话,就是可以的
public function read($sFileName)
{
// Check if file exists and is readable
if(!is_readable($sFileName)) {
throw new Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
}
// Get the file data
$this->data = file_get_contents($sFileName);
Ⅳ 检查使用PHP SSH2远程目录问题,怎么解决
为PHP安装SSH2扩展需要两个软件包,libssh2和ssh2。两者的最新版本分别为1.4.2和0.12,下载地址分别为http://www.libssh2.org/download/和http://pecl.php.net/package/ssh2。这里我们可以均下载最新版本,libssh2的源码包为libssh2-1.4.2.tar.gz,ssh2的源码包为ssh2-0.12.tgz。
其次,解压并安装libssh2和ssh2。其中,libssh2需要先安装,ssh2后安装。安装步骤如下:
# tar -zxvf libssh2-1.4.2.tar.gz
# cd libssh2-1.4.2
# ./configure --prefix=/usr/local/libssh2
# make && make install
以上为安装libssh2,这里需要记住libssh2的安装目录,因为在安装ssh2的时候还会用到。
# tar -zxvf ssh2-0.12.tgz
# cd ssh2-0.12
# phpize
# ./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2
# make
执行完以上过程后,在当前目录下的moles目录下会生成一个ssh2.so文件,这就是扩展PHP所需要的,将该文件拷贝到PHP库的存储目录下在修改PHP的配置文件即可。
# cp moles/ssh2.so /usr/lib64/php/moles/
注:PHP库的存储目录可能因系统而异,本博主的机器上是/usr/lib64/php/moles/
# vi /etc/php.ini
# 向该文件中添加内容:extension=ssh2.so
此时为PHP扩展SSH2就已经完成了,为了验证是否安装成功,我们可以通过执行一下命令来验证。
# php -i|grep ssh2
Registered PHP Streams => php, file, http, ftp, compress.bzip2, compress.zlib, https, ftps, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp
ssh2
libssh2 version => 1.4.2
banner => SSH-2.0-libssh2_1.4.2
最后,我们再通过一个简单的PHP程序来试用SSH2,该程序首先连接远程服务器,然后执行相关操作,最后读取操作执行的返回结果,具体例子代码如下。
<?php
$user="user";
$pass="password";
$connection=ssh2_connect('202.112.113.250',22);
ssh2_auth_password($connection,$user,$pass);
$cmd="ps aux";
$ret=ssh2_exec($connection,$cmd);
stream_set_blocking($ret, true);
echo (stream_get_contents($ret));
?>
Ⅳ 用php程序自动读取远程文件并更新到本地,每天一次,如何做
windows:
准备:
1.将 php.exe 的路径加入 windows 的环境变量
2.编写文件:
D:\fileGeter.php
<?php
$filelist = Array(
"http://**********/a.txt",
"http://**********/b.txt",
);
$saveas="D:\\" ;
$endl = ".txt"
function getfile(){
foreach( $filelist as $k => $file )
file_put_contents( $saveas . $k . $endl , file_get_contents( $file ) ) ;
}
getfile();
?>
3.执行cmd命令
at 11:20 /every:1,2,3,4,5,6,7 "php D:\fileGeter.php"
linux 更方便
直接把此文件包含进 你要写的程序里就OK了,
fileGeter.php:
<?php
...
...
$saveas = "./";
...
..
?>
index.php:
<?php
require_once("fileGeter.php");
//and so on .....
.....
....
....
?>
Ⅵ php读取远程xml文件简单方法
<?php
set_time_limit(0);
function_rand(){
$length=26;
$chars="";
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
$string='';
for($i=0;$i<$length;$i++){
$string.=$chars[mt_rand(0,$max)];
}
return$string;
}
$HTTP_SESSION=_rand();
$HTTP_SESSION;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?&theUserID=&theCityCode=贵港");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727)");
$res=curl_exec($ch);
curl_close($ch);
//print_r($res);
$xml_array=simplexml_load_string($res);
//www.hi-docs.com/php/simplexml_load_string.html
foreach($xml_arrayas$tq){
echo$tq;
}
?>
Ⅶ php怎样遍历远程文件夹下的文件
window是用的GB2312的编码,你的php文件应该用的是UTF-8,所以正如你写的那样,先要转换编码$dir=iconv("utf-8","gb2312",$dir);
但你别忘了,你用的是UTF-8的编码,所以你第六行写错了,把GB2312转换为UTF-8搞倒了吧
123456789101112131415<?phpfunction refresh($dir){ $dir=iconv("utf-8","gb2312",$dir); if ($headle=opendir($dir)){ while ($file=readdir($headle)){ $file=iconv("gb2312","utf-8",$file); if ($file!='.' && $file!='..'){ echo "文件".$file."在文件夹".$dir."下<br />"; } } closedir($headle); }}refresh("D:/AppServ/www/test");?>
Ⅷ 100分求php读取目录文件
if ( $handle = opendir( "back" ) )
{
$i = 0;
while ( false !== ( $file = readdir( $handle ) ) )
{
if ( !( $file != "." ) && !( $file != ".." ) )
{
$i += 1;
echo "\t\r\n\t<tr>\r\n\t\t<td>";
echo $i;
echo "</td>\r\n \t<td>";
echo $file;
echo "</td>\r\n \t <td align=\"center\">\r\n\t\t<a href=\"Admin_Sql.php?Action=backin&file=";
echo $file;
echo "\">导入</a>\r\n\t\t<a href=\"Admin_Sql.php?Action=delback&file=";
echo $file;
echo "\">删除</a>\r\n\t </td>\r\n\t</tr>\r\n";
}
}
closedir( $handle );
}
这一段修改为下面的三行:
echo '<pre>';
echo `dir back`;
echo '</pre>';
Ⅸ php有哪些函数可以远程读取文件
php读取远程文件的方式很多,常用的函数有file_put_contents,fopen,也可以使用curl的方式读取。具体用法参考php手册。
Ⅹ php中的file_get_contents获取远程页面如何实现
安装CopSSH之前先确保防火墙开启了SSH端口,这个虽然不影响CopSSH的安装,但是影响SSH访问,所以写在前面。
CopSSH是windows下的SSH服务器软件,下载地址之,本文使用的是Copssh_4.1.0_Installer.exe,
安装完成后,到控制面板中新建一个管理员账户root,用这个账户来共享SSH。然后你在账户管理中会看到之前的SvcCOPSSH账户。
将root用户添加到CopSSH用户中,为简单操作,允许使用密码认证方式
若是不允许密码认证,则需要使用公钥密钥方式认证,
三、CopSSH中使用GIT
现在已经安装GIT和CopSSH,接下来需要做的就是让CopSSH可以使用GIT的命令,这样不仅能够远程SSH管理GIT服务器,而且可以将GIT仓库通过SSH共享。具体的操作方法是将GIT的某些命令程序和动态链接库复制到CopSSH安装目录下即可。
l 将$ Git\libexec\git-core目录下的git.exe , git-receive-pack.exe , git-upload-archive.exe , git-upload-pack.exe复制到$ICW\bin目录下
l 将$Git\bin目录下的libiconv-2.dll复制到$ICW\bin目录下
重启CopSSH即可