How to copy a file from one directory to another using PHP?
You could use the copy()
function :
// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');
Quoting a couple of relevant sentences from its manual page :
Makes a copy of the file source to
dest.
If the destination
file already exists, it will be
overwritten.
Copy all files from one folder to another Folder using PHP scripts?
// images folder creation using php
$mydir = dirname( __FILE__ )."/html/images";
if(!is_dir($mydir)){
mkdir("html/images");
}
// Move all images files
$files = glob("images/*.*");
foreach($files as $file){
$file_to_go = str_replace("images/","html/images/",$file);
copy($file, $file_to_go);
}
Copy file into new directory (PHP)
If I understand you well, you want to copy career.php template from /associates/ folder to /associates/username/ folder
$rootfolder = $_SERVER['DOCUMENT_ROOT'];
$name = $user->username;
$thisdir = "/associate/";
$folderPath = $rootfolder.$thisdir.$name."/"; // desired directory
if(!is_dir($folderPath)){
mkdir($folderPath, 0777, true);
}
$currentfile = $rootfolder.$thisdir.'joshua/career.php';
$destination = $folderPath.'career.php';
copy($currentfile, $destination); // copy this file into new directory
Copying an image file from one directory to another using php
If the PHP script is in the conf_images
folder, then the paths need to be relative to that, so should be just
$source = "room";
$destination = "room/DTGLA";
Copy existing file into new directory using PHP
Copy function in php accept two names and not only directory.
So instead
copy("file1.txt", $folderPath)
do
copy("file1.txt", $folderPath . "/file1.txt")
and use error feedback to understanding better
if (copy("file1.txt", "folder1/file1.txt")) {
echo "File Copied";
} else {
$errors= error_get_last();
echo "File not copied, " . $errors["messages"];
}
Related Topics
Ternary Operator Left Associativity
Setting Value of a HTML Form Textarea
Read a File Backwards Line by Line Using Fseek
How to Sum N Number of Time (Hh:Mm Format)
What Is Pdo & Why Should I Use It
What Is the Most Efficient Way to Count All the Occurrences of a Specific Character in a PHP String
Parse Xml Namespaces with PHP Simplexml
Best Way to Transfer an Array Between PHP and JavaScript
How to Send Emails via Cron Job Usng PHP MySQL
Fix Malformed Xml in PHP Before Processing Using Domdocument Functions
How to Create Codeigniter Batch Insert Array
MySQL No Connection Could Be Made Because the Target MAChine Actively Refused It
Hide Shipping Methods for Specific Shipping Class in Woocommerce