How to Copy a File from One Directory to Another Using PHP

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



Leave a reply



Submit