duan7007 2013-08-22 13:02
浏览 41
已采纳

在按钮单击时重命名dir中的文件

I have an input field containing a file name and when submit is clicked it changes the files name.

If there is more than 1 file in the directory and I change the second files input it changes the first files name.

No matter how many files are in the directory it only changes the first files name.

if (isset($_POST['submit'])) {
 rename ($file_path.$file, $file_path.$_POST['new_name']);
 header("Location: /php rename/");
 exit();
 echo "name changed";
}
else {
  echo "name was not changed";
}

I have looked online and cannot find a script that does what I need it to do.

i want to be able to change the second files name when i click submit on the second file form and then third/fourth and so on.

    <?php
    if ($handle = opendir('./uploaded/')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") {
            echo'
    <a id="file-link" href="./uploaded/'.$file.'" target="_blank">'.$file.'</a>

<form method="post">
    <input type="text" name="new_name" value="'.$file.'">
    <input type="submit" id="submit" name="submit" value="submit">
</form>    
    ';

    $file_path = "./uploaded/";

                if (isset($_POST['submit'])) {

                    rename ($file_path.$file, $file_path.$_POST['new_name']);
                    header("Location: /php rename/");
                    exit();
                    echo "name changed";
                }
                else {
                        echo "name was not changed";
                    }


                }

            }
    closedir($handle);
    }
    ?>
  • 写回答

1条回答 默认 最新

  • duanshang9426 2013-08-22 13:40
    关注

    I suppose you want to rename all files in the directory on click of a button. $handle is already a handle to the directory and $file_path contains the path of the directory. Also assuming that the name of all input fields for new filename field is new_name[]. You need to iterate though each file in the directory and rename it like below:

    if (isset($_POST['submit'])) {
     $i = 0;
     while($file = readdir($handle)) {
      rename ($file_path.$file, $file_path.$_POST['new_name'][$i]);
      $i++;
     }
     header("Location: /php rename/");
     exit();
     echo "name changed";
    }
    else {
     echo "name was not changed";
    }
    

    Ajax Solution with Javascript

    test.php

    <html>
     <head>
      <script>
       function changename(id,dirpath){
        new_name = document.getElementById(id + "_new_name").value;
        old_name = document.getElementById(id + "_old_name").value;
    
        old_file_path= dirpath + old_name;
        new_file_path = dirpath + new_name;
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            document.getElementById("response").innerHTML=xmlhttp.responseText;
        document.getElementById(id + "_old_name").value=new_name;
          }
        }
        xmlhttp.open("GET","change_name.php?old_name="+old_file_path+"&new_name="+new_file_path,true);
        xmlhttp.send();
      }
     </script>
    </head>
    <body>
     <?php
      $dirpath="testdir/";
      $handle = opendir($dirpath);
      print '<div id="response"></div>';
      print "<table>";
      $i=0;
      while($file = readdir($handle)) {
       if($file!="." && $file!="..") {
        $parameters = $i . ",'"  . $dirpath . "'";
    print "<tr><td><input type='text' id='{$i}_new_name' value='{$file}' /></td><input type='hidden' id='{$i}_old_name' value='{$file}' /></td><td><input type='button' name='change' value='Change Name' onclick=\"changename({$parameters});\" /></td></tr>";
    $i++;
       }
      }
      print "</table>";
     ?>
     </body>
    </html>
    
    
    
    change_name.php
    
    <?php
     if(isset($_GET['old_name']) && isset($_GET['new_name'])) {
    $old_name = $_GET['old_name'];
    $new_name = $_GET['new_name'];
    
    if(file_exists($old_name)) {
        rename($old_name,$new_name);
        echo "file renamed from ($old_name) to ($new_name)";
    }
    else {
        echo "file does not exist";
    }
    }
    
    ?>
    

    Ajax Solution with Jquery

    test.php

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        $(".change_class").click(function() {
            dirpath = "testdir/";
    
            id = $(this).attr('id');
            console.log(id);
            new_name = $("#"+id+"_new_name").val();
            old_name = $("#"+id+"_old_name").val();
            console.log(new_name + " " + old_name);
    
            old_file_path= dirpath + old_name;
            new_file_path = dirpath + new_name;
    
            $.ajax(
                    {
                     url:"change_name.php?old_name="+old_file_path+"&new_name="+new_file_path,  
                     success:function(data) {
                                $("#response").text(data);
                                $("#"+id+"_old_name").val(new_name);
                            }
                    }
                );
    
        });
    });
    </script>
    </head>
    <body>
    <?php
    $dirpath="testdir/";
    $handle = opendir($dirpath);
    print '<div id="response"></div>';
    print "<table>";
    $i=0;
    while($file = readdir($handle)) {
    if($file!="." && $file!="..") {
        print "<tr><td><input type='text' id='{$i}_new_name' value='{$file}' /></td><input type='hidden' id='{$i}_old_name' value='{$file}' /></td><td><input type='button' name='change' class='change_class' value='Change Name' id='{$i}' /></td></tr>";
        $i++;
    }
    }
    print "</table>";
    ?>
    </body>
    </html>
    

    change_name.php

    <?php
    if(isset($_GET['old_name']) && isset($_GET['new_name'])) {
        $old_name = $_GET['old_name'];
        $new_name = $_GET['new_name'];
    
        if(file_exists($old_name)) {
            rename($old_name,$new_name);
            echo "file renamed from ($old_name) to ($new_name)";
        }
        else {
            echo "file does not exist";
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大