Question

I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.

copy(file,dest) does not work. rename(file,dest) moves the files to the new folder but I lose them in the repository.

How do I copy the files in one folder to the new folder without losing the files in the original folder?

$dest = '../'.$menuLocation.'/'.$pageName; 
$file1= "repository/index.html"; 
$file2= "repository/content.php"; 
mkdir($dest,0777); 
rename($file1,$dest.'/index.html'); 
rename($file2,$dest.'/content.php');

$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.

Was it helpful?

Solution

For anyone hunting for the solution to this:

when using copy() in php you also need to include the file name.

copy(orginalfile,destination_with_filename);

for example:

wrong:

copy('/temp/sports/basketball.jpg','/images/sports/')

Correct:

copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')

OTHER TIPS

Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top