Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:14:03+00:00 2026-05-27T22:14:03+00:00

My script creates a directory. Before I realized that I needed to chown() the

  • 0

My script creates a directory. Before I realized that I needed to chown() the directory from apache to my user name so that I could access the uploaded files, I ran the script a few times. Now I can’t seem to get rid of the directory so I can start clean.

I figured that all I needed to do was write a PHP script to do is since within the script I would ‘be’ apache. I’ve tried rmdir and chown but I keep getting permission denied. I even tried loading the file in browser instead of just php file.php at the command line in case that affects the effective user.

What else can I try?


I already realized invoking the script from the command line would run it with my username as the user instead of the username that the web server runs it as (apache) so I’ve been invoking the script through the browser. I’ve been simply trying to chown it to my user name so that I can delete it from the command line instead of trying to write some recursive rmdir function.

<?php
   $target = "/some/absolute/file/path";
   chown($target, "myself");
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:14:04+00:00Added an answer on May 27, 2026 at 10:14 pm

    This is a common problem on shared hosting, anyways there are several ways to fix that.

    • PHP scripts are executed as “www-user” only when executed through the web server. If you run them from shell as php script.php, they are executed as the user you are logged in with — let’s call it “shell-user“.
    • Usually, an unprivileged user cannot change ownership of a file/directory.
    • Instead, if your www-user and shell-user have a group in common (it depends on your shared hosting provider policies), you can just chgrp all the files/directories (recursively) to that group, change permissions on 440 for files, 770 for directories, and you will be able to manipulate them as shell-user too. Better way would be to use Posix ACLs to automatically set group/permissions, but it’s not common to find that enabled on shared hostings.
    • If you are lucky, and your shared hosting is good enough, you might be able to sudo to www-user, so that you can run commands as that user too. If not, the only way is making it run php scripts through the webserver.

    If you just want to delete directory recursively..

    ..you can use a function like this (From: https://gist.github.com/1407308):

    /**
     * Delete a file or directory recursively.
     * @param string $path
     */
    function rmtree($path) {
        if (is_dir($path)) {
            foreach (scandir($path) as $name) {
                if (in_array($name, array('.', '..'))) {
                    continue;
                }
                $subpath = $path.DIRECTORY_SEPARATOR.$name;
                rmtree($subpath);
            }
            rmdir($path);
        }
        else {
            unlink($path);
        }
    }
    

    Just beware that you cannot change ownership on dirs/files from an unprivileged user, only group and permissions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that creates other PHP files based on user input.
I created a script that list the directories in the current directory <?php $dir
I have created a script that saves some files in the Documents directory of
Is it possible to change current directory from a script? I want to create
I have php script that creates a temporary watermark image for users that are
I have a script which creates a user-defined object like this: obj = new
I have a bash script that creates a Subversion patch file for the current
Source I've never used C before. this script adds a listener onto a directory
I've written a big sql script that creates a CSV file. I want to
I have a PHP script which creates and listens on a port for connections

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.