0

Hello, so the code i have gets the files in the directory and lists them in a dropdown. what im trying to do is rename the selected dropdown file with the input of the text field. error i get is:

Warning: rename(test.php,) [function.rename]: No such file or directory in.....

For some reason its adding a comma ( , ) after the selected file from the dropdown.
I have tried a few differant ways but come up with the same problem.
Any kind of help would be appreciated.
Thank you

<form name="form" method="post">
                   Rename file:<input type="text" maxlength="24" style="background:#000006; color: #FF0000" value="<?php echo htmlspecialchars($text_box); ?>"

 name="new_name" size="30"/>

<br /> <input type="submit" name="submit" id="submit" value="Rename" />


<?php

echo "<form>";

echo "<select name='yourfiles'>";

$dirpath = "./";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {

if (!is_dir("$dirpath/$file")) {

echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
}
}
closedir($dh);

echo "</select>";
echo "</form>";

$old = $_POST['yourfiles'];
$new = $_POST['$text_box'];
if($_POST['submit'])
{

if (!rename($old,$new)) {
    echo "failed to rename $old...\n";
}
}
?>
2
Contributors
2
Replies
15
Views
3 Years
Discussion Span
Last Post by Tko_1
1

1) you just try to find values that it post

echo "<pre>";
print_r($_POST);
echo "</pre>";

2) ALSO GIVE NAME TO your text box and use same name when renaming

<input type=text name='newname'>

.
.
.
$new=$_POST['newname'];

3) I think you have to keep directory name with filenames and ensure that web user have permission to access that directory to rename

0

Thank you urtrivedi, i knew i was missing something simple..
Thanks so much.

<form name="form" method="post">
                   Rename file:<input type="text"name='newname' maxlength="24" style="background:#000006; color: #FF0000" value="<?php echo htmlspecialchars($text_box); ?>"

<br /> <input type="submit" name="submit" id="submit" value="Rename" />


<?php

echo "<form>";

echo "<select name='yourfiles'>";

$dirpath = "./";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {

if (!is_dir("$dirpath/$file")) {

echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
}
}
closedir($dh);

echo "</select>";
echo "</form>";
$old = $_POST['yourfiles'];
$path = $old;
$ext = ltrim(strstr($path, '.'), '.');
$new = $_POST['newname'] . "." . $ext;
if($_POST['submit'])
{

if (!rename($old,$new)) {
    echo "failed to rename $old...\n";
}
else {
echo "$old was successfully renamed to $new";
}
}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.