php 网页ftp 代码三 创建目录

 更新时间:2016年11月25日 16:31  点击:1866

<?php
    $ftpserver=$_POST[ftpserver];
    $ftpport=$_POST[ftpport];
    $ftpuser=$_POST[ftpuser];
    $ftppassword=$_POST[ftppassword];
    $ftp=@ftp_connect($ftpserver,$ftpport);
    if(!$ftp){ echo "连接FTP服务器".$ftpserver."的端口".$ftpport."失败";exit;}
    $rs=@ftp_login($ftp,$ftpuser,$ftppassword);
    if(!$rs){ echo "用户名或密码错误,连接FTP服务器失败";exit;}
    $curDir=stripslashes($_POST[curDir]);
    $dirname=stripslashes($_POST[name]);
    
    if($dirname)
    {
     if($curDir=="/")
     {
      $dirpath=$curDir.$dirname;
     }else{
      $dirpath=$curDir."/".$dirname;
     }
     $rs=ftp_mkdir($ftp,$dirpath);
     if($rs)
     {
      echo "<script>alert('创建文件夹成功');history.back();</script>";
      exit;
     }else{
      echo "<script>alert('创建文件夹失败');history.back();</script>";
      exit;
     }
    
    }else{
      echo "<script>alert('文件夹不能为空');history.back();</script>";
      exit;
    }
?>
<?php
    $ftpserver=$_POST[ftpserver];
    $ftpport=$_POST[ftpport];
    $ftpuser=$_POST[ftpuser];
    $ftppassword=$_POST[ftppassword];
    $ftp=@ftp_connect($ftpserver,$ftpport);
    if(!$ftp){ echo "连接FTP服务器".$ftpserver."的端口".$ftpport."失败";exit;}
    $rs=@ftp_login($ftp,$ftpuser,$ftppassword);
    if(!$rs){ echo "用户名或密码错误,连接FTP服务器失败";exit;}
    $targetDir=str_replace("\","/",stripslashes($_POST[targetDir]));
    $filename=substr(strrchr($_POST[filelist],"/"),1);
    echo $filename;
//    exit;
    if(substr($targetDir,strlen($targetDir)-1)=="/")
    {
     $rs=@ftp_get($ftp,$targetDir.$filename,$_POST[filelist],FTP_ASCII);
    }else{
     $rs=@ftp_get($ftp,$targetDir."/".$filename,$_POST[filelist],FTP_ASCII);
    }
    if($rs)
    {
     if(substr($targetDir,strlen($targetDir)-1)=="/")
     {
      echo "<script>alert('文件下载成功,文件在本机中路径为[".$targetDir.$filename."]');window.close();</script>";
     }else{
      echo "<script>alert('文件下载成功,文件在本机中路径为[".$targetDir."/".$filename."]');window.close();</script>";
     }
     exit;我是
    }else{
     echo "<script>alert('文件下载失败,请检查目标文件夹是否存在,源文件为[".$_POST[filelist]."]');window.close();</script>";
     exit;
    }
?>
<?php
    $ftpserver=$_POST[ftpserver];
    $ftpport=$_POST[ftpport];
    $ftpuser=$_POST[ftpuser];
    $ftppassword=$_POST[ftppassword];
    $ftp=@ftp_connect($ftpserver,$ftpport);
    if(!$ftp){ echo "连接FTP服务器".$ftpserver."的端口".$ftpport."失败";exit;}
    $rs=@ftp_login($ftp,$ftpuser,$ftppassword);
    if(!$rs){ echo "用户名或密码错误,连接FTP服务器失败";exit;}
    $curDir=stripslashes($_POST[curDir]);
    $localfile=str_replace("\","/",stripslashes($_POST[file1]));
    
    if($localfile)
    {
     $filename=substr(strrchr($localfile,"/"),1);
     if($curDir=="/")
     {
      $remotefile=$curDir.$filename;
     }else{
      $remotefile=$curDir."/".$filename;
     }
     $rs=ftp_put($ftp,$remotefile,$localfile,FTP_ASCII);
     if($rs)
     {
      echo "<script>alert('上传文件成功');history.back();</script>";
      exit;
     }else{
      echo "<script>alert('上传文件失败');history.back();</script>";
      exit;
     }
    
    }else{
      echo "<script>alert('没有选择上传文件');history.back();</script>";
      exit;
    }
?>

php 网页ftp 代码

<?php
 $ftpserver="127.0.0.1";
 $ftpport="21";
 $ftpuser="anonymous";
 $ftppassword="";
 if($_POST)
 {
  $action=$_POST[action];
  switch($action)
  {
   case "open":
   case "change":
    $ftpserver=$_POST[ftpserver];
    $ftpport=$_POST[ftpport];
    $ftpuser=$_POST[ftpuser];
    $ftppassword=$_POST[ftppassword];
    $ftp=@ftp_connect($ftpserver,$ftpport);
    if(!$ftp){ echo "连接FTP服务器".$ftpserver."的端口".$ftpport."失败";exit;}
    $rs=@ftp_login($ftp,$ftpuser,$ftppassword);
    if(!$rs){ echo "用户名或密码错误,连接FTP服务器失败";exit;}
    $curDir=$_POST[curDir];
    if($curDir=="") $curDir="/";
    if($curDir=="/")
    {
     $parentDir="/";
    }else{
     if(strrpos($curDir,"/")==0)
     {
      $parentDir="/";
     }else{
      $parentDir=substr($curDir,0,strrpos($curDir,"/"));
     }
    }
    $arr=ftp_rawlist($ftp,$curDir);
    if(count($arr)>1)
    {
     foreach($arr as $val)
     {
         if($curDir=="/")
      {
          $val="/" . trim(strrchr($val," "));
      }else{
          $val=$curDir . "/" . trim(strrchr($val," "));
      }
      
      $file_size=ftp_size($ftp,$val);
      if($file_size==-1)
      {//为目录
       $dirlist[]=str_replace("\\","/",$val);
      }else{
       $filelist[]=str_replace("\\","/",$val);
      }
     }
    }
    break;
   case "close":
    break;
   
   
  }
 
 }

下面来看看WEB页面形式了.


?>

    真正支持单文件和多文件上传类代码,修正了$_FILES[$field]['name']中的$field不能用变量只能和表单中的文件名name="userfile"一致的缺点$_FILES['userfile']['name'],这里<input type="file" name="userfile"> 中的文件名可以随意取。

//index.htm
1、单文件上传
<form method="post" action="./upload.php" name="frmUpload" enctype="multipart/form-data" >
<input type="file" name="userfile" style="WIDTH: 282px">
<input type="submit" align="center" name="upfiles" value="确定"></form>
2、多文件上传
<form method="post" action="./upload.php" name="frmUpload" enctype="multipart/form-data" >
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="submit" align="center" name="upfiles" value="确定">
</form>
--------------------------------------------------------------------------------------------------------------------------------
//upload.php
<?php

class File_upload{
public $upload_path='./upload/';//上传文件的路径
public $allow_type=array();//允许上传的文件类型
public $max_size='20480';//允许的最大文件大小
public $overwrite=false;//是否设置成覆盖模式
public $renamed=false;//是否直接使用上传文件的名称,还是系统自动命名

/**
* 私有变量
*/
private $upload_file=array();//保存上传成功文件的信息
private $upload_file_num=0;//上传成功文件的数目
private $succ_upload_file=array();//成功保存的文件信息
/**
* 构造器
*
* @param string $upload_path
* @param string $allow_type
* @param string $max_size
*/
public function __construct($upload_path='./upload/',$allow_type='jpg|bmp|png|gif|jpeg',$max_size='204800')
{
$this->set_upload_path($upload_path);
$this->set_allow_type($allow_type);
$this->max_size=$max_size;
$this->get_upload_files();
}
/**
* 设置上传路径,并判定
*
* @param string $path
*/
public function set_upload_path($path)
{
if(file_exists($path)){
if(is_writeable($path)){
$this->upload_path=$path;
}else{
if(@chmod($path,'0666'))
$this->upload_path=$path;
}
}else{
if(@mkdir($path,'0666')){
$this->upload_path=$path;
}
}
}
//设置上传文件类型
public function set_allow_type($types){
$this->allow_type=explode("|",$types);
}
//上传文件
public function get_upload_files()
{
foreach ($_FILES AS $key=>$field)
{
$this->get_upload_files_detial($key);
}
}
//上传文件数据存放到数组中
public function get_upload_files_detial($field){
if(is_array($_FILES["$field"]['name']))
{
for($i=0;$i<count($_FILES[$field]['name']);$i++)
{
if(0==$_FILES[$field]['error'][$i])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'][$i];
$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'][$i];
$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'][$i];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'][$i];
$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'][$i];
$this->upload_file_num++;
}
}
}
else {
if(0==$_FILES["$field"]['error'])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES["$field"]['name'];
$this->upload_file[$this->upload_file_num]['type']=$_FILES["$field"]['type'];
$this->upload_file[$this->upload_file_num]['size']=$_FILES["$field"]['size'];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES["$field"]['tmp_name'];
$this->upload_file[$this->upload_file_num]['error']=$_FILES["$field"]['error'];
$this->upload_file_num++;
}
}
}
/**
* 检查上传文件是构满足指定条件
*
*/
public function check($i)
{
if(!empty($this->upload_file[$i]['name'])){
//检查文件大小
if($this->upload_file[$i]['size']>$this->max_size*1024)$this->upload_file[$i]['error']=2;
//设置默认服务端文件名
$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];
//获取文件路径信息
$file_info=pathinfo($this->upload_file[$i]['name']);
//获取文件扩展名
$file_ext=$file_info['extension'];
//检查文件类型
if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i]['error']=5;
//需要重命名的
if($this->renamed){
list($usec, $sec) = explode(" ",microtime());
$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;
unset($usec);
unset($sec);
}
//检查文件是否存在
if(file_exists($this->upload_file[$i]['filename'])){
if($this->overwrite){
@unlink($this->upload_file[$i]['filename']);
}else{
$j=0;
do{
$j++;
$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$this->upload_file[$i]['filename']);
}while (file_exists($temp_file));
$this->upload_file[$i]['filename']=$temp_file;
unset($temp_file);
unset($j);
}
}
//检查完毕
} else $this->upload_file[$i]['error']=6;
}
/**
* 上传文件
*
* @return true
*/
public function upload()
{
$upload_msg='';
for($i=0;$i<$this->upload_file_num;$i++)
{
if(!empty($this->upload_file[$i]['name']))
{
//检查文件
$this->check($i);
if (0==$this->upload_file[$i]['error'])
{
//上传文件
if(!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename']))
{
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!<br>';
}else
{
$this->succ_upload_file[]=$this->upload_file[$i]['filename'];
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 成功了<br>';
}
}else $upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!<br>';
}
}
echo $upload_msg;
}
//错误信息
public function error($error)
{
switch ($error) {
case 1:
return '文件大小超过php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
return '文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
return '文件只有部分被上传';
break;
case 4:
return '没有文件被上传';
break;
case 5:
return '这个文件不允许被上传';
break;
case 6:
return '文件名为空';
break;
default:
return '出错';
break;
}
}
//获取成功的数据信息为数组(备用)
public function get_succ_file(){
return $this->succ_upload_file;
}
}
$upload=new File_upload('./upload/','jpg|bmp|png|gif|jpeg');
$upload->upload();
$t=$upload->get_succ_file();
print_r($t);

?>

[!--infotagslink--]

相关文章

  • 解决Pycharm的项目目录突然消失的问题

    今天小编就为大家分享一篇解决Pycharm的项目目录突然消失的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-22
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • C#路径,文件,目录及IO常见操作汇总

    这篇文章主要介绍了C#路径,文件,目录及IO常见操作,较为详细的分析并汇总了C#关于路径,文件,目录及IO常见操作,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • JS创建Tag标签的方法详解

    这篇文章主要介绍了JS创建Tag标签的方法,结合具体实例形式分析了javascript动态操作页面HTML元素实现tag标签功能的步骤与相关操作技巧,需要的朋友可以参考下...2017-06-15
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • JavaScript动态创建div属性和样式示例代码

    1.创建div元素: Javascript代码 复制代码 代码如下: <scripttypescripttype="text/javascript"> functioncreateElement(){ varcreateDiv=document.createElement("div"); createDiv.innerHTML="Testcreateadiveleme...2013-10-13
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • docker 启动elasticsearch镜像,挂载目录后报错的解决

    这篇文章主要介绍了docker 启动 elasticsearch镜像,挂载目录后报错的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-20