duan7007
duan7007
2013-08-22 13:02

在按钮单击时重命名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 duanshang9426 8年前

    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";
        }
    }
    
    ?>
    
    点赞 评论 复制链接分享