我有一个包含文件名的输入字段,当点击提交时,它会更改文件名。
如果目录中有多个文件并且我更改了第二个文件输入,则会更改第一个文件名。
无论目录中有多少文件,它只会更改第一个文件名。
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);
}
?>
答案 0 :(得分: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解决方案
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";
}
}
?>
使用Jquery的Ajax解决方案
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";
}
}
?>