Page 1 of 1

creating a directory and automatically copy a file there

Posted: Thu Oct 10, 2002 2:42 am
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.

Posted: Thu Oct 10, 2002 8:52 am
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);

?>

Posted: Thu Oct 10, 2002 2:40 pm
by m3mn0n
thanks, i'll try to modify it ti copy the 10 files i wanted auto placed. 8)

Posted: Thu Oct 10, 2002 6:35 pm
by m3mn0n
mission impossible. :?

does anyone have a version of that, which adds files and makes a directory in a new directory?

Posted: Thu Oct 10, 2002 6:58 pm
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);
}
?>

Posted: Sat Oct 12, 2002 7:31 am
by m3mn0n
thanks, but shouldn't that include what files to copy from where?

Posted: Sat Oct 12, 2002 10:23 am
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