Solved

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

Posted on 2004-10-29
188 Views
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.

CODE:

<?php
/**
 * Copy a file, or a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @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/datafilesoftware.com/httpdocs/site_manager/default_site_structure/";
$dest = " /home/httpd/vhosts/hygienebags.com/httpdocs/";

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)) {
        mkdir($dest);
    }
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
      
      echo $entry;
      
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }

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

    // Clean up
    $dir->close();
    return true;
}

copyr("/home/httpd/vhosts/domain1.com/httpdocs/site_manager/default_site_structure", "/home/httpd/vhosts/domain2.com/httpdocs/test_site");

?>
0
Question by:ralphuk100
    6 Comments
     
    LVL 48

    Expert Comment

    by:hernst42
    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
    0
     

    Author Comment

    by:ralphuk100
    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.
    0
     

    Author Comment

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

    Expert Comment

    by:hernst42
    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.
    0
     

    Author Comment

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

    Accepted Solution

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

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

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

    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"');
    0

    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