move_uploaded_file failed to open stream: Permission denied - Mac - php

While i am trying to move_uploaded_file in php with following code :
if(is_uploaded_file($_FILES['fileupload2']['tmp_name'])){
move_uploaded_file($_FILES['fileupload2']['tmp_name'], "images/".$_FILES['fileupload2']['name']);
}
I've got this error saying:
Warning: move_uploaded_file(images/VIDEO_TS.VOB): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/do_upload.php on line 24
i tried in the terminal and didn't work:
sudo CHMOD 775 /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/do_upload.php
sudo chmod -R 0755 /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/do_upload.php
sudo chown nobody /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/do_upload.php
I am still getting the error and i am using Yosemite, any other solution ?

My solution was to give the permission for the images folder and the php file, by going to the file > Right click > Get info > and then change all the permissions to read&write as the following picture.

I am using ubuntu and once I have encounter this problem, I have solved this problem by changing all the access permissions of the folder in which I want to move the files to 'create and delete files'. See below screenshot:

Set the same permission for your target(uploading) folder

This problem solved by changing permission as described avobe
and making the target folder in the same directory {Root rather than Home } in which application is running
path for image upload
/Applications/XAMPP/xamppfiles/htdocs/emp/files/admin_assets/addsdassets/targrt_Folder
path of folder in which application is running
/Applications/XAMPP/xamppfiles/htdocs/emp/files/admin_assets/addsdassets/inser_db.php

You can try after changing owner for the folder images where you are saving the images. It may work.
chown ownername:group folder;
you have to give absolute address of the folder

I was running Yii2 on IIS (Windows 10) and I tweaked the path and got it working. See code snippet below:
if($model->load(Yii::$app->request->post())) {
$image = UploadedFile::getInstance($model, 'image');
$model->image = $image->getBaseName().'.'.$image->extension;
if($model->save()) {
$image->saveAs('uploads/'.$model->image);//Notice the path change here
return $this->redirect(['view', 'id' => $model->ID]);
}
}
else {
return $this->render('update-image', [
'model' => $model
]);
}
Hope this helps.

This issue is caused by restricted permission. To resolve extend or change your permission to the folder using: chmod -R 777 folder-path.
E.g:
chmod -R 777 /opt/lampp/htdocs/php/aitech/images/passport_photos

apply
sudo chmod 777 images/
to the folder/directory which is your server folder to store images. Please check which is your folder. Totally depends on your choice.

Related

move_uploaded_file Failed to open stream: Permission denied in XAMPP MacOS

I was trying to upload a file using php to my local using XAMPP on MacOS. Whenever I tried to upload a file through script, I got thi error.
move_uploaded_file - Failed to open stream: Permission denied
So I tried some fixes I saw on internet, like chmod -R 777 , changing the user-group and also tried to give read&write permission to every user;
None of them worked.
This is the code I am using for uploading:
if(!empty($filename)){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = $slug.'_'.time().'.'.$ext;
if(!move_uploaded_file($_FILES['eventImage']['tmp_name'], '../assets/images/events/'.$new_filename))
{
$_SESSION['error']='<strong>Error!</strong> Uploading Image failed. Please try again.';
}
}
else{
$new_filename = '';
}
I wanna know how this error can be fixed. I think this is some security issues of MacOS.
like under nix, You can try (under test - not productive server):
chmod -R 755 ./.*
chmod +x ./.
where ./is your document root of HTML files (do not use it as root/admin !!!)
the Argument -R stands for "recursive operation" - so it will try to include all sub directories under ./
and:
chown -R www-data:www-data ./.
at Your document root.
www-data group, and user is need by Apache2.4 - see Settings in Manual.
The group, and user www-data is important under *nix like Operation Systems.
Under Windows, You have to start XAMPP with Administrator Rights.
So be warned !!!
And: You should never be use XAMPP productive !!!

php won't upload images?

I have a php script with no erros (I tested it in other servers), the problem is, in this server I cannot upload images from php.
I add 755 to folder, php.ini file upload is on, max file size is 12mb.
I run this:
chown apache /var/www/html/img-img/
chmod 755 /var/www/html/img-img/
but also didn't solve my problem.
my php also try to create a folder to upload images inside img-img (no success):
$pasta = date("dmy");
$pasta_dir2 = "img-img/$pasta";
if(!file_exists($pasta_dir2)){
mkdir($pasta_dir2, 0755);
}
I have this warnings:
move_uploaded_file(img-img/14719222_886454304825512_8860750091242176512_n.jpg): failed to open stream: Permission denied in /var/www/html/teste.php on line 38, referer: http://example.com/teste2.php
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phptZYSam' to 'img-img/14719222_886454304825512_8860750091242176512_n.jpg' in /var/www/html/teste.php on line 38, referer: http://example.com/teste2.php
any ideas how to solve this without break server security?
this solve my problem:
I don't know what it does, but works... if anyone knows, please, answer this and let me know.
sudo chcon -t httpd_sys_rw_content_t /var/www/html/img-img -R
Check if img-img folder exist and try to set 744 permission to all the folders and subfolders of html using chmod -R 0755 /var/www/html/

How to make directory outside of current directory?

I'm trying to create a secure way to handle file uploads by uploading the files to a directory outside of public_html.
I'm testing my script and my script works when I execute sudo php receive_files.php, but when I run receive_files.php without sudo I get failed to open stream: Permission denied.
Right now all permissions all the way back to var/www/html/mysite/public_html are set to 755. I tried changing all of them to 775 and the command still didn't work
Without sudo. And I'm pretty sure that would not be secure. How do I get around this problem?
My code:
$encoded_file = $_POST['file'];
$user_id = $_POST['user_id'];
$decoded_file = base64_decode($encoded_file);
$new_directory = "../../../../../../user_uploads/$user_id/";
if(!file_exists($new_directory)){
mkdir($new_directory, 0775, true);
file_put_contents($new_directory . $decoded_file);
}
Sudo is required when you are not the owner of something or you try to change permissions. So if you want php script to have access to upper directory. Change the group or user owner of upper directory same as php executor, in most cases its www-data
You can check owner of php script by using
ls -al
Then change owner using
chown -hR www-data:www-data directorypath
For more detail check manual of chown command
This will most probably fix your issue
Additional info
specifying absolute path instead of relative path solved the issue

Opencart fopen(/upload/system/logs/): failed to open stream: Is a directory

I got this error in my opencart shop right after uninstalling an extension. The complete shop does not work anymore. It has not my own template and all php functions do not work. This error shows up:
Warning: fopen(/my_path/system/logs/): failed to open stream: Is a directory in /my_path/system/library/log.php on line 6
This is the Log class:
class Log {
private $handle;
public function __construct($filename) {
//this is line 6
$this->handle = fopen(DIR_LOGS . $filename, 'a');
}
public function write($message) {
fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n");
}
public function __destruct() {
fclose($this->handle);
}
}
Looks like the $filename var is empty. This also reflects the not working php i mentioned above. The logs directory is 755 and i tried it with 777.
And a second error shows at the bottom of my shop:
Fatal error: Call to a member function get() on a non-object in /homepages/6/d421894284/htdocs/opencart/upload/index.php on line 104
Have anyone experienced this error in Opencart 2.0.1.1? Google says, that this must be a common error, but I canĀ“t find a solution.
the answer is simple , change ---->
define('DIR_STORAGE','/storage/');
define('DIR_STORAGE', DIR_SYSTEM . '/storage/');
and do that to config.php , and config.php inside admin folder , change both folders
this problem happens When the system cant create the storage folder which contains the logs cache and modification folders because of permissions problems ,some times the problem happens after storage directory transfer from the dash board , so to solve the problem you need to do the following
create a storage folder in the root of the application
sudo mkdir storage
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 storage
create a storage folder in the root of the application
sudo mkdir logs
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 logs
create a storage folder in the root of the application
sudo mkdir modification
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 modification
The $filename for the log file is either not set in your admin panel or not being passed to the constructor. Check in Admin > System > Settings > Server > Error Log Filename and make sure you have something set there.
If so, next step would be to check the main index.php and make sure the code is intact around line 68 which should be :
$log = new Log($config->get('config_error_filename'));
$registry->set('log', $log);
$filename is empty , Find the filename because it not exist.
<?php
class Log {
private $handle;
public function __construct($filename) {
if (! empty( $filename )) {
$this->handle = fopen(DIR_LOGS . $filename, 'a');
}
}
public function write($message) {
fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n");
}
public function __destruct() {
fclose($this->handle);
}
}
check if $filename is not empty
We were facing the same problem and were able to resolve it by putting correct data in these two files:
config.php
admin/config.php
Please make sure that you have put correct entry for DB_PREFIX:
define('DB_PREFIX', 'oc_');
This is Local Project Root here, but you can change your Server Path easily.
Note: you can change to config.php and config.php inside admin folder both File Compulsory.
define('DIR_STORAGE', 'D:/xampp/htdocs/opencart_project/system/storage/');
OR
define('DIR_STORAGE', 'D:/xampp/htdocs/opencart_project/storage/');
chcon -R -t httpd_sys_rw_content_t /var/www/...
try this. i've solved this error using the command.
Is looking for the files inside your storage folder. Do this step by step
cd /var/www/opencart_storage/
mkdir logs
chmod 777 logs
cd logs
vim error.log
chmod 777 error.log
vim googleshopping.0.log
chmod 777 googleshopping.0.log
vim ocmod.log
chmod 777 ocmod.log
vim openbay.log
chmod 777 openbay.log
reload your opencart site
Presto!
I faced the same problem. Immediately after the installation appeared in the admin:
Warning: fopen (/ my_path / system / logs /): failed to open stream: Is a directory in /my_path/system/library/log.php on line 6
Here is the solution:
Permissions on the folder with logs after installation, you can leave 755! The file is located at /var/www/my_account/data/www/mysite.com/system/logs/openbay.log need to put permission 777. After that, I left all the mistakes.

Codeigniter - move_uploaded_file permission(?) issue

This problem was asked before, but did not solve my case. I am working with Codeigniter.
I have a simple form that submits a file and want to move this file into a folder on my server. After submitting I can print_r the $_FILES['new-item-file'] array and everythings looks good.
But when I use move_uploaded_file I get an error:
failed to open stream: HTTP wrapper does not support writeable connections
The folder I want to write in exists and has 777 permissions.
Dies anyone have a solution to this?
Thanks a lot!
I solved it:
Move_uploaded_file does not seem to accept absolute paths. I took out the base_url(); and it worked.
This error happens due to two reasons.
Permission of the folder and invalid group and user owner.
Wrong path.
Permission and owner of folder :
If you are in Linux. Open terminal type this command. This will change the owner of the folder.
$ cd project_path && sudo chown -R www-data:www-data picture_folder/
Type this command to change the permission of folder.
$ sudo chmod -R 777 picture_folder/
note : Use man command more help about these commands.
$ man chmod OR man chown
Path of folder .
In codeigniter, index.php root file is executed. That is responsible for all activities. So don't use base_url() for root path. Instead. Just give root folder name. like
CI application
->
-application
-system
-user_guide
-index.php
-picture_folder
$target_directory="picture_folder/sub_folder";

Categories