Log in

View Full Version : On register create new directory and copy files



motormichael12
11-24-2006, 11:13 PM
Does anyone know of a script where people cna register and when they do it makes a new directory and copies folders from another directory over to the directory they just made?

I want it to copy them over, but then make them separate so they don't edit it and it edits the original directory too.

thetestingsite
11-25-2006, 12:22 AM
To make the new directories you could simply use the mkdir(); (http://php.net/mkdir) function that PHP has. As far as coping folders over, I'm not sure I quite follow you. Does the user already have the folders on the server somewhere, then you just want to copy the files from there to the new directory? If so, you could also use the copy(); (http://us3.php.net/manual/en/function.copy.php) function. Let me know if this helps or not.

motormichael12
11-25-2006, 01:07 AM
i dont know how to use either of those functions, I haven't usd either before...

I have heard of mkdir though...

the copy thing, I want to put files on a folder somewhere on my site, then when the user registers it will copy the files from the hidden folder and then disconnect, therfore when you edit files in the new directory, it doesn't edit both the new directory AND the directory they were copied from...

thetestingsite
11-25-2006, 01:25 AM
Ok then lets just use the following (non-tested) code as an example. Edit the following as needed.



<?php
//Start Config

$homepath = "/home/mysite/public_html/members/";
$filesdir = "/home/mysite/hidden/";

//for windows systems, uncomment the following and comment above.
//$homepath = "C:/mysite/www/members/";
//$filesdir = "C:/mysite/hidden/";

/* Add the files in which you want to copy from the old to the new directory. Use the same format as below! */

$cpFiles = array("file1.txt","file2.png","file3.php","file4.dat");

// End config
reset($cpFiles);

$username = $_REQUEST["username"];
$userDir = $homepath.$username;

//create the new directory
$newDir = mkdir($userDir, 0777);


if (!$newDir) {

//If directory not created, end script and display error message

exit("The user directry was not created. Please try again or contact an Administrator.");

}

else {

//Begin copying files listed in the cpFiles array to the new directory.

foreach($cpFiles as $theFile) {

copy($filesdir.$theFile,$userDir.$theFile);

} //end foreach


} //end else

?>


Please note, that the directory that the ones will be created in must be chmod to 777 or all writeable. Let me know if you need any further help.

motormichael12
11-25-2006, 01:34 AM
in the


/* Add the files in which you want to copy from the old to the new directory. Use the same format as below! */

$cpFiles = array("file1.txt","file2.png","file3.php","file4.dat");


what if I want it to copy all files?

If I want all files from the directory "http://site.com/dir/test/" to copy to "http://site/directoryname/*here are all the files*"

would I make the script as copying from "http://site.com/dir/" and then the cpfiles array as "test/"?

then it would be like


$homepath = "http://site.com/dir";
$filesdir = "http://site.com/directoryname/";

/* Add the files in which you want to copy from the old to the new directory. Use the same format as below! */

$cpFiles = array("test/");

thetestingsite
11-25-2006, 04:41 AM
if you wanted to copy all the files, try the following:



<?php
//Start Config

$homepath = "/home/mysite/public_html/members/";
$filesdir = "/home/mysite/hidden/";

//for windows systems, uncomment the following and comment above.
//$homepath = "C:/mysite/www/members/";
//$filesdir = "C:/mysite/hidden/";

// End config

if ($dir = opendir($filesdir)) {

while (false !== ($file = readdir($dir)))
{

if ($file != "." && $file != "..") {

if (is_file($file)) {

$cpFiles[] = $file;

}
}

} //end while

reset($theFiles);


$username = $_REQUEST["username"];
$userDir = $homepath.$username;

//create the new directory
$newDir = mkdir($userDir, 0777);


if (!$newDir) {

//If directory not created, end script and display error message

exit("The user directry was not created. Please try again or contact an Administrator.");

}

else {

//Begin copying files listed in the cpFiles array to the new directory.

foreach($cpFiles as $theFile) {

copy($filesdir.$theFile,$userDir.$theFile);

} //end foreach


} //end else

?>


Note this will only take files (not directories) in the hidden folder you want to be copied over. That should get you started at least. Also note, that the $homepath and $filesdir variables are absolute paths to the folders, not relative. In other words, where on your server, are the items located? I'm not sure if this is the correct terms for the paths, but it would be "/home/sites/mysite/www/index.php" (or for windows using IIS: "c:/Inetpub/www/index.php") instead of "http://mysite.com/index.php" if that makes sense.

Again let me know if you need any further help.

motormichael12
11-25-2006, 07:37 PM
o like when i get errors how it will have like /var/something/morestuff/file.php is what i use?

thetestingsite
11-25-2006, 07:41 PM
exactly.

motormichael12
11-25-2006, 08:36 PM
okay so basically to find out I should make a page that has an error and copy the one that is there?

If there is some way to get it in like the web hosting area I can't because I am only 14 and my site is a sub domain on my dad's site... so the error one will work, right? if not I can't find it out through the hoster...

thetestingsite
11-25-2006, 08:40 PM
you could do it that way, or you could find out by putting this in a php page.



<?php
echo getcwd();

/*

will produce something like /home/public_html/myfolder/ or C:/www/myfolder/

*/
?>


Other than that, if you have any problems let me know. Or if you would like, I could set you up a subdomain on one of my websites, for testing purposes.

motormichael12
11-25-2006, 08:41 PM
Parse error: parse error, unexpected '=' in /homepages/37/d91075885/htdocs/domain/subdomain/file.php on line 2

/homepages/37/d91075885/htdocs/domain/subdomain/file.php would be it?

I blocked out things wiht domain and subdomain because the domain is my last name and subdomain is my first name making my site firstname.lastname.us

motormichael12
11-25-2006, 08:42 PM
no my subdomain is somehting my dad gave me solely for the purpose of my php testing, he had me make him a php script for his work and in return he gave me the subdomain for testing.

thetestingsite
11-25-2006, 08:44 PM
/homepages/37/d91075885/htdocs/domain/subdomain/ That would be one of the directories. Is that directory the one that the new users will have their directory created, or is it the one that has the files to be copied? You will need to find both for the script to work.

motormichael12
11-25-2006, 08:44 PM
/homepages/37/d91075885/htdocs/domain/subdomain

once again marked out... but thats wiht the command you gave me.

Thanks for all your help, if I have any more questions I will post them.

thetestingsite
11-25-2006, 08:46 PM
Sounds like a plan.

motormichael12
11-25-2006, 08:54 PM
I am oging to have question later, maybe oyu can answer it now possibly...

First of all, what is the difference between include('file) and require('file)

and if I set up a file to be included can I have like a variable before the include and have it work?

like:

$variable = 'this here'
include('file.txt')
(or require)


then file.txt is just "$variable"

will that make the text appear as "this here"?

thetestingsite
11-25-2006, 09:13 PM
I'm not sure I quite follow you one the variable part. As far as include and require, there is no difference except the way they handle errors. include produces a warning while require produces a Fatal Error.

If you made a PHP code like this:



<?php

$variable = 'this text';

include('file.txt');

echo '<br>'.$variable;

/* if there is any text or php statements in the file.txt, it will echo that. Otherwise it will just add whatever is in it. */

motormichael12
11-25-2006, 09:22 PM
the variable part, can you define variables for text before the include, then anyhting in the included file will change?

if oyu have

$test = 'pie'
require(file.txt)

then file.txt data is set as "I like $test"

will the outcome be "I like pie" or "I like $test", like iwll it use the variable even though the variable is in an included file and not the original file?

thetestingsite
11-25-2006, 09:30 PM
If you did that, then the .txt file would have to have php in it (so you would have to change the extensions from txt to php unless you have your server set up to parse txt as php [which is rare]) Otherwise it would only produce "I like $test". Look at the following example (taken from php.net)



vars.php



<?php

$color = 'green';
$fruit = 'apple';

?>


test.php



<?php

echo "A $color $fruit"; // will produce "A"

include 'vars.php';

echo "A $color $fruit"; // will produce "A green apple"

?>

thetestingsite
11-25-2006, 09:33 PM
Oh another way you could do it is like so:

test.php



<?php

$test = "This is a test";

include('file.php');

?>


file.php



<?php

echo 'I have written the following: '.$test;

?>

motormichael12
11-25-2006, 09:37 PM
well I wnat to make variables then make anohter page where the registered person could edit the txt file but not mess with any php parts, so that it has variables in it for most php areas so they cna just edit a certain part of a script, if there are any easier ways can you help me?

thetestingsite
11-26-2006, 12:01 AM
You know I'm still not following you on this part, but if what I think is right you want something like this:

main.txt



<?php

$var1 = "Variable 1";
$var2 = "Variable 2";
$username = "The user";

//any other variables the users want to add
?>


test.php



<?php
require('main.txt');

echo $var1.'<BR>'.$var2.'<BR>'.$username;

//displays all of the variables in main.txt
?>


If it's not what you are talking about, maybe you could "Paint A Picture" for me (explain it a little more). Let me know.

motormichael12
11-27-2006, 02:27 AM
nvm i will test it... i just wrote it and the internet messed up so i lost it all and i dont wanna rewrite it for you so i will do my own testing