php递归遍历目录文件与文件夹

 更新时间:2016年11月25日 15:51  点击:1304

他们利用了递归的方法来实例目录遍历,可以查找出无限级目录的文件与文件夹中的文件并显示,下面是实例代码

<?php教程
$dir = 'f:game';
function read_dir_all($dir) {
$ret = array('dirs'=>array(), 'files'=>array());
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file != '.' && $file !== '..') {
$cur_path = $dir . directory_separator . $file;
if(is_dir($cur_path)) {
$ret['dirs'][$cur_path] = read_dir_all($cur_path);
} else {
$ret['files'][] = $cur_path;
}
}
}
closedir($handle);
}
return $ret;
}
$p = read_dir_all($dir);
echo '<pre>';
var_dump($p);
echo '</pre>';
?>

php教程文件上传之原理分析与上传类代码

今天研究PHP注册POST/GET大变量的时候,看到了关于这块的一些东西,跟踪了半天,,先记录下来,免得以后再如此麻烦的跟踪
处理器注册:
[mod_php5.c, mod_php5模块初始化]
php_init_handler(server_rec *s, pool *p)
    ->[main/SAPI.c]sapi_startup(&apache_sapi_module)
        ->[main/SAPI.c]
sapi_globals_ctor(&sapi_globals)
            ->[main/php_content_types.c]php_setup_sapi_content_types(TSRMLS_C)
                ->[main/php_content_types.c
php_post_entries如下]sapi_register_post_entries(php_post_entries
TSRMLS_CC)
                    ->[main/SAPI.c]sapi_register_post_entry(p
TSRMLS_CC)

  如下面的代码,共注册了俩个处理器,分别处理post数据和文件上传。
  注1:参看在PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究
[main/rfc1867.h]
      #define
MULTIPART_CONTENT_TYPE
"multipart/form-data"

[main/php_content_types.h]
     #define
DEFAULT_POST_CONTENT_TYPE
"application/x-www-form-urlencoded"

[main/SAPI.c]
struct
_sapi_post_entry
{
char *content_type;
        uint
content_type_len;
        void
(*post_reader)(TSRMLS_D);
        void
(*post_handler)(char *content_type_dup, void *arg
TSRMLS_DC);
    };
  [main/php_content_types.c]
static
sapi_post_entry
php_post_entries[] = {
{
DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler
},
        {
MULTIPART_CONTENT_TYPE,    sizeof(MULTIPART_CONTENT_TYPE)-1,    NULL,                         rfc1867_post_handler
},
        {
NULL, 0, NULL, NULL
}
};


<?php
#*********************************************************
#文件名称: yl_upload.class.php
#功能描述: 印像上传类
#程序制作:留印(adleyliu)
#联系qq  :14339095
#联系邮箱:adleyliu@163.com
#最后更新:     2007-11-11
#注:转发时请保留此声明信息,这段声明不并会影响你的速度!
#如有修改请将修改后的文件以邮件形式发送给作者一份,谢谢!
#
#*********************************************************
/*
//使用说明:
//声明一个上传类
   $yl_upload = new yl_upload_class();
//设置参数
   global $yl_filedata,$yl_directroy,$file_urldirectroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   $yl_filedata = 'filedata';//表单名
   $yl_directroy = '../upload_file';//上传主目录
   $file_urldirectroy = 'upload_file';//文件url(在编辑器KindEditor中用到,请查看upload.php实例)
   $yl_settingsnew = 'month_'.date('Ym');//上传主目录1按版块存入不同目录,2按月份存入不同目录,3按天存入不同目录
   $yl_maxsize = 2097152;  //这里以字节为单位(1024*2)*1024=2097152 = 2M
   $yl_sizeformat = 'k';   //显示文件大小单位b,k,m
   $yl_arrext = array('gif','jpg','jpeg','png','bmp');//允许上传文件类型
   $yl_ext = 0;  //0原文件类型上传,1统一为存为jpg
//上传
   $yl_upload -> yl_uploadfile();
*/
class yl_upload_class
{
#*********************************************************
#创建目录函数
#*********************************************************
function createfolder($yl_path)
{
   if (!file_exists($yl_path))
   {
    $this -> createfolder(dirname($yl_path));
    @mkdir($yl_path, 0777);
   }
   return $this -> createfolder;
}
#*********************************************************
#获取文件名称,大小,类型,临时文件名
#*********************************************************
function yl_getfilename($yl_type)
{
   global $yl_filedata,$yl_directroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   return $_FILES[$yl_filedata][$yl_type];
}
#*********************************************************
#获取文件大小
#*********************************************************
function yl_getfilesize()
{
   global $yl_filedata,$yl_directroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   if($this -> yl_getfilename('size') == 0){
      $this -> alert("请选择上传文件!");
      exit;
   }
   if($this -> yl_getfilename('size') > $yl_maxsize){
         $yl_maxsizek=$yl_maxsize/1024;
      $this -> alert("上传文件超出限制范围$yl_maxsizek.K!");
      exit;
   }
   switch (strtolower($yl_sizeformat)){
   case 'b':
   return $this -> yl_getfilename('size') . ' B';
   break;
   case 'k':
   return ($this -> yl_getfilename('size')/1024) . ' K';
   break;
   case 'm':
   return ($this -> yl_getfilename('size'))/(1024*1024) . ' M';
   }
}
#*********************************************************
#获得文件扩展名
#*********************************************************
function yl_getfiletype()
{
   global $yl_filedata,$yl_directroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
     $yl_temp_arr = explode(".", $this -> yl_getfilename('name'));
     $yl_file_ext = array_pop($yl_temp_arr);
      $yl_file_ext = trim($yl_file_ext);
     $yl_file_ext = strtolower($yl_file_ext);
     //检查扩展名
     if (in_array($yl_file_ext, $yl_arrext) === false) {
        $this -> alert("上传文件类型被限制!");
        exit;
     }
     return $yl_file_ext;
}
#*********************************************************
#上传
#*********************************************************
function yl_uploadfile()
{
   global $yl_filedata,$yl_directroy,$file_urldirectroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   $yl_file_nameold = $this -> yl_getfilename('name');//原文件名
   $yl_file_namenews = date('Ymd').'_'.md5(date('YmdHis'));//重命名
   if($yl_ext == 0){
      $yl_file_names = $yl_file_namenews.'.'.$this -> yl_getfiletype();//改名
   }elseif ($yl_ext == 1){
      $yl_file_names = $yl_file_namenews.'.jpg';//统一改名为jpg
      }
   $yl_tmp_name = $this -> yl_getfilename('tmp_name');//服务器上临时文件名
   $yl_file_size = $this -> yl_getfilesize();//获取文件大小
   $yl_file_type = $this -> yl_getfiletype();//获取文件类型
   $yl_file_path = $yl_directroy.'/'.$yl_settingsnew;//建立一个目录
     //检查目录是否存在,不存在则创建
     if(@is_dir($yl_file_path) === false) {
      $this -> createfolder(''.$yl_file_path.'');//创建目录
     }
     //检查是否已上传
   if(@is_uploaded_file($yl_tmp_name) === false) {
        $this -> alert("文件已上传!");
        exit;
   }
     //检查目录写权限
     if (@is_writable($yl_file_path) === false) {
          $this -> alert("上传目录没有写权限!");
          exit;
     }
   $yl_doupload = @copy($yl_tmp_name, ''.$yl_file_path.'/'.$yl_file_names.'');
   if($yl_doUpload === false)
   {
    $this -> alert("上传失败!");
   }else{
    echo '上传成功';
    echo '<br>';
    echo '文件目录:'.$yl_file_path.'';
    echo '<br>';
    echo '原文件名:'.$yl_file_nameold.'';
    echo '<br>';
    echo '新文件名:'.$yl_file_names.'';
    echo '<br>';
    echo '文件大小:'.$yl_file_size.'';
    echo '<br>';
    echo '文件类型:'.$yl_file_type.'';
   }
    return;
}
#*********************************************************
#*删除文件
#*********************************************************
function delfile()
{
   global $yl_filedata,$yl_directroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   $yl__dir = dirname(trim($yl_directroy));
   if( $this->_isDel( $yl__dir ) )
   {
      return @unlink( $yl_directroy ) ? true : false;
   }else{
   return false;
   }
}
#*********************************************************
#删除目录 目录下如果有文件不能删除
#*********************************************************
function deldir( )
{
   global $yl_filedata,$yl_directroy,$yl_settingsnew;
   global $yl_maxsize,$yl_sizeformat,$yl_arrext,$yl_ext;
   if( $this->_isdel($yl_directroy) && is_dir( $yl_directroy ) )
   {
      return @rmdir( $yl_directroy ) ? true : false;
   }else{
      return false;
   }
}
#*********************************************************
#提示
#*********************************************************
function alert($yl_msg)
{
    echo '<html>';
    echo '<head>';
    echo '<title>error</title>';
    echo '<meta http-equiv="content-type" c>';
    echo '</head>';
    echo '<body>';
    echo '<script type="text/网页特效">alert("'.$yl_msg.'");;</script>';
    echo '</body>';
    echo '</html>';
    exit;
}
}
?>

php教程 soap 调用获取返回信息

trace soap message
获取 soap返回信息

<?php
   $ws = "http://www.111cn.net/sd/2001/temperatureservice.wsdl";
   $zipcode = "23590";
   $client = new soapclient($ws,array('trace' => 1));
   $temperature = $client->gettemp($zipcode);
   echo htmlspecialchars($client->__getlastrequest());
?>

得到函数原型从wsdl

 

<?php
   $ws = "http://www.www.111cn.net/sd/2001/temperatureservice.wsdl";
   $client = new soapclient($ws);
   var_dump($client->__getfunctions());
?>
 

<?php
   $ws = "http://www.xmethods.net/sd/2001/temperatureservice.wsdl";
   $zipcode = "12312";
   $client = new soapclient($ws,array('trace' => 1));
   $temperature = $client->gettemp($zipcode);
   echo htmlspecialchars($client->__getlastresponse());
?>
 

由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php教程 以我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例。

<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1>

<h3>demonstrates checkboxes</h3>
<form action ="handleformcheckbox.php">

<ul>
  <li><input type ="checkbox" name ="chkfries" value ="11.00">fries</li>
  <li><input type ="checkbox" name ="chksoda"  value ="12.85">soda</li>
  <li><input type ="checkbox" name ="chkshake" value ="1.30">shake</li>
  <li><input type ="checkbox" name ="chkketchup" value =".05">ketchup</li>
</ul>
<input type ="submit">
</form>

</body>
</html>


<!-- handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

here;

$total = 0;

if (!empty($chkfries)){
  print ("you chose fries <br>");
  $total = $total + $chkfries;
}

if (!empty($chksoda)){
  print ("you chose soda <br>");
  $total = $total + $chksoda;
}

if (!empty($chkshake)){
  print ("you chose shake <br>");
  $total = $total + $chkshake;
}

if (!empty($chkketchup)){
  print ("you chose ketchup <br>");
  $total = $total + $chkketchup;
}

print "the total cost is $$total";

?>
</body>
</html>


-->

实例

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server['php_self']);
if (!empty($food)) {
    echo "the foods selected are:<br />";
    foreach($food as $foodstuf)
    {
        echo "<strong>".htmlentities($foodstuf)."</strong><br />";
    }
}
else
{
    echo ("<form action="$self" ");
    echo ('method="get">
    <fieldset>
        <label>italian <input type="checkbox" name="food[]" value="italian" />
</label>
        <label>mexican <input type="checkbox" name="food[]" value="mexican" />
</label>
        <label>chinese <input type="checkbox" name="food[]" value="chinese"
        checked="checked" /></label>
    </fieldset>
    <input type="submit" value="go!" >');
}
?>
</body>
</html>


多选checkbox

<?php
$options = array('option 1', 'option 2', 'option 3');

$valid = true;
if (is_array($_get['input'])) {
    $valid = true;
    foreach($_get['input'] as $input) {
        if (!in_array($input, $options)) {
            $valid = false;
        }
    }
    if ($valid) {
        //process input
    }
}
?>

实例checkbox多值获取

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get["food"];//www.3ppt.com
if (!empty($food)){
    echo "the foods selected are: <strong>";
    foreach($food as $foodstuff){
        echo '<br />'.htmlentities($foodstuff);
    }
    echo "</strong>.";
}
else {
    echo ('
    <form action="'. htmlentities($_server["php_self"]).'" method="get">
        <fieldset>
            <label>
                italian
                <input type="checkbox" name="food[]" value="italian" />
            </label>
            <label>
                mexican
                <input type="checkbox" name="food[]" value="mexican" />
            </label>
            <label>
                chinese
                <input type="checkbox" name="food[]" value="chinese" checked="checked" />
            </label>
        </fieldset>
        <input type="submit" value="go!" />
    </form> ');
    }
?>
</body>
</html>

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

语法
file_get_contents(path,include_path,context,start,max_length)参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context 可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。
 
start 可选。规定在文件中开始读取的位置。该参数是 php教程 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 php 5.1 新加的。


php代码

<?php 
function post($url, $post = null) 

    $context = array(); 
 
    if (is_array($post)) 
    { 
        ksort($post); 
 
        $context['http'] = array 
        ( 
            'method' => 'post', 
            'content' => http_build_query($post, '', '&'), 
        ); 
    } 
 
    return file_get_contents($url, false, stream_context_create($context)); 

 
$data = array 

    'name' => 'test', 
    'email' => 'test@gmail.com', 
    'submit' => 'submit', 
); 
 
echo post('http://localhost/5-5/request_post_result.php', $data); 
?>  
接收数据:

request_post_result.php  接收经过post的数据:
php代码
<?php 
echo $_post['name']; 
echo $_post['email']; 
echo $_post['submit']; 
echo "fdfd"; 
?>  


实例二

/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restserver.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
    $context = array(
        'http'=>array(
            'method'=>'post',
            'header'=>'content-type: application/x-www-form-urlencoded'."rn".
                      'user-agent : jimmy's post example beta'."rn".
                      'content-length: '.strlen($post_string)+8,
            'content'=>'mypost='.$post_string)
        );
    $stream_context = stream_context_create($context);
    $data = file_get_contents($remote_server,false,$stream_context);
    return $data;
}

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • php获取一个文件夹的mtime的程序

    php获取一个文件夹的mtime的程序了,这个就是时间问题了,对于这个问题我们来看小编整理的几个例子,具体的操作例子如下所示。 php很容易获取到一个文件夹的mtime,可以...2016-11-25
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • C#路径,文件,目录及IO常见操作汇总

    这篇文章主要介绍了C#路径,文件,目录及IO常见操作,较为详细的分析并汇总了C#关于路径,文件,目录及IO常见操作,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29
  • C#使用StreamWriter写入文件的方法

    这篇文章主要介绍了C#使用StreamWriter写入文件的方法,涉及C#中StreamWriter类操作文件的相关技巧,需要的朋友可以参考下...2020-06-25
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
  • 查找php配置文件php.ini所在路径的二种方法

    通常php.ini的位置在:复制代码 代码如下:/etc目录下或/usr/local/lib目录下。如果你还是找不到php.ini或者找到了php.ini修改后不生效(其实是没找对),请使用如下办法:1.新建php文件,写入如下代码复制代码 代码如下:<?phpe...2014-05-31