creating a directory and automatically copy a file there

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

creating a directory and automatically copy a file there

Post by m3mn0n »

so far, that is impossible to me

i've looked into copy(), mkdir(), but i have yet to see a variation that enables both to work together.

i'm way to inexpirenced to script my own code up that mixes thoses two, does anyone know how or have a tutorial that shows you?

thanks.
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

THis is how I do it

Code: Select all

<?php
$folder = new_dir;//name the dir

	//create the dir, and chmod to 777
	mkdir($folder, 0777);
	
	
	  //read the dir, if no files (which there shouldn't be), then create the files
	  $file = readdir($folder);
	  if(!$file):
	  	//moves back into the new dir
		chdir($folder);
		
		//starts the creation of the new files
		$newfilename = "your_file_name.ext";
		$newfile = fopen($newfilename,"w");
		$content = "Whatever you want written to the new file";

		fwrite($newfile,$content);
		fclose($newfile);

?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

thanks, i'll try to modify it ti copy the 10 files i wanted auto placed. 8)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

mission impossible. :?

does anyone have a version of that, which adds files and makes a directory in a new directory?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not sure if I got the point, but if you want to create (possibly) a directory tree try this one (completely untested)

Code: Select all

<?php
function createDirectory($path)
{
	// testings if you'd like
	// $path maybe already a file/dir
	mkdir($path);
}

function copyTo($src, $dest)
{
	$path = split('/', $dest);
	$dest = '';
	$file = array_pop($path);

	foreach($path as $subpath)
	{
		$dest .= $subpath.'/';
		createDirectory($dest);
	}
	$dest .= $file;
	return copy ($src, $dest);
}
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

thanks, but shouldn't that include what files to copy from where?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$src is the path to the file you want to copy
take a look at http://www.php.net/manual/en/function.copy.php
Post Reply