douci2516 2011-03-24 10:52
浏览 69
已采纳

PHP将所选文件从A文件夹移动到B文件夹

This is a tricky one and I'm not sure where to start, so any help will be grateful.

I have a parent folder called 'source' (c:/dev/source) which contains several child folders.

I need a PHP script that will display the child folders with checkboxes next to each, and a text field for a new folder name, allowing users to tick the checkboxes of the ones they want to copy to a 'destination of c:/dev/destination/the_folder_name_they_typed_in

When they click submit, the selected child folders will be copied from c:/dev/source to c:/dev/destination/the_folder_name_they_typed_in

This is all running on a local internal development server. The child folders will always be in c:/dev/source/

  • 写回答

2条回答 默认 最新

  • duaner1634 2011-03-24 11:04
    关注

    Here's a not well known little bit of code called DirectoryIterator. It's not fully documented on the PHP site, but heres the jist of it:

    Create a list of files and folders with checkboxes next to them, slap them all in an array.

    $Directory = new RecursiveDirectoryIterator('c:/dev/source');
    $Iterator = new RecursiveIteratorIterator($Directory);    
    
    ?><form method="post"><?
    foreach($Iterator  as $r){
    
           if($r->isDot()) continue;
    
           echo "<input type=\"checkbox\" name=\"copy[]\" value=\"".($r->getSubPathName())."\"> ".$r->getSubPathName() . " <br>";
    
    }
    ?></form><?
    

    Now add this part to the top of the file

    <?php 
    if($_POST){
    if(is_array($_POST['copy'])) foreach($_POST['copy'] as $c){
     @copy($c, str_replace('c:/dev/source','c:/dev/dest', $c));
      echo "copied: $c to ". str_replace('c:/dev/source','c:/dev/dest', $c) . "<br>";
    
    }
    }
    

    I'm not fully sure what result you get from $r->getSubPathName() can you let me know if it outputs an array? if so it might be that you replace that with $r->getSubPath() and then add the "c:/dev/source" to the variable $c when you copy it?

    Further Reading:

    here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化