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

Recommended Answers

All 2 Replies

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

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";
}
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.