Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-29
6
Medium Priority
?
194 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
Comment
Question by:ralphuk100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12442432
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
ID: 12442593
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
ID: 12442628
I dont want to upload a file, just copy it from one vhost directory to another
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:hernst42
ID: 12442774
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
ID: 12442800
Ok thanks - so how do I write exec to copy a directory structure to another vhost directory?
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 12446871
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

597 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