Home > Mobile >  How to copy a file inside a newly created folder and rename the file?
How to copy a file inside a newly created folder and rename the file?

Time:02-26

Good day experts!

I want to copy a file inside a newly created folder and rename the file but it seems there's something wrong with codes and I cant figure it out. Here's my code:

<?php
$name = $_POST["newFileName"];
$folder = mkdir($name);
session_start();
$name = $_POST["newFileName2"];
$file = 'data.php';
$newfile = $folder/$_POST["newFileName2"].'.php';

file_exists($newfile) && die(" <center><br><br><br><br>The Exam name already exists.! Change it! <br><br> <a href='quiz.php'><button>GO BACK</button></a>");

if (!copy($file, $newfile)) {
    echo "Failed to create Quiz";
}else {
   echo "Created Successfully";
}
?>

CodePudding user response:

session_start();

if ( !isset($_POST["newFileName"], $_POST["newFileName2"]) ) {
    echo "An meaningful error message";
    exit;
}
$existingFile = 'data.php';

// $_POST["newFileName"] is the new folder name
// may have been better named $_POST["newFolder"] :)
$folder = '';
$folder = $_POST["newFileName"];
if ( ! mkdir($folder) ) {
    echo 'Cannot create a folder with that name';
    exit;
}

$newFilename = $_POST["newFileName2"];
$newfile = "$folder/$_POST[newFileName2].php";

if ( file_exists($newfile) ) {
    echo " <center><br><br><br><br>The Exam name already exists.! Change it! <br><br> <a href='quiz.php'><button>GO BACK</button></a>";
    exit;
}  

if (!copy($existingFile, $newfile)) {
    echo "Failed to create Quiz";
}else {
   echo "Created Successfully";
}

  • Related