• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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

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
ralphuk100
Asked:
ralphuk100
  • 3
  • 3
1 Solution
 
hernst42Commented:
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
 
ralphuk100Author Commented:
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
 
ralphuk100Author Commented:
I dont want to upload a file, just copy it from one vhost directory to another
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
hernst42Commented:
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
 
ralphuk100Author Commented:
Ok thanks - so how do I write exec to copy a directory structure to another vhost directory?
0
 
hernst42Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now