I run my webserver on windows (XAMPP Control Panel v3.2.2). I am creating files and folders like this:
use File;
...
$Path = app_path()
. '/Projects/'
. strtolower($Project->name)
. '/Schema/';
if (File::exists($Path)) {
$result = 1;
} else {
$result = File::makeDirectory($unitPath , '775', true);
$content = "the content is basically a whole controller";
$file = $Path . strtolower($Unit->name) . '.php';
$bytes_written = File::put($file, $content);
if ($bytes_written === false) {
return false;
}
}
The file which I create is a PHP controller.
The files and folders are created. However by using git bash I noticed that the permission is not set to 775 even though I have specified it in makeDirectory, same for the files.
If I try to execute an action from the created file (which is a controller), then I get:
ErrorException in
C:\xampp\htdocs\selenium\vendor\composer\ClassLoader.php line 440:
include(C:\xampp\htdocs\selenium): failed to open stream: Permission
denied
How can I solve this, other than running my webserver on linux instead of windows?
NOTE: Everything works fine on my computer at home, where I also run the webserver on windows. The only difference is that I am using Windows 10 64bit at home and Windows 7 64bit here at work.
UPDATE:
The ErrorException include(C:\xampp\htdocs\selenium): failed to open stream: Permission denied dissapeared after executing composer update. But the question how I can create a file/folders with specific permissions persists.
UPDATE 2:
After creating a new file the ErrorException appears again...
Related
Environment:
Laravel Version: 5.8.29
PHP Version $ php --version: PHP 7.2.24 (cli)
Problem Statement:
I'm unable to download file from s3 to local disk using put method in created directory.
ErrorException (E_WARNING) : file_put_contents(/path/storage/4804_1626): failed to open stream: Is a directory
However, I've found that directory has been created with below permission. I tried setting permission 777 as passing third parameter in makeDirectory() method but it didn't work.
ubuntu#ip:~/path/storage$ ll
drwxr-xr-x 2 www-data www-data 4096 Sep 20 07:07 4804_1626/
Files & Configuration:
$folderPath = Storage::disk('local')->makeDirectory($folderName, 0777);
$contents = Storage::disk('s3')->get('path/' . $fileName);
Storage::disk('local')->put($folderName, $contents); // Following line is throwing error
You need to specify full path to file, not only folder:
Storage::disk('local')->put($folderName . '/' . $fileName, $contents);
I have inherited a PHP application and need to investigate various issues. I am not a PHP programmer so bear with me.
I wrote a function to log messages to a custom log file.
<?php
function my_logger($log_msg)
{
error_log("USER INFO:::::",0);
error_log(get_current_user());
error_log(exec('whoami'));
file_put_contents('/var/www/html/myproject/ulogs/my-log.log', date('G:i:s') . ">>$ " . $log_msg . "\n", FILE_APPEND);
}
After a lot of 500 errors and no information I discovered that the following line is being written to the error_log.log
PHP Warning:
file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to
open stream: Permission denied in
/var/www/html/myproject/app/lib/log_fns.php on line 11
I have put in a call to get_current_user and whoami which shows me:
[07-Apr-2021 14:26:04 UTC] USER INFO::::
[07-Apr-2021 14:26:04 UTC] root
[07-Apr-2021 14:26:04 UTC] apache
I am calling this at the moment from a simple php page called at http:///test_log.php:
<?php
require("/var/www/html/myproject/app/lib/log_fns.php");
//phpinfo();
my_logger("This is a test message from test_log.php");
?>
I tried to chmod the files to 777 in the ./lib directory but was still getting this error. I have hard coded the path you can see above creating the ulogs directory.
I have tried combinations of chmod the ulogs directory to 777 and chown the directory to apache:apache but still get this error.
Any ideas where I can look next or how to solve?
TIA
Run this command
sudo chmod 777 /var/www/html/myproject/ulogs/
With this work you file_put_contents can generate or edit the file in this directory
This happened to me and no changing of file/folder permissions worked. The issue in the end was SELinux, which is setup as enforcing by default on CentOS.
To disable it, edit /etc/selinux/config, and change
SELINUX=enforcing
to
SELINUX=disabled
Reboot and it should work.
I use on my application some fonctions about files. creation of files, copy/create from a directory to another, update. On my localhost, all is okay, that's work.
The main purpose is to have a manager for translation on the website.
rights of parent directories : 755
I have error :
1) if files doesn't exist the error is
copy(/blabla/path.php): failed to open stream: Permission denied
2) if I create files before (touch + chown on usr:usr) with basic rights (644) or with updated rights (775) :
file_put_contents(/blabla/path.php) : failed to open stream: Permission denied
On my localhost :
right of directories : 755
right of files : 644
I also tried to clean php cache (php artisan cache:clear)
edit :
I use 2 functions :
copy (who copy default translate file in the directory of second langage) and file_put_contents (who edit file)
public function replaceSlugs(Array $slugs,$filename, $lang)
{
$file=$this->path2File($filename,$lang);
if(!file_exists($file)){$this->duplicateFile($lang,$filename);}
$translate="<?php return ".var_export($slugs,true).";";
return file_put_contents ( $file , $translate, LOCK_EX );
}
private function duplicateFile($lang,$filename)
{
copy($this->path2File($filename),$this->path2File($filename,$lang));
}
private function path2File($filename,$lang="en")
{
return resource_path('lang/'.$lang.'/'.$filename.'.php');
}
Solution is Risky but it will do the trick
Give your folders full permission & clear Cache
Something like this.
sudo chmod -R 777 public/lang/en
if you don't give 777 permission you will also stuck while unit Test.
I'm working on implementing Safari Push Notification service for my website. I've been following the tutorial made by Apple which can be found here. And we have to create a package to serve push notifications and Apple provides a companion php file that helps to create the package.
Now I've downloaded the php file and tried running it, but it always throws a warning saying "Failed to open stream: No such file or directory".
Full Log:
D:\...\Downloads\php-7.0.4-nts-Win32-VC14-x86>php D:\..\Downloads\Safari-Push-Notifications-master\createPushPackage.php
Warning: copy(pushPackage.raw/icon.iconset/icon_16x16.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_16x16#2x.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_32x32.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_32x32#2x.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_128x128.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_128x128#2x.png): failed to openstream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line30
Warning: copy(pushPackage.raw/website.json): failed to open stream: No such fileor directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: file_get_contents(D:/../Desktop/dntest1457346433/icon.iconset/icon_128x128#2x.png): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39
.
.
.
Warning: file_get_contents(D:/../Desktop/dntest1457346433/icon.iconset/icon_16x16.png): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39
Warning: file_get_contents(D:/../Desktop/dntest1457346433/website.json): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39
Warning: file_get_contents(Desktop): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 47
PS: I do have a directory named pushPackage.raw that contains a folder called icon.iconset which has a set of 6 png images. So I have no idea why it says "No such file is present".
PHP Companion file
<?php
// This script creates a valid push package.
// This script assumes that the website.json file and iconset already exist.
// This script creates a manifest and signature, zips the folder, and returns the push package.
// Use this script as an example to generate a push package dynamically.
$certificate_path = "Desktop"; // Change this to the path where your certificate is located
$certificate_password = "pwd1"; // Change this to the certificate's import password
// Convenience function that returns an array of raw files needed to construct the package.
function raw_files() {
return array(
'icon.iconset/icon_16x16.png',
'icon.iconset/icon_16x16#2x.png',
'icon.iconset/icon_32x32.png',
'icon.iconset/icon_32x32#2x.png',
'icon.iconset/icon_128x128.png',
'icon.iconset/icon_128x128#2x.png',
'website.json'
);
}
// Copies the raw push package files to $package_dir.
function copy_raw_push_package_files($package_dir) {
mkdir($package_dir . '/icon.iconset');
foreach (raw_files() as $raw_file) {
copy("pushPackage.raw/$raw_file", "$package_dir/$raw_file");
}
}
// Creates the manifest by calculating the SHA1 hashes for all of the raw files in the package.
function create_manifest($package_dir) {
// Obtain SHA1 hashes of all the files in the push package
$manifest_data = array();
foreach (raw_files() as $raw_file) {
$manifest_data[$raw_file] = sha1(file_get_contents("$package_dir/$raw_file"));
}
file_put_contents("$package_dir/manifest.json", json_encode((object)$manifest_data));
}
// Creates a signature of the manifest using the push notification certificate.
function create_signature($package_dir, $cert_path, $cert_password) {
// Load the push notification certificate
$pkcs12 = file_get_contents($cert_path);
$certs = array();
if(!openssl_pkcs12_read($pkcs12, $certs, $cert_password)) {
return;
}
$signature_path = "$package_dir/signature";
// Sign the manifest.json file with the private key from the certificate
$cert_data = openssl_x509_read($certs['cert']);
$private_key = openssl_pkey_get_private($certs['pkey'], $cert_password);
openssl_pkcs7_sign("$package_dir/manifest.json", $signature_path, $cert_data, $private_key, array(), PKCS7_BINARY | PKCS7_DETACHED);
// Convert the signature from PEM to DER
$signature_pem = file_get_contents($signature_path);
$matches = array();
if (!preg_match('~Content-Disposition:[^\n]+\s*?([A-Za-z0-9+=/\r\n]+)\s*?-----~', $signature_pem, $matches)) {
return;
}
$signature_der = base64_decode($matches[1]);
file_put_contents($signature_path, $signature_der);
}
// Zips the directory structure into a push package, and returns the path to the archive.
function package_raw_data($package_dir) {
$zip_path = "$package_dir.zip";
// Package files as a zip file
$zip = new ZipArchive();
if (!$zip->open("$package_dir.zip", ZIPARCHIVE::CREATE)) {
error_log('Could not create ' . $zip_path);
return;
}
$raw_files = raw_files();
$raw_files[] = 'manifest.json';
$raw_files[] = 'signature';
foreach ($raw_files as $raw_file) {
$zip->addFile("$package_dir/$raw_file", $raw_file);
}
$zip->close();
return $zip_path;
}
// Creates the push package, and returns the path to the archive.
function create_push_package() {
global $certificate_path, $certificate_password;
// Create a temporary directory in which to assemble the push package
$package_dir = 'D:/Users/Mike/Desktop/dntest' . time();
if (!mkdir($package_dir)) {
unlink($package_dir);
die;
}
copy_raw_push_package_files($package_dir);
create_manifest($package_dir);
create_signature($package_dir, $certificate_path, $certificate_password);
$package_path = package_raw_data($package_dir);
return $package_path;
}
// MAIN
$package_path = create_push_package();
if (empty($package_path)) {
http_response_code(500);
die;
}
header("Content-type: application/zip");
echo file_get_contents($package_path);
die;
Another thing, this php script does create a package at the specified location (D:/Users/Mike/Desktop), but without the contents present in the pushPackage.raw directory.
Please help me resolve this issue. Thank you so much!
You need to run createPushPackage.php from the folder with Safari-Push-Notifications package (in your case D:\..\Downloads\Safari-Push-Notifications-master\) because code copy("pushPackage.raw/$raw_file", "$package_dir/$raw_file"); use relative path to assets files.
Add some explanation for relative paths:
bash$ pwd
/var/www/test # we inside /var/www/
bash$ ls -la # there are 2 files
-rw-r--r-- 1 alexanderpolomodov wheel 48 Mar 7 15:07 test.php
-rw-r--r-- 1 alexanderpolomodov wheel 15 Mar 7 15:08 test.txt
# test.php consist from one string
# var_dump(file_get_contents('test.txt'));
bash$ php test.php # run this script
string(15) "test filepaths"
bash$ cd ..
# when we run this command from another folder we get error
bash$ php test/test.php
Warning: file_get_contents(test.txt): failed to open stream: No such file or directory in /private/var/www/test/test.php on line 3
bool(false)
I've recently migrated linux servers.
I'm currently getting this error when trying to upload an image using Imagick from a form.
Warning: fopen(/home/user/public/website.com/public/images/users/current/thumb/username.jpg): failed to open stream: Permission denied in /home/user/public/website.com/public/inc/functions/functions.php on line 864
The code that is run resides in a function:
try{
$im = new Imagick($image['tmp_name']);
if($im_w != 'O' && $im_h != 'O'){
$im->cropThumbnailImage($im_w,$im_h);
}
$fp = fopen($path, 'w'); //the line that is causing the error to occur. line 864.
$im->writeImageFile($fp);
return 1;
}
catch(Exception $e)
{
echo $e->getMessage();
}
I've had a look through various PHP/Linux fixes for permissions, The /images/ directory and subdirectories currently have permission: 755
Can anyone help me with a solution. Comment if you need to know anything else.
Thanks in advance.
Simply entered the command into my linux command line:
sudo chmod -R 777 /home/username/public/website.com/public/x_images/