与文件系统的和服务器的交互

文件上传

文件上传的 HTML 代码

<html>
 <head>
  <title>Administration - upload new files</titlr>
 </head>
 <body>
  <h1> Upload new news files</h1>
  <form action="upload.php" method="post" enctype="multipart/form-data"/>
   <div>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <lable for="userfile">Upload a file:</lable>
    <input type="file" name="userfile" id="userfile"/>
    <input type="submit" value="Send File"/>
   </div>
  </form>
 </body>
</html>

编写处理文件的PHP

在这里插入图片描述
在这里插入图片描述


//Check to see if an error code was generated on the upload attempt
  if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:	echo 'File exceeded upload_max_filesize';
	  			break;
      case 2:	echo 'File exceeded max_file_size';
	  			break;
      case 3:	echo 'File only partially uploaded';
	  			break;
      case 4:	echo 'No file uploaded';
	  			break;
	  case 6:   echo 'Cannot upload file: No temp directory specified.';
	  			break;
	  case 7:   echo 'Upload failed: Cannot write to disk.';
	  			break;
    }
    exit;
  }

  // Does the file have the right MIME type?
  if ($_FILES['userfile']['type'] != 'text/plain')
  {
    echo 'Problem: file is not plain text';
    exit;
  }

  // put the file where we'd like it
  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  {
     if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  } 
  else 
  {
    echo 'Problem: Possible file upload attack. Filename: ';
    echo $_FILES['userfile']['name'];
    exit;
  }


  echo 'File uploaded successfully<br><br>'; 

  // reformat the file contents
  $fp = fopen($upfile, 'r');
  $contents = fread ($fp, filesize ($upfile));
  fclose ($fp);
 
  $contents = strip_tags($contents);
  $fp = fopen($upfile, 'w');
  fwrite($fp, $contents);
  fclose($fp);

  // show what was uploaded
  echo 'Preview of uploaded file contents:<br><hr>';
  echo $contents;
  echo '<br><hr>';

?>

在这里插入图片描述
在这里插入图片描述

使用目录函数

从目录读取

  $current_dir = '/uploads/';
  $dir = opendir($current_dir);

  echo "<p>Upload directory is $current_dir</p>";
  echo '<p>Directory Listing:</p><ul>';
  
  while(false !== ($file = readdir($dir)))
    //strip out the two entries of . and ..
  	if($file != "." && $file != "..")
  	{
    	  echo "<li>$file</li>";
  	}
  echo '</ul>';
  closedir($dir);

在这里插入图片描述

  $dir = dir("/uploads/");

  echo "<p>Handle is $dir->handle</p>";
  echo "<p>Upload directory is $dir->path</p>";
  echo '<p>Directory Listing:</p><ul>';
  
  while(false !== ($file = $dir->read()))
    //strip out the two entries of . and ..
  	if($file != "." && $file != "..")
  	{
		echo '<a href="filedetails.php?file='.$file.'">'.$file.'</a><br>';
  	}
  echo '</ul>';
  $dir->close();
  • dir 类

获取当前目录信息

在这里插入图片描述

创建和删除目录

在这里插入图片描述

与文件系统的交互

  $current_dir = '/uploads/';
  $file = basename($file);  // strip off directory information for security

  echo '<h1>Details of file: '.$file.'</h1>'; 
  $file = $current_dir.$file;

  echo '<h2>File data</h2>';
  echo 'File last accessed: '.date('j F Y H:i', fileatime($file)).'<br>';
  echo 'File last modified: '.date('j F Y H:i', filemtime($file)).'<br>';

  //$user = posix_getpwuid(fileowner($file));
  echo 'File owner: '.$user['name'].'<br>';

  //$group = posix_getgrgid(filegroup($file));  
  echo 'File group: '.$group['name'].'<br>';
  
  echo 'File permissions: '.decoct(fileperms($file)).'<br>';
  
  echo 'File type: '.filetype($file).'<br>';

  echo 'File size: '.filesize($file).' bytes<br>';

 
  echo '<h2>File tests</h2>';

  echo 'is_dir: '.(is_dir($file)? 'true' : 'false').'<br>';
  echo 'is_executable: '.(is_executable($file)? 'true' : 'false').'<br>';
  echo 'is_file: '.(is_file($file)? 'true' : 'false').'<br>';
  echo 'is_link: '.(is_link($file)? 'true' : 'false').'<br>';
  echo 'is_readable: '.(is_readable($file)? 'true' : 'false').'<br>';
  echo 'is_writable: '.(is_writable($file)? 'true' : 'false').'<br>';

在这里插入图片描述

更改文件属性

在这里插入图片描述

创建、删除和移动文件

  • touch 创建文件、或者修改文件上次被修改的时间
bool touch(string file, [int time [,int atime]])

在这里插入图片描述
在这里插入图片描述

使用程序执行函数

在这里插入图片描述

   chdir('/uploads/');

/ exec version
   echo '<pre>';
   
   // unix
   exec('ls -la', $result);
   // windows
   // exec('dir', $result);
   foreach ($result as $line) 
     echo "$line\n";

   echo '</pre>';
   echo '<br><hr><br>';

/ passthru version
   echo '<pre>';
   
   // unix
   passthru('ls -la');
   // windows
   // passthru('dir');

   echo '</pre>';
   echo '<br><hr><br>';
 
/ system version 
   
   echo '<pre>';
   // unix
   $result = system('ls -la');
   // windows
   // $result = system('dir');
   echo '</pre>';
   echo '<br><hr><br>';

/backticks version
   
   echo '<pre>';
   // unix
   $result = `ls -al`;
   // windows
   // $result = `dir`;
   echo $result;
   echo '</pre>';


与环境变量交互: getenv() 和 putenv()

  • 改变和获取运行php 的服务器上的环境变量
  • phpinfo() 获得PHP所有环境变量的列表
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值