$image_path ="images/";
$dir_perms = 0777; //0755;
$file_perms = 0644;
//sets PHP's umask to mask & 0777 and returns the old umask
$old_umask= umask(0);
echo " old umask == $old_umask";
if (mkdir($image_path, 0777, TRUE) )
echo "created $image_path";
else
{
echo " $image_path failed creation";
exit();
}
/*
//if (chmod($image_path, 0777) )
if( chmod( $image_path, $dir_perms))
echo " $image_path folder set as $dir_perms";
else
echo "$image_path failed to chmod to $dir_perms";
*/
//chmod( $create_path,0777);
$owner = "root";
// Set the user
if ( chown($image_path, $owner) )
{
echo "user changed to root ";
}
else
{
echo "\n -------- NOT changed to root! ";
exit();
}
My chown fails. According to some people this is a bad approach because it would let my script change ownership of a file in the "client"'s system.
My main concern is that I want my php script to access the directory it created!
I am probably making some silly mistake. So, when I try to access www-data permission folder, it fails.
If I try to show a image from a directory with root permission, a code I have work.
Related
When a user creates a profile and uploads a profile picture, the PHP "move_uploaded_file" function is used to move a temporary copy of this image to the project directory.
While my current code works perfectly fine on my Windows 10 machine, it doesn't work on my Mac because of incorrect file permissions.
I have already tried the method provided in other answers and nothing changes after running the terminal command:
sudo chmod 644 /opt/lampp/htdocs/yourfolder
This is true for any variation including:
sudo chmod -R 777 /opt/lampp/htdocs/yourfolder
Note that I DO have admin permissions on the laptop
Other users have stated that these commands have fixed their problems, so I suspect that mine is different.
PHP Code Snippet:
$username = $_POST["username"];
$password = $_POST["password"];
$ext = null;
/* Debugging information
echo $_FILES["profile_pic"]["name"]."<br>";
echo $_FILES["profile_pic"]["tmp_name"]."<br>";
echo $_FILES["profile_pic"]["size"]."<br>";
echo $_FILES["profile_pic"]["error"]."<br>";
*/
if (strrpos($_FILES["profile_pic"]["name"], ".jpg") != null || strrpos($_FILES["profile_pic"]["name"], ".jpeg") != null) {
$ext = ".jpg";
};
if (strrpos($_FILES["profile_pic"]["name"], ".png") != null) {
$ext = ".png";
};
$path = __DIR__ . "\data\users\images\\" . uniqid() . $ext;
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $path)) {
echo("File successfully uploaded");
} else {
echo("Upload failed");
};
Returned: Upload failed
Error thrown:
move_uploaded_file(/opt/lampp/htdocs/Test-Site\data\users\images\5c1dadd52bb71.jpg): failed to open stream: Permission denied in /opt/lampp/htdocs/Test-Site/signup_confirm.php on line 27
Expected: user profile picture is inserted into the .../htdocs/Test-Site/data/users/images folder
Actual: above error about incorrect file permissions is thrown
The reason for the error is that I was using incorrect directory separators ("\" instead of "/" for Mac). Changing these made the profile picture move to the intended location.
I want to create a new directory(folder) inside the webroot so i can create/edit files inside that new folder.
My code is:
$path = 'files' . DS . 'pathtofolder';// $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) { //this is to verify if created
echo "Exists";
}
else{
echo "Doesnt exist";
}
The result is always doesnt exist. And i cant find any created folder inside my cakephp folders.
I tried to change 'files' for 'webroot' but it doesnt work.
Is this the correct code to create a directory?
try this
$path = 'files' . DS . 'pathtofolder';
$folder = new Folder($path, true, 0755);
if ($folder->path) {
echo "Exists";
}
else{
echo "Doesnt exist";
}
There is a direct PHP call to create directories. So, you can use that. I dont know the cakephp version.
if (!mkdir($structure, 0777, true)) {
die('Failed to create folders...');
}
Also, check if your web-server user www-data has permissions to create directories. Otherwise, on command prompt run this command
addgroup www-data
That will add www-data to enable permissions. You might need to add sudo in some cases to above command if you get permission denied error.
$path = WWW_ROOT . 'webroot' . DS . 'FolderNameYouWantToSet' . DS;
if (!file_exists($path)) {
$oldMask = umask(0);
mkdir($path, 0777, true);
chmod($path, 0777);
umask($oldMask);
}
This will create a folder with 777 permission, you can set the folder permission as per your requirement.
WWW_ROOT is the global cake variable which stores the path up to the project.
Please am using this to create a new directory, but is invalid path when i want to create folder in the same directory and is showing Invalid path specified. But if i try to create in a different folder it will work.
<?php
if(isset($_POST['makejail'])){
$jailorgname = $_POST['jailName'];
$parentDir = '/';//cjl I want to create in the same root folder
if(!is_dir($parentDir)) {
echo('Invalid path specified');
}
else if(!is_writable($parentDir)) {
echo('Unable to create directory, permissions denied.');
}
else if(file_exists($parentDir."/".$jailorgname)){
echo('Folder already exist');
}
else if(mkdir($parentDir."/".$jailorgname) === false) {
echo('Problems creating directory.');
}
else{
echo "Folder was created";
}
}?>
You're defining $parentDir wrong:
$parentDir = '/';
This points to the root directory of the machine, not the parent directory of your script.
You may want to use the magic constant __DIR__ like this:
$parentDir = __DIR__ . "/.."; // Go up one level from the current directory
See here for more info:
http://php.net/manual/en/language.constants.predefined.php
The PHP version is 5.5.12 ( under WAMP 2.5 )
I want to create a directory recursively , it is on my development computer Windows7 at the moment but the production system is Linux :
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
At first run of the script the directory is created , but when I rerun the script then the code execution goes to the else block !
So how to make the mkdir always succeed ?
Do this:
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if(is_dir($dir)){
echo 'directory already exists';
}
else if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
I can't make a new directory in my web server. I think my code is ok to create a directory. Can you tell me what is the error ?
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
mkdir($path);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
It's always showing me "directory is not created".
This is not just a permission issue, you are doing impossible things.
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
You can never use a url as the path to create a directory in a server.
The path should be the path in your web server like:
$path = "/path/to/your/project/astuces/uploads/products/".$id;
And then make sure the apache user has the permission.
If the the parent directory also not exist at first, you have to set the third parameter to true of mkdir:
if(mkdir($path, 0755, true)){
Try this code
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}