Need to set directory permissions when copying them with this function.

Posted on 2004-10-29
Last Modified: 2006-11-17
Hi, I am using the code below (slightly modified already) to replicate my default site structure (with my site manager) when a new site is created. However - the trouble is that when this site is created the permissions on the directories mean I can't even delete them through FTP (I get a permission denied error.) How do I include CHMOD into the code below to set more feasible permissions? Thanks in advance.


 * Copy a file, or a folder and its contents
 * @author      Aidan Lister <>
 * @version     1.0.0
 * @param       string   $source    The source
 * @param       string   $dest      The destination
 * @return      bool     Returns true on success, false on failure

$source = "/home/httpd/vhosts/";
$dest = " /home/httpd/vhosts/";

echo $source;
echo "<p>";
echo $dest;

function copyr($source, $dest)
    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);

    // Make destination directory
    if (!is_dir($dest)) {
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
      echo $entry;
        // Skip pointers
        if ($entry == '.' || $entry == '..') {

        // Deep copy directories
        if (is_dir("$source/$entry") && ($dest !== "$source/$entry")) {
            copyr("$source/$entry", "$dest/$entry");
        } else {
            copy("$source/$entry", "$dest/$entry");

    // Clean up
    return true;

copyr("/home/httpd/vhosts/", "/home/httpd/vhosts/");

Question by:ralphuk100
    LVL 48

    Expert Comment

    As the script is run as the webserver-user you might not be able to create files as another user directly with php (security issue)

    A solution might be to use sudo and exec of php

    See http:Q_21185044.html#12431265

    how such things can be done

    Author Comment

    Hi, this script works already. Its just setting the wrong permissions that is the problem. It does not work cross vhost directories but this is something we are working on. Thanks for the comment.

    Author Comment

    I dont want to upload a file, just copy it from one vhost directory to another
    LVL 48

    Expert Comment

    its the same problem. Your webserver user will not be allowed to change those premissions of the files you wnat to copy. Changing the owner is typical only allowed for root. So you need a script that runs with root-priveliges.

    Author Comment

    Ok thanks - so how do I write exec to copy a directory structure to another vhost directory?
    LVL 48

    Accepted Solution

    The following will describe how you setup sudo correctly to change the ownership:

    As root create a /usr/local/bin/phpcp
    if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4"]; then
        echo "Missing parameter";
        exit 1

    if [ "$1" == "root" ]; then
        echo "Copy to root not allowed"
        exit 1

    cp -r "$3" "$4"
    chown -R $1 "$4"
    chgrp -R $2 "$4"

    then make the script executable via
    chmod +x /usr/local/bin/phpcp

    the add the following line in /etc/sudoers:
    wwwrun  ALL=(ALL) NOPASSWD: /usr/local/bin/phpcp

    in php do
    exec('/usr/local/bin/phpcp $user $group "$src" "$dst"');

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now