move_uploaded_file Failed to open stream: Permission denied in XAMPP MacOS - php

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 !!!

Related

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

move_uploaded_file failed to open stream: Permission denied - Mac

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.

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

fopen() Permission Denied despite having correct file directory

$handle = fopen('/Applications/XAMPP/xamppfiles/htdocs/test/file.txt', 'w');
I tried doing the above and every time I try it, the following statement appeared on my browser:
Warning:fopen(/Applications/XAMPP/xamppfiles/htdocs/test/file.txt)
[function.fopen]: failed to open stream: Permission denied in
/Applications/XAMPP/xamppfiles/htdocs/test/index.php on line 26.
I tried looking through answered questions with the same type of questions but most of the things I tried did not work. For example, writing the full directory...
Maybe, you have no premissions to acces the file. One of the answet, is that, you must change CHMOD to e.g. 777. You can co it with your ftp explorer or with PHP.
chmod("/somedir/somefile", 777);
By default when XAMPP is installed, the htdocs folder does not have any read permissions. You can change the permissions through the terminal like this.
cd /Application/XAMPP/xamppfiles/htdocs/test/
sudo chmod 0664 file.txt
Alternatively, you can recursively set all the permission level of all files and folders
cd /Application/XAMPP/xamppfiles/
sudo chmod -R 0664 htdocs/
You could chmod to 777, but that is risky security. What I'm guessing you really want is change ownership of the file. You can do this using chown. PHP usually runs as user www-data, so you'd run a command something like this.
sudo chown www-data:root path/to/file.ext
If you're file permission on the file was something normal like 664, that'd give PHP the 6 permission (Read and Write) instead of the 4 (just Read).

Ubuntu Server won't upload files with PHP 'move_uploaded_file'

I'm trying to upload files using my Ubuntu Virtual Server.
The PHP function moving the uploaded files returns the following error:
"Warning: move_uploaded_file(files/Site Logo.png): failed to open stream: Permission denied in /var/www/test.php on line 5 Warning: move_uploaded_file(): Unable to move '/tmp/phpZB7Mxi' to 'files/Site Logo.png' in /var/www/test.php on line 5"
The directory has permissions 0777 which I set through the SSH and it still doesn't upload. PHP.ini is configured to upload ON and stuff but no luck. I'd guess its permissions problems but I have set it to pretty much RWX for every user?
My code originally was too complex for a video system so I tried a basic code like below:
$file = $_FILES['video']['name'];
echo $file;
move_uploaded_file($_FILES['video']['tmp_name'],"files/".$file);
echo "Done";
This wouldn't work either. So I am guessing it's how the server is configured?
I was getting the same problem, it was permission problem. Just
sudo chmod 777 /var/www/work_pathname/the_upload_pathname/*
A few things to check:
You did check the permissions on files/ using ls -l?
Did you run
chmod 777 /var/www without the -R flag?
Still sounds like a permissions error, something small you might have missed...

Categories