我想您想通过单击按钮重命名目录中的所有文件。$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";
}
}
?>