Skip to content
Advertisement

How to copy a file then rename it based on input field via buttonclick?

Good day experts!

I want to copy a file and rename it based on input value via button click. MY code is not working. There is no file being copied nor being renamed. Here’s my code:

<?php
error_reporting(0);

if($_POST['action'] == 'call_this') {
  echo Success!;
};

$file = 'data.php';
$newfile = '$_GET['subject'].php';
echo copy($file, $newfile);
  
?>

<form action="<?php echo $newfile ?>" method="get">
<input type="text" name="subject" required>
<button type="submit"><a href="" onclick="change()">Change</a></button>
</form>

<script>
function change() {
      $.ajax({
           type: "POST",
           url: 'data.php',
           data:{action:'call_this'},
           success:function(html) {
             alert(html);
           }

      });
 }
</script>

Advertisement

Answer

i think this is what you looking for:

<html>
<body>
<form method="post" action="copy.php">
<input type="text" placeholder="new name" name="newFileName"/>
<input type="submit" value="Change"/>
</form>
</body>
</html>

copy.php :

<?php
$file = 'sample.txt';
$newfile = $_POST["newFileName"].'.txt';

if (!copy($file, $newfile)) {
    echo "failed to copy";
}else {
    echo "copy with new name";
}
?>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement