LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   will this script copy only certain files? (https://www.linuxquestions.org/questions/programming-9/will-this-script-copy-only-certain-files-317705/)

verbatim 04-27-2005 10:00 AM

will this script copy only certain files?
 
i want to copy all the files [but none of the folders] from one folder to another.

To copy only the files and not the folders will this work:

Code:

<?php
//copy dep files
$dep = "/home/abc/dep/*";

$dep_new ="/home/xxx/mainwebsite/new";
copy ($dep, $dep_new);
?>


mschutte 04-27-2005 10:31 AM

The code looks like PHP, but if you had omitted the comment, it would be Perl... Please, mention the programming language you are using, so we do not have to guess. (Including the starters <?php and ?> would also help.)

Solution for PHP:
PHP Code:

define('TARGETDIR''/home/xxx/mainwebsite/new/');
foreach (
glob('/home/abc/dep/*') as $dep) {
    if (
is_file($dep))    # Exclude directories
        
copy($depTARGETDIR basename($dep));


Well, I haven't tested the code above.

Hope I could help,
mschutte

verbatim 04-27-2005 01:00 PM

thanks alot, it seems to work. Just one problem.

Code:

define('TARGETDIR', '/home/xxx/mainwebsite/new/'');
foreach (glob('/home/abc/dep/*') as $dep) {
    if (is_file($dep))    # Exclude directories
        copy($dep, TARGETDIR . basename($dep));
}

seems to work but i get the following error for every file that should be transfered [

Warning: copy(/home/abc/dep/*"misc_filename".php): failed to open stream: No such file or directory in /var/www/html/"scriptname".php): on line 101

line 101 :
Code:

  copy($dep, TARGETDIR . basename($dep));
any quick solutions??

mschutte 04-27-2005 01:45 PM

Well, it doesn't match the error message, but does the target directory exist? Oh, and does the httpd user have reading permissions to the source and target directories? The httpd user is commonly 'wwwrun', 'apache', ... depending on the distro.
It does work at my host.

verbatim 04-27-2005 02:03 PM

the target is made prior in the script, other folders are linked to the target folder, then i place your copy files script to copy the files.

so in theory it should work, but i cant get it for some reason.

Code:

// Define directory
$dir = "./new/{$username}/";

// make directory
if (!(is_dir($dir))) {
    if (mkdir($dir, 0755)) {
        echo "-";
      }
    else {
        echo ",";
      }}




//define symlink
$templates = "/home/misc/templates";
$html = "/home/misc/htmlarea";
$temc = "/home/misc/temp2";

$templates_new = "/home/xxx/mainwebsite/new/{$username}/templates";
symlink($templates, $templates_new);


$html_new = "/home/xxx/mainwebsite/new/{$username}/htmlarea";
symlink($html, $html_new);

$temc_new = "/home/xxx/mainwebsite/new/{$username}/temc";
symlink($temc, $temc_new);


}


define('TARGETDIR', '/home/xxx/mainwebsite/new/{$username}/'');
foreach (glob('/home/abc/dep/*') as $dep) {
    if (is_file($dep))    # Exclude directories
        copy($dep, TARGETDIR . basename($dep));
}


mschutte 04-28-2005 09:28 AM

It might be a typo, but you have to enclose the path in TARGETDIR in double quotes "" to expand {$username} -- in single quotes, {$username} is not substituted, but included verbatim.


All times are GMT -5. The time now is 09:58 PM.