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 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线