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'];
}
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"]
Related
I'm writing the code for converting disqus comments to HashOver system, and I have code like this:
// $threads is list of threads from disqus api that I cached on disk
foreach ($threads as $thread) {
$dir_name = getSafeThreadName(preg_replace("%^https?://[^/]+%", "", $thread['link']));
$dir = 'threads/' . $dir_name;
if (!is_dir($dir)) {
mkdir($dir);
chmod($dir, 0774);
}
// ...
// $thread_posts are $post that belong to $thread
foreach($thread_posts as $post) {
$xml = new SimpleXMLElement('<comment/>');
// ...
// $name_arr is array of numbers for HashOver comments
$fname = $dir . '/' . implode('-', $name_arr) . ".xml";
$f = fopen($fname, 'w');
fwrite($f, $xml->asXML());
fclose($f);
chmod($fname, 0664);
}
}
it created directory for each of my posts in threads that that is read/write with owner apache:apache and inside there are files like 1.xml with owner root:root
why root:root? How can I make it apache:apache?
EDIT:
This is no a duplicate, I don't want to change permission to apache:apache from root:root, which can be done using chown, but I want to make it so it don't change it to root:root in first place, I also want to know why it changed to root:root. This looks like a bug in php or apache for me or some wrong configuration in apache on my side. I don't think is the code since it just open, write and close file.
Don't know why and I would like to know, but this solve the issue:
I've use this:
chmod($dir, 0775);
instead of:
chmod($dir, 0774);
directory was not executable for others and it make files in that directory owned by root when created. Very weird.
My script generates a directory for photos that user uploads but permissions for that generated directory shows as r----x--t even though i specify 777 in php
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 777, true);
}
}
I should mention that i'm on hostinger free account that is running centos OS.
set your umask temporarily to zero, the umask acts as a set of permissions that applications cannot set on files. Source : http://php.net/manual/en/function.umask.php
$old = umask(0);
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 0777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 0777, true);
}
}
umask($old);
if ($old != umask()) {
die('An error occurred while changing back the umask');
}
I try to delete everything in folder (including subfolders) but:
Warning: unlink(./../kaj-content/theme/one-four): Permission denied in
C:\wamp\www\kaj\kaj-admin\includes\incAppearance.php on line 36
this is my code:
$themeDirectory = './../kaj-content/theme';
$dir = $themeDirectory . '/' . $themeName;
array_map('unlink', glob($dir));
how can I change my code ?
Other code didn't work, like:
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..")
rrmdir("$dir/$file");
rmdir($dir);
} else if (file_exists($dir))
unlink($dir);
}
Your php code is running as a particular user, maybe apache.
Your error means that php does not have the correct permissions for directory one-four.
Check the permissions on that directory. Grant write permission for that directory to the user that php is using. Then, your code will be able to delete the file.
I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.
If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.
Is there any way in PHP to temporary change the permission to 777 while uploading video?
PHP provides a function, chmod() for the task.
Attempts to change the mode of the specified file to that given in mode.
You can put it in an if statement, and if it returns false, you can skip the upload file part.
The usage will be like
if( chmod($path, 0777) ) {
// more code
chmod($path, 0755);
}
else
echo "Couldn't do it.";
As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)
There is a way (PHP provides chmod function) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.
You have to initialize the config for the upload, like this:
$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);
You can use chmod() function.
For more information, try here
Warning: You cannot undo the file permissions that are changed by the script below. Proceed with extreme caution.
Important: this code should only be used if you remember to delete it immediately after use. As above, its use may put your site into an insecure state.
//replace dirname(__FILE__) with desired folder.
file_fix_directory(dirname(__FILE__));
function file_fix_directory($dir, $nomask = array('.', '..')) {
if (is_dir($dir)) {
// Try to make each directory world writable.
if (#chmod($dir, 0777)) {
echo "Made writable: " . $dir . "";
}
}
if (is_dir($dir) && $handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file")) {
// Recurse into subdirectories
file_fix_directory("$dir/$file", $nomask);
}
else {
$filename = "$dir/$file";
// Try to make each file world writable.
if (#chmod($filename, 0666)) {
echo "Made writable: " . $filename . "";
}
}
}
}
closedir($handle);
}
}
or you can use terminal for this
chmod -R 755 public_html/test
This is what I've got so far. I need to be able to apply 0666 to all the files in the archive. Can't I do that as I am exporting? What is a sample code for changing the chmod during unarchiving or after unarchiving?
$zip = new ZipArchive;
if ($zip->open('upload/'. $username . $file_ext) === TRUE) {
$zip->extractTo('dir/' . $username);
$zip->close();
} else {
echo 'failed';
}
Thanks for all of the help!
Brandon
Setting 0666 on directories might not be what you want ;-)
File creation in any process in Linux will use with 0777 for directories and 0666 for files but it depends on the umask value what the final permissions will be. By default the umask value is 0022 which creates files like 0644; it works like a subtraction.
So by resetting the umask to 0 you probably get what you need.
umask(0);
$zip = new ZipArchive;
if ($zip->open('upload/'. $username . $file_ext) === TRUE) {
$zip->extractTo('dir/' . $username);
$zip->close();
} else {
echo 'failed';
}