Magento. Destination folder is not writable or does not exists - php

Hey I'm stuck with the following problem, plz help.
I get "Destination folder is not writable.." when trying to add an image to a product, but the permission for all needed folders is 777! I had deleted all files on server, didn`t touch DB, reinstalled Magento from scratch with new DB, and everything is OK.
But when I switched to previous DB (change settings in the local.xml) the bug appeared again.
How can the DB impact the folder permissions?
UPDATE:
Thanx a lot, we found out that Magento jump from this method:
public function getBaseMediaUrl()
{
return Mage::getBaseUrl('media') . 'catalog/product';
}
to the following method:
public function getBaseTmpMediaUrl()
{
return Mage::getBaseUrl('media') . 'tmp/catalog/product';
}
Does anybody know why and how????

There's only one spot in the Magento code base that uses that error language.
File: lib/Varien/File/Uploader.php
...
if( !is_writable($destinationFolder) ) {
throw new Exception('Destination folder is not writable or does not exists.');
}
...
Add some temporary debugging code right above this
...
if( !is_writable($destinationFolder) ) {
Mage::Log($destinationFolder);
//or
var_dump($destinationFolder);
throw new Exception('Destination folder is not writable or does not exists.');
}
...
This will let you know the exact folder Magento wants to write to, but can't. Examine this folder, and you'll find it's not writable. Reviewing the edge cases on is_writable may also shed some light on the subject.

Goto : lib/Varien/File/Uploader.php
Temporary change the following code and try to upload image. Now in error message you can see folder path.
In folder path you have to give file permission 777 and it will work as normal. After the error is resolved revert your code to as it wass.
if( !is_writable($destinationFolder) ) {
var_dump($destinationFolder);
throw new Exception(''.$destinationFolder.'Destination folder is not writable or does not exists.');
}

I got the below error while uploading images in Magento then I did the below steps and that worked for me.
Cd /var/www/
chmod 755 -R /html
chown apache:apache -R /html
setenforce 0
then restart apache ..

Magento 2
This is the file in Magento 2 where the error come from:
vendor/magento/framework/File/Uploader.php
At line 256 you can temporarily place this code to get the unwritable/unexisting folder:
if( !is_writable($destinationFolder) ) {
error_log($destinationFolder);
// or
// throw new Exception('Destination folder is not writable or does not exists.');
throw new Exception($destinationFolder);
}
Otherwise you can check you have these folders and that are writable by the web server:
pub/media/catalog/
pub/media/catalog/category
pub/media/catalog/product
pub/media/images
pub/media/wysiwyg/

It may be the expired certificate from the Plesk administration (it was my case).
I tried the steps above, but it did not work. From there I tried to access the files through FileZilla to give the permissions at once to all folders, hence an error message about the expired certificate. It is not the SSL certificate of the store itself, but the administration of Plesk. I created a new self-signed certificate, applied its Plesk administration and everything went back to normal.
This worked for me. I leave here my contribution.
Good luck

I had the same problem:
Sign in to your SSH:
Give media folder permissions through run this command:
cd public_html/media
find . -type d -exec chmod 777 {} \;

My issue is solved after changing the permissions of the media folder. Just go locate the media folder on your server, right click->change permissions->set value 777 for folder permissions.

Related

Move images into a folder in the public folder in Laravel but fails

I have three folders (userImages, productImages and clientImages) inside the images folder with the public folder in a Laravel(9.5.1) project. I am trying to upload images and move same into the productImages folder but it is not working. However, if I try with the userImages folder, it works and works only with that folder. I would be glad for any assistance from you. Thank you for your assistance in advance.
This is code:
if($request->hasFile('product_image')){
$image = $request->file('product_image');
$newImageName = uniqid().'-'.Str::slug($request->product).'.'.$image->getClientOriginalExtension();
$location = public_path('/images/productImages');
$image->move($location, $newImageName);
}else {
$newImageName = 'default_image.png';
}
The error message I get from the above line of code is:
The stream or file "/Applications/XAMPP/xamppfiles/htdocs/pos/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file
You need to be specific with the directory you want to allow the permission. Like below for your case.
sudo chmod -R 777 /Applications/XAMPP/xamppfiles/htdocs/pos/public/images/productImages
Please make 777 for storage folder permissions. Laravel tries to log the error to the log file. But storage folder permissions don't let it. Then you will see what is wrong with your code in the log file.
sudo chmod -R 777 storage

Failed to create folders Zipper/laravel

I am uploading a zip file which I then need to get extracted at the path with the same file name. So I have used the Chumper/Zipper and to achieve my task I have used the following code i.e.
$zipper->zip('storage/apps/'.$name)->extractTo('storage/apps/'.$name);
It throws an error Failed to create folders at zipper.php at 534, which states, i.e.,
`if (!$this->file->exists($dir) && !$this->file->makeDirectory($dir, 0755, true, true)) {
throw new \RuntimeException('Failed to create folders');`
I figured out that there must a permission issue, so I did
sudo chmod -R 755 /var/www/html/admin-panel/public/storage/apps/
Still got the same error, so I again tried the above command with 777 which is both read/write in the parent directory.
Still, the issue is not resolved.
Help will be appreciated. Thanx
Are your sure, you have created "apps" folder in the storage directory? Laravel generates the folder named "app" by default.

PHP mkdir() won't work on Mac OS X even if folder permissions are 777

I'm facing a strange issue. I cannot get mkdir() to work on Mac OS X in a directory that is under my home, using nginx + php70-fpm webserver. nginx and php-fpm are installed from MacPorts. Coming from the Linux world, I have tried the following:
Edit /opt/local/etc/nginx/nginx.conf and add the user myusername directive and reload nginx, but no success. The server won't even start.
Through the get info menu option, give permission to the folder I want to write to to everyone, but no success.
From the console, chmod 777 the folder, but no success.
Is there something I am missing?
Code:
if(!file_exists( dirname( __FILE__ ) . $ds. $storeFolder . $ds . $_SESSION["lid"] . $ds)) {
// No directory, so create it
if(mkdir(dirname( __FILE__ ) . $ds. $storeFolder . $ds . $_SESSION["lid"] . $ds, 0777, true)) {
die("Folder creation failed");
};
}
Edit:
As an observation, after doing this you may face issues with phpMyAdmin saying incorrect permissions are applied. Therefore, some may find it useful to grand everyone "Read & Write" access only to the htdocs folder, or better yet, the specific folder for the project, not the whole XAMPP folder.
Here is the solution:
Open the applications folder
Locate the XAMPP folder
right-click, and get info (as described above)
In the pop-up window, locate the 'sharing & permission' section
Click the 'locked' padlock symbol
Enter the administrator password
Change 'Everyone' permissions to read & write
In the get info window still, select the 'cog' icon' drop down
option at the very bottom and select 'Apply to enclosed items'.
This will adjust the permission across all sub-folders as well.
Re-lock the padlock symbol
Close the 'Get Info' window.
The task is complete. This will now allow you to make directories.
I had the same problem. I'm not sure if your problem is the same as mine but I changed the permissions on the root folder to 777 and then everything worked fine.

Warning: mkdir(): Permission denied

I am trying to make a directory when a new account is created.
The directory should be in my images folder and is used to better separate uploaded images.
//get the ID of the new account that was just created/inserted
$accountID = mysqli_insert_id($dbc);
//create a new directory path for that account
$directoryPath = "../images/" . $accountID;
// check if the directory exists
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
I had no problem getting this to work a few days ago, however when testing it today I am having problems.
I have included ini_set('display_errors', 'On'); in my page to see what error I am being thrown and it is a permission error.
Warning: mkdir(): Permission denied
The images folder has full read/write permissions for all users and groups as well as any parent folders so I don't understand how that would be an issue, that and it had worked several times before.
I am working on windows if that matters.
Any ideas?
To avoid spending too much time on permissions problems between the CLI user and the Apache user, an easy configuration is to use same user for both processes.
Get your user id and group by doing
$ id
uid=1000(my_user), gid=1000(my_group), ...
And then:
$ sudo service apache2 stop
$ sudo vi /etc/apache2/envvars
export APACHE_RUN_USER=my_user
export APACHE_RUN_GROUP=my_group
$ sudo chown -R my_user /var/lock/apache2
it's better and safer than to change you whole directory permission to 777
I think you should try this -
mkdir("../images/".$accountID, 0777, 'R');
Recursive folder creation may causing the problem.
Also get more information from - mkdir
Also check for folder permission.
You have to be sure that the parent directory allows you to create folder, and not the folder it self that is being created with 0777 rights...
Also, check with which user the Apache server is launched
Check if directory is already exist before using mkdir()
if (!is_dir ($directoryPath) ) {
mkdir($directoryPath, 0777);
}
I am working on windows if that matters.
It does.
Try changing this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
To this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath); //breaking here
}
You are on a Windows box so it will ignore the chmod mode. Also try using the full paths and not relative.
The mode is 0777 by default, which means the widest possible access.
For more information on modes, read the details on the chmod() page.
Note:mode is ignored on Windows.
http://php.net/manual/en/function.mkdir.php

php mkdir() chmod and permissions

i was using this basic script:
$folderPath = "../path/to/$folder/";
mkdir("$folderPath");
i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser
The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos
I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all
All the older folders were being created with: -w- rwx r-x r-x
and I can't get this recreated
I've tried adding a chmod line into my script:
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
but I can't recreate the same permissions, I'm trying to understand how chmod works, but I can't figure out how to get this very basic function working properly again
Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-
In the folder "root_folder/sales/inventory/$folder/", where "$folder" is as mentioned in your code.
If not found, try in the folder "root_folder/sales/inventory/".
If not found, try in the folder "root_folder/sales/".
If not found, try in the folder "root_folder/".
When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder", where the code will be "Options Indexes".
Also in the PHP page, the logic must be like this:-
<?php
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
// Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable.
chmod($targetFile, 0755);
?>
This will help you when you will be unlinking / deleting the uploaded files from the "$folder" folder.
Hope it helps.
If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.
Imagine that $folder = '/store1/ally23/shelf42'; so your final directory structure is
../sales/inventory/store1/ally23/shelf42, and you want 0777 permisions.
You do:
mkdir($folderPath, 0777, true) || chmod($folderPath, 0777);
Only the final directory shelf42 is chmoded to 0777. The intermediary directories are created with default permissions (in my case 0744).
There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.
If you're in a shared environment, you may also want to chown after upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.
Simply umask means the default permissions for new files/directories:
<?php
umask(022);
?>
This sets the default permissions for user, groups, and others respectively:
0 - read, write and execute
1 - read and write
2 - read and execute
3 - read only
4 - write and execute
5 - write only
6 - execute only
7 - no permissions

Categories