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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程