My goal :
I want to create Registration Form, when the registration is success so I direct to 'user profile -> index page'.
Directory :
Registration page in : myproject/account/create.php
User profile -> index page in : myproject/profile/nameuser/index.php
Step How it work :
1. Fill registration form
2. Submit form
3. If fail, fill again until success
4. When success, go to folder profile, then create a unique new folder for new user (it mean user folder), then create index page in user folder, then bring each user to access their each index page.
My problem :
I am trying to using method mkdir() in PHP but it can't solve because permission is denied
If any configuration in Linux to solve, I don't know what is the way
My Code :
if (empty($err)) {
if ($insert_stmt = $mysqli->prepare("INSERT INTO member (uname, mail, pwd) VALUES (?, ?, ?)")) {
$insert_stmt->bind_param('sss', $uname, $mail, $pwd);
if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}
$directory = "../profile/$uname";
if(!is_dir($directory)) {
mkdir("$directory");
touch('$directory/index.php');
}
header('Location: $directory . index.php');
}
Hope someone can help me to solve my problem.
before proceeding to create the directory try to change the permissions of the project folder to 755 or 777
chmod -R 755 myproject/
if its still not working try it with permission 777
chmod -R 777 myproject/
and also while creating directory with mkdir you should assign permissions simultaneously
mkdir("$directory", 0755);
Usualy your server run on a user account different from your user account on the machine. Apache can read your file to send it to your client but it doesn'T have write permission over your files.
Options:
Option one, make you folder writable using http://php.net/manual/en/function.chmod.php but this can be a security issue and some host will serve a 500 (internal) error if they find any 777 folder in your user folder. One trick is to use chmod(0777), write the file and re chmod(0755 or 0644) after write. check linux permission if you need a lead on the 'code' https://www.linux.com/learn/understanding-linux-file-permissions. You don'T provide the right code to make it haappend for you but it would be something like
$perm=substr(sprintf('%o', fileperms('../profile/')), -4); //current perm
chmod ("../profile/", "0777"); //make the parent folder writable
mkdir("$directory");
touch('$directory/index.php');
chmod ("../profile/", $perm); // make it back to old perms.
Another option is to add you and apache to a group ad then give write permission to this group but that would be for another stack exchange site.
You could also create a folder and chown (change onwership) to apache, but again it has it's flaw as when you will want to write on it yourself.
NOTE: all solution to your probem are subject to the operating system file permission. and you need to under stand the difference bewteen your user, apache user and even some host are using a user for php.
Related
I have a folder that will be the container for subfolders created by user of my application. I use the following commands to let my application execute the mkdir() function: "sudo chmod -R 775 /var/www/laravel/public/folder_want_give_permission_to", "sudo chmod -R 777 /var/www/laravel/public/folder_want_give_permission_to", "chown -R www-data:www-data folder_want_to_give_permission_to". As I am testing my application, visiting the pages that execute the mkdir function, I have to run the commands from time to time because it appears that Ubuntu "forgets" that I allowed permission in the directory and I get "ErrorException: mkdir(): Permission denied". Things get worse when I remove all subfolders from the directory that I gave permission, the error appears right after I create the first subfolder. The Ubuntu version I am using is 16.04.
To show my mkdir() I think I have to explain the variable values involved.
My system counts the number of subdirectories to manage the user directories to avoid creating directories with the same name. To create a user directory I use the code below.
mkdir( $userdirectory, 0755, true);
$userDirectory would have a value like this, if the user was the first user to register.
"/var/www/laravel/public/folder_want_to_give_permission_to/1"
This would create a subdirectory inside the folder I want to grant permission for them create additional subfolders to store images.
Now to create the subfolders to store images inside the user folder.
mkdir( $userdirectory. $numSubDir, 0755, true);
$folder has a value something like this "/var/www/laravel/public/folder_want_to_give_permission_to/1/" and $numSubdir stores a number like "1" or "2" to store images.
I would end up with something like this:
/var/www/laravel/public/folder_want_to_give_permission_to/1/1
So a user could have directories to store images like this:
/var/www/laravel/public/folder_want_to_give_permission_to/1/1
/var/www/laravel/public/folder_want_to_give_permission_to/1/2
another user
/var/www/laravel/public/folder_want_to_give_permission_to/2/1
/var/www/laravel/public/folder_want_to_give_permission_to/2/2
if what I am trying to do isn't clear:
/var/www/laravel/public/folder_want_to_give_permission_to/user_directory/folder_to_store_images_1
/var/www/laravel/public/folder_want_to_give_permission_to/user_directory/folder_to_store_images_2
a
Please run the below command, hope this helps.
sudo chown -R www-data:www-data /var/www/laravel/
For some reason all folders created dynamically using my PHP upload script get permission 1354 and then it's not possible to save files inside them.
if(!file_exists($options['uploadDir']) && !is_dir($options['uploadDir'])
&& mkdir($options['uploadDir'], 0750, true))
{
$this->data['hasWarnings'] = true;
$this->data['warnings'] = "A new directory was created in " .
realpath($options['uploadDir']);
}
if(!is_writable($options['uploadDir']))
#chmod($options['uploadDir'], 0750);
I don't know what am I doing wrong.
Check parameters of your http server especially the profile of the user running it and the default user profile of the system. There may be a "umask" set somewhere forcing another permission.
You can try to change the umask with a php command umask.
Also you can check the permission of the parent folders and also check the ownership of your folder.
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
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.
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