Creating folder outside /var/www - php

I'm trying to create folder outside of my www folder where are stored my php scripts.
<?php
$subfolder = $_POST['subfolder'];
mkdir("../Norme/".$subfolder, 0755, true);
?>
If I chech inside Norme folder there are no new folders created.
Folder Norme is placed in /var/Norme.
If I change mkdir statement to:
mkdir("Norme/".$subfolder, 0755, true);
The script creates folder Norme and given subfolder inside /var/www
How to set different folder than www folder?

Josef, Just change your code to:
`mkdir("/var/Norme/".$subfolder, 0755, true);
It should work. And make sure that the folder Norme has proper rights/permissions to create the folder inside it.

Related

php mkdir will only work when creating a folder inside a 777 folder

I have a folder called 'folders'. I want to create a folder inside 'folders' called 'folder' with mkdir()
However the folder fails to create when 'folders' is set to 755. The only may I can get mkdir to work is making 'folders' 777. Is this typical or is there something else wrong? Shouldn't be able to do that on 755, isn't 777 a security risk?
A 777 permission on the directory means that everyone has access to read/write/execute (execute on a directory means that you can do a ls of the directory).
Try this one below if you fails to create folder inside the folder.
<?php
// Desired folder structure
$structure = './folders/folder/';
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0755, true)) {
die('Failed to create folders...');
}
?>

How to create directory based on the username

I want to create a directory based on the username(user) that is logged in, to upload images to that folder, so when each user uploads an image file it will be sent to the directory based on the username and the image file is saved in that folder
$_SESSION["user"];
I use session to get the username, now I want to create a folder inside a folder name uploads
mkdir('/uploads/' . $_SESSION['user']);
and make $target_dir= location for upload as above one.
Use of mkdir() and its parameter:
recursive Allows the creation of nested directories specified in the pathname. Defaults to FALSE.It means
Path : path of directory including directory name.
Permission : 0777
Recursive Flag : when you need to create subfolder.
$path = '/upload/UserA';
mkdir($path, 0777, true);
Note
/upload/ is almost certainly wrong. It points to the "upload" directory in the root directory, where you most likely don't have the permission to create one.

PHP mkdir permissions - wrong path

I have an problem and hope you can give me a hint for that:
I have an XAMPP running under OSx.
My APP is in htdocs/app.
Inside /app there is a folder /scripts and there is my PHP File:
app/scripts/file.php
Inside the /app folder there is a second folder /stuff that have 777 permissions.
from the script in app/scripts/file.php I want to create a folder
in app/stuff
But I got permission denied when I try to create a folder like:
mkdir('../scripts/newfolder', 0777, true);
What do I have to do?
Note:
When I test like this:
mkdir('newfolder', 0777, true);
Then it will work, but the new folder is under the wrong location:
app/scripts/newfolder
Give directory path as follows inside file.php.
mkdir('../stuff/newfolder', 0777, true);
If I understand you correctly you want to make a folder in app/stuff from the app/scripts folder, but you are telling the computer to go back into the apps folder (..), then back into the scripts folder (../scripts/) and then to make the newfolder.
mkdir('../stuff/newfolder', 0777, true);
should work then.
Your trying to create a directory from a dynamic place. You should prefer to use DIR which is the absolute directory of the current file being processed.
so you would use
mkdir(__DIR__ . '/../stuff/newfolder', 0777, true);
assume this line from your question is correct
from the script in app/scripts/file.php I want to create a folder in
app/stuff

Creating writable directory with mkdir in PHP

I have this code to create recursive directories:
define('_PATH', '/www/virtual/user/galleries');
$gallery_path = "/".intval($gallery['id']);
if(!is_dir(_PATH.$gallery_path))
mkdir(_PATH.$gallery_path, 0777, false);
I made the parent directory ('galleries') writable (0777).
I can confirm that the directory is created. However, it is NOT created with 0777 permissions, but with 0755! I have no explanation for this. :-(

Upload image from outside of webroot in webroot folder

I have folder structure like this
public_html
/images
outside
/file_uploader.php
that is, public_html is webroot folder.
outside is outside of webroot folder.
So, I want upload file in folder public_html/images.
I have in file outside/file_uploader.php this code:
move_uploaded_file(
$_FILES['img_name']['tmp_name'],
absolute/path/to/public_html/images/folder
);
But this returns error, that Unable to access to public_html/images folder.
Question: how can upload file from outside_of_webroot_file in webroot\folder ?
// see if you have permission to write to this folder
if(!is_writable(absolute/path/to/..)) {
chmod()
}
move_uploaded_file()

Categories