file_put_contents(path): failed to open stream: Is a directory - php

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);

Related

Why does switching to secure folder (httpsdocs) break the PHP file upload function

Server version: Apache/2.4.34 (Unix)
NOTE: This is not a Plesk or control panel questions. This is a custom PHP application hosted on a Rackspace dedicated server with no control panel.
I'm trying to utilize an existing SSL certificate for several sub-directories of a domain, but the architecture for the domain separates out two directories for non-secure/secure:
httpdocs
httpsdocs
I copied everything from httpdocs to httpsdocs and also forced traffic to https using .htaccess. Everything worked great until I tried the PHP file upload functionality.
Here's the bit of code that is failing from the secure side:
$path = "uploaded_files/".$row_query[0].'/'.$file_name;
copy($temp_file, $path);
In doing some debugging, I found that the temp file gets created:
/tmp/phpanKT4N
but the error I see in the logs is:
copy(uploaded_files/New Sub Folder/30052_testing.txt): failed to open stream: Permission denied in /var/www/vhosts/.com/httpsdocs/demo/resource/add_resource.php on line 108
I tried changing copy() to move_uploaded_file() but here's the error for that:
PHP Warning: move_uploaded_file(uploaded_files/test/30054_testing.txt): failed to open stream: No such file or directory in /var/www/vhosts/<thedomain>.com/httpsdocs/demo/resource/add_resource.php on line 111
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpOC9YEF' to 'uploaded_files/test/30054_testing.txt' in /var/www/vhosts/<thedomain>.com/httpsdocs/demo/resource/add_resource.php on line 111
I searched stack and saw that ownership must match, but the permissions appear to be the same for httpdocs and httpsdocs.
The /tmp folder is owned by root:root and like I said this works perfectly from the non-secure folder. The non-secure and secure upload folders are identical:
httpdocs:
drwxrwxr-x+ 8 theuser apache 4096 Sep 22 2015 uploaded_files
httpsdocs:
drwxrwxr-x+ 8 theuser apache 4096 Sep 22 2015 uploaded_files
Is this a configuration, permissions issue, or what?
Figured this out... Ran the following:
First find apache's user:
egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
Result:
User apache
Group apache
Then ran this and it solved all the PHP file upload issues in httpsdocs:
chown -R apache:apache /var/www/vhosts/<thedomain>.com/httpsdocs/
chmod -R g+rw /var/www/vhosts/<thedomain>.com/httpsdocs/

file_get_contents failed to open stream: No such file or directory

I'm getting a rather weird error
file_get_contents('/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt'): failed to open stream: No such file or directory
So I changed the code to:
if (file_exists($cookie_file)) {
echo file_get_contents($cookie_file);
} else {
echo "$cookie_file does not exist\n";
}
And it outputs:
/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt does not exist
In the command line if I do
cat /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
It cats the file just fine
Permissions are fine too:
ls -lrt /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
-rwxrwxrwx. 1 apache apache 391 Feb 11 16:27 /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
So yeah, I'm rather confused to why it won't read the file that clearly exists.
Any ideas?
Use realpath to expand relative path to absolute path. .. between config and runtime in path means its relative, while file_get_contents require absolute.

Symfony 2.4 project deployment without SSH

I have made a small project with symfony 2.4 at my local system. I have uploaded all the project files to my shared hosting server with same directory structure and database. Only the name of my public directory is "public_html" instead of "web". I don't have SSH access to this server. When i run the website by typing the domain name then it shows blank page. I see below given error in my server's log file:
adminhelpline.com [Sun Apr 20 17:08:55 2014] [error] [client 5.10.83.33:35937] AH01215: PHP Warning: require_once(/home/sites/adminhelpline.com/vendor/composer/autoload_real.php): failed to open stream: No such file or directory in /home/sites/adminhelpline.com/vendor/autoload.php on line 5
My Autoload file looks like this:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit48e5d1e7fa77bf3d631b331b6343761b::getLoader();
Can anyone help me to sort out the problem?
it's quite simply. what the error log tells you that the file autoload_real.php in the folder /home/sites/adminhelpline.com/vendor/composer doesn't exist.
check the structure of your folders I assume you use some sort of ftp client? the worst case try to change the autoload.php not to include that file and see what you get or just move the file to the same folder as the autoload.php and change line 5 folder structure.
Execute composer update in your local machine, and then deploy the generated vendor/ folder.
This folder will contain the dependencies and the generated autoload

PHP Warning: failed to open stream: no such file or directory

I am working on local server, but I am having problem including files, even with full path given. eg, I have a file /home/[user]/public_html/vt/test.php like this:
<?php
include_once('/home/<user>/public_html/vt/Menu.php');
print "included_once called.\n";
?>
I got error in error_log:
failed to open stream: No such file or directory
/home/[user]/public_html/vt/Menu.php exists, and access right is:
-rwxr-xr-x. 1 <user> apache 3906 Jul 5 08:43 <full/path/of/the/file>
The local documentRoot folder is set to (recursively):
drwxr-xr-x. 4 <user> apache 4096 Jul 26 14:06 public_html
So what is wrong?
try something like that: (DIR is a magic constant which contains the directory of the current file)
include_once dirname(__FILE__).'/Menu.php';
// PHP >= 5.3
include_once __DIR__.'/Menu.php';

crontab cannot find required php file when running a php script

I am trying to get crontab to run a php file, here is the cronjob
10 * * * * /usr/bin/php /var/www/update/ranks.php >> /var/www/update/log/ranks.txt
But I keep getting an error saying the required file does not exist
PHP Warning: require_once(../mws_products.php): failed to open stream: No such file or directory in /var/www/update/ranks.php on line 2
PHP Fatal error: require_once(): Failed opening required '../mws_products.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/update/ranks.php on line 2
I do not get this problem when I run the file from a browser or when I go into the directory of the file and execute the file e.g. cd /var/www/update/
ranks.php
<?php
require_once('../mws_products.php');
echo "-------------------------------------------------------------\n";
echo date('d-M-Y H:i:s',time())."\n";
echo "Update Ranks\n";
$products->updateRanks();
$database->addUpdate("ranks", time());
echo "\n\n\n";
?>
folder structure
[folder] update
|____ [file] ranks.php
[file] mws_products.php
What could be causing this problem? (note: I have tried restarting apache and the server)
As your are running in a crontab from the root directory ../mws_pruducts.php does not exists, relative to the given root.
There are multiple solutions, this is one of them:
define( 'ROOT', dirname(__FILE__) );
require_once(ROOT . '/../mws_products.php');
Try with the absolute path for the file...
require_once('/var/www/mws_products.php');

Categories