Warning: mkdir(): Permission denied - php

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

Related

mkdir function not working properly

I have folder hierarchy as Bucharest/Waterfall/a.php. My code snippet for making directory on server is as follows:
if(!is_dir($this->folder)){
$old_umask = umask(0);
mkdir($this->folder, 0777);
umask($old_umask);
}
chmod($this->folder, 0777);
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile))
return true;
All files are uploaded to the server. Now the issue is that the parent folder ,i.e. Bucharest has permission 755 while inner folder has permission 777. $this->folder has value as Bucharest/Waterfall. It gives 755 permission to Bucharest while 777 permission to waterfall. According to my code the 777 permission should also be given to Bucharest.
I have also tried chmod but all in vain. I want to give full permission to parent folder.
is_dir($this->folder); tells you that the file isn't a directory, not that it doesn't exist. and of course you can't mkdir if the directory is already there.
Also, if the directory already exists, according to the documentation chmod will not do anything if the user under which php is running isn't the same as the user who owns that directory.
Is the folder you are checking a subfolder already? If so, create the parent folders firts.
i.e.
mkdir('/tmp/test1/test2/test3/test4');
will fail if '/tmp/test1/test2/test3' doesn't exist

mkdir(folder_name) with 755 permission in php

I have my web application hosted in /var/www folder. I am creating a folder from one of the PHP scripts of the web application. The default permission of the created folder is drwx------, i.e. 700. But I want that folder to have at least 755 permission.
Up to now I tried: mkdir($path, 0755) and chmod($path, 0755) PHP functions but without any success.
Does anybody know how to solve my problem please?
Millions of thanks beforehand.
Have you tried changing the umask ?
Have a look here: http://nl3.php.net/manual/en/function.umask.php
The easiest way it to:
$oldmask = umask(0);
chmod($path, 0755);
umask($oldmask)
Since you have default permission of 700, which means the parent directory (the directory in which you are trying to create the folder) do not have rw permission for group owner or other users. Most often the running demon(httpd) is not the owner of the parent folder and hence cannot modify the directory.
In simple terms, the php script do not have access to modify or add new directory. You need to change the permission of the parent folder to at least drwxrw-rw- (or 0755).
Use ssh, cpanel or ftp client to do this. If you do it using php script you will end with the same problem again, as parent of parent will have again 0700. ;)

Magento. Destination folder is not writable or does not exists

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.

Permission denied on mkdir()

I'm getting the following error when trying to call mkdir() on a server...
Warning: mkdir() [function.mkdir]:
Permission denied in
/home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php
on line 373
The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.
How can I find out if the uploads folder is writable?
protected function category_images_base_dir()
{
// Where should the dir be? Get the base WP uploads dir
$wp_upload_dir = wp_upload_dir();
$base_dir = $wp_upload_dir[ 'basedir' ];
// Append our subdir
$dir = $base_dir . '/cat-images';
// Does the dir exist? (If not, then make it)
if ( ! file_exists( $dir ) ) {
mkdir( $dir ); //THIS IS LINE 373
}
// Now return it
return $dir;
}
is_writable() is probably the function you're looking for.
http://cz.php.net/manual/en/function.is-writable.php says:
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Also, the directly next line is relevant here:
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)
Make sure the parent folder is writable to the process that the web server runs as.
Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?

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