Avatar of evibesmusic
evibesmusicFlag for United States of America

asked on 

How can I create a new directory with predetermined contents?

Hello all experts,

I have a routine that creates a folder and names it 'username' where 'username' is a variable supplied by a user.

My question is; when this folder is created using the 'username' given by our user, can I automatically copy some files into it.

For example: I have a flash file that automatically recognizes and plays .mp3s, I want this file to be in each directory created so that when a user uploads their music, the file is their and therefore can automatically begin to play music if called on.

Please let me know if you need more information.  I have heard of this happening but, have not been able to find any example code.

The current routine to create the new folder with a supplied 'username' is below.

EVibesMusic
<?
$directoryname = $rows['username'];
 
if (is_dir('../'.$directoryname)) {
echo "Your profile page is located here: <a href='http://www.lionsshareworldmedia.com/test/members/$directoryname'>http://www.lionsshareworldmedia.com/test/members/$directoryname</a><br/><br/>";
}
 
if (!is_dir('../'.$directoryname)) {
// START CREATE DIR
mkdir($directoryname,0777);
echo "Your profile page is located here: <a href='http://www.lionsshareworldmedia.com/test/members/$directoryname'>http://www.lionsshareworldmedia.com/test/members/$directoryname</a><br/><br/>";
} else {
echo "Their was a problem creating your profile page.  Please contact the EVM Admin.";
}
//  END CREATE DIR}
?>

Open in new window

PHPScripting Languages

Avatar of undefined
Last Comment
evibesmusic
Avatar of hernst42
hernst42
Flag of Germany image

just add some lines like the following after:
mkdir($directoryname,0777);

copy('/full/path/to/this.mp3', $directoryname . '/default.mp3');
copy('/full/path/to/other.txr', $directoryname . '/other.txt');

Open in new window

Avatar of evibesmusic
evibesmusic
Flag of United States of America image

ASKER

Is it possible to copy all contents of a directory without having to specify each file within?

EVM
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of evibesmusic
evibesmusic
Flag of United States of America image

ASKER

Can you please specify the variable '$ds' for me cause with the code listed above I can't seem to get it to work.  I only ask about $ds cause I don't see a call to it anywhere in the code.

Is $ds like a built-in php function?

EVM
Avatar of hernst42
hernst42
Flag of Germany image

it's just junk, not needed should become $destination, but then realized it's alread in $directory.
Avatar of evibesmusic
evibesmusic
Flag of United States of America image

ASKER

hernst42:

Thanks for the help.  The code needed a little tweaking so I put the finished code underneath.

EVibesMusic
$dir = "/this/is/the/directory/you/want/to/copy/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (is_dir($dir . '/' . $file)) {
                continue;
            }
            copy($dir . '/' . $file, $directoryname . '/' . $file);
        }
        closedir($dh);
    }
}
}

Open in new window

PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo