MisterBla
MisterBla

Reputation: 2395

Deleting a file through cli in vagrant give permission denied

I'm trying to run a php script from the command line in vagrant, but I'm getting a permission denied error when trying to delete a file.

Using the command sudo chmod 777 messages/ does not work, because I can't change the directory permissions, vagrant's shared folders don't allow that.

I also tried to use chmod and umask to change the directory's and file's permissions to be able to delete it, but to no avail.

$oldUMask = umask(0000);
chmod($dir, 0777);
chmod($file, 0777);

unlink($file);

umask($oldUMask);

How can I fix this? Am I missing something obvious?

The directory structure is like so:

/vagrant
    /app
        /messages

And the owner and group of the directory is www-data, vagrant is run with those user and group settings aswell.

This is the complete script:

<?php

require 'cli-start.php';

use CodeRichard\Config\Config;
use CodeRichard\Text\MessageInfo;
use CodeRichard\Text\TextMessage;

$dir = 'messages/';
$iterator = new DirectoryIterator($dir);
$file = null;

/** @var SplFileInfo $entry */
foreach($iterator as $entry)
{
    $ext = $entry->getExtension();

    if($entry->isDir() || strtolower($ext) != 'json')
    {
        continue;
    }

    $file = $entry->getRealPath();
    break;
}

if($file != null)
{
    $message = json_decode(file_get_contents($file), true)['message'];

    $service = new Services_Twilio(Config::get('twilio.account_sid'), Config::get('twilio.auth_token'));
    $messageInfo = new MessageInfo(Config::get('twilio.from_number'), Config::get('twilio.to_number'), $message);

    $textMessage = new TextMessage($service, $messageInfo);

    $status = $textMessage->send();

    if($status['sent'])
    {
        $oldUMask = umask(0000);
        chmod($dir, 0777);
        chmod($file, 0777);

        unlink($file);

        umask($oldUMask);
    }

    echo $status['message'];
}

Upvotes: 1

Views: 1071

Answers (1)

Alex
Alex

Reputation: 137

The known problem. The simplest solution is to modify in the Vagrantfile the synced folders settings:

config.vm.synced_folder "./htdocs", "/var/www", owner: "vagrant", group: "www-data", mount_options: ["dmode=775,fmode=664"]

Upvotes: 1

Related Questions