0

我有一个包含文件名的输入字段,当单击提交时,它会更改文件名。

如果目录中有多个文件并且我更改了第二个文件输入,它会更改第一个文件名。

无论目录中有多少文件,它只会更改第一个文件名。

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";
}

我在网上查看过,找不到可以满足我需要的脚本。

当我单击第二个文件表单上的提交,然后单击第三/第四个等等时,我希望能够更改第二个文件名。

    <?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);
    }
    ?>
4

1 回答 1

1

我想您想通过单击按钮重命名目录中的所有文件。$handle 已经是目录的句柄并且 $file_path 包含目录的路径。还假设新文件名字段的所有输入字段的名称是 new_name[]。您需要遍历目录中的每个文件并将其重命名如下:

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";
}

使用 Javascript 的 Ajax 解决方案

测试.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";
}
}

?>

使用 Jquery 的 Ajax 解决方案

测试.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>

更改名称.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";
    }
}

?>
于 2013-08-22T13:40:38.623 回答