/    Sign up×
Community /Pin to ProfileBookmark

Copying files from an old directory to a new directory

I tried this but it hasnt worked

[code=php]
<?php
function copy_stuff($olddir,$newdir)
{
if(is_dir($olddir))
{
if($old_dir_list=opendir($olddir))
{
while(($file=readdir($old_dir_list))!==false)
{
if(filetype($olddir.$file)==”dir”&&!ereg(“^.+$”,$file))
{
mkdir($newdir.$file);
copy_stuff(“$olddir$file/”,”$newdir$file/”);
}
else if(filetype($olddir.$file)==”file”)
copy($olddir.$file,$newdir.$file);
}
}
}
}
if(copy_stuff(“theold”,”thenew”))
{
echo”Success”;
}
else
{
echo”Failure”;
}
?>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 10.2005 — A couple things:

  • 1. Does the phpuser have permission to write to the directory to which the files are to be copied? (PHP scripts are normally processed by a unique PHP userid, not by your userid.)


  • 2. The function does not ever actually return a value anywhere that I can see. I suspect it will therefore always evaluate as FALSE, regardless of whether it works or not.
  • Copy linkTweet thisAlerts:
    @ConorauthorApr 12.2005 — umm I'm not really understanding what you are saying. Can you maybe explain it differently
    Copy linkTweet thisAlerts:
    @ShrineDesignsApr 12.2005 — try[code=php]<?php
    function copy_stuff($old_dir, $new_dir)
    {
    $old_dir = (substr($old_dir, strlen($old_dir) - 1} != '/') ? $old_dir.'/' : $old_dir;
    $new_dir = (substr($new_dir, strlen($new_dir) - 1) != '/') ? $new_dir.'/' : $new_dir;

    if(!file_exists($new_dir))
    {
    mkdir($new_dir);
    }
    if(is_dir($old_dir))
    {
    if(($dh = opendir($old_dir) !== false)
    {
    while(($file = readdir($dh) !== false)
    {
    if($file != '.' && $file != '..')
    {
    if(is_dir($old_dir . $file))
    {
    copy_stuff($old_dir . $file, $new_dir . $file)
    }
    else
    {
    copy($old_dir . $file, $new_dir . $file);
    }
    }
    }
    }
    }
    }
    ?>[/code]
    Copy linkTweet thisAlerts:
    @Stephen_PhilbinApr 12.2005 — 1. Does the phpuser have permission to write to the directory to which the files are to be copied? (PHP scripts are normally processed by a unique PHP userid, not by your userid.).[/QUOTE]

    I thought that as far as the file system was concerned, PHP was just apache, because PHP is a module of apache it just appears to be an apache process?

    Please educate this poor, feeble minded simpleton.
    Copy linkTweet thisAlerts:
    @NogDogApr 12.2005 — Yes, " When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the "nobody" user)." -- from http://us3.php.net/manual/en/security.apache.php

    Suffice to say, it's some user id other than your personal login id. ?
    Copy linkTweet thisAlerts:
    @Stephen_PhilbinApr 13.2005 — Ah right. Yeah that's how I thought it worked out. Thanks for the clarification. ?

    I have another question though. If I set a directory to be owned by the nobody user, and set permissions so that anyone can read from the directory but only the owner can create/delete/edit the contents of the directory, does that make it so inly the nobody user can add/edit/delete? Or can anyone do that?
    Copy linkTweet thisAlerts:
    @NogDogApr 13.2005 — Ah right. Yeah that's how I thought it worked out. Thanks for the clarification. ?

    I have another question though. If I set a directory to be owned by the nobody user, and set permissions so that anyone can read from the directory but only the owner can create/delete/edit the contents of the directory, does that make it so inly the nobody user can add/edit/delete? Or can anyone do that?[/QUOTE]

    Basically, yes, only the owner (or superuser) could add files to the directory. However, I'm not positive (and do not currently have access to a *NIX box test, and am too lazy at the moment to search online UNIX documentation ? ) about what happens if you chmod a file within that directory to have write permission for the world.
    ×

    Success!

    Help @Conor spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...