mkdir() not creating folder on the website - php

my php code is not creating the final folders which are (profile,post and cover) when hosted online but creates when hosted locally.Here is the code please help
<?php
if(isset($_POST['file']) && ($_POST['file']=='Upload'))
{
$path = "fh_users/Male/".$user."/Profile/";
$path2 = "fh_users/Male/".$user."/Post/";
$path3 = "fh_users/Male/".$user."/Cover/";
mkdir($path, 0, true );
mkdir($path2, 0, true);
mkdir($path3, 0, true);
$img_name=$_FILES['file']['name'];
$img_tmp_name=$_FILES['file']['tmp_name'];
$prod_img_path=$img_name;
move_uploaded_file($img_tmp_name,"fh_users/Male/".$user."/Profile/".$prod_img_path);
mysql_query("insert into user_profile_pic(user_id,image) values('$userid','$img_name')");
header("location:Secret_Question1.php");
}
?>

the second parameter of the mkdir() function is the permissions.
using mkdir($path, 0, true ); you create a new folder with 000 permissions (ie no permissions for anybody)
you should use mkdir($path, 0750, true ); instead which will give full permissions to the owner, read/execute permissions for group users and nothing for unknown users
The execute permission on a directory allows the affected user to enter the directory, and access files and directories inside

Related

mkdir() function: No such file or directory

I have the code:
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid."/coverphoto/";
if(!file_exists($path_directory) && !is_dir($path_directory)){
mkdir($path_directory, true);
and it is telling me Warning: mkdir(): No such file or directory
and I do not know what to do.
Help is very much appreciated
You should use mkdir() like this
mkdir($path_directory, 0777, true);
FYI: https://www.php.net/manual/en/function.mkdir.php
And you may use below code to make sure you have enough permission first.
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook";
mkdir($path_directory);
Try this
First check if the user has his own directory
$user_path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid;
if(!file_exists($user_path_directory) && !is_dir($user_path_directory)) {
mkdir($path_directory, true);
}
If the user has his own directory, lets create his coverphoto directory.
$path_directory = $user_path_directory."/coverphoto/"
if(!file_exists($path_directory) && !is_dir($path_directory)) {
mkdir($path_directory, true);
}

fopen creates files owned by root in php

I'm writing the code for converting disqus comments to HashOver system, and I have code like this:
// $threads is list of threads from disqus api that I cached on disk
foreach ($threads as $thread) {
$dir_name = getSafeThreadName(preg_replace("%^https?://[^/]+%", "", $thread['link']));
$dir = 'threads/' . $dir_name;
if (!is_dir($dir)) {
mkdir($dir);
chmod($dir, 0774);
}
// ...
// $thread_posts are $post that belong to $thread
foreach($thread_posts as $post) {
$xml = new SimpleXMLElement('<comment/>');
// ...
// $name_arr is array of numbers for HashOver comments
$fname = $dir . '/' . implode('-', $name_arr) . ".xml";
$f = fopen($fname, 'w');
fwrite($f, $xml->asXML());
fclose($f);
chmod($fname, 0664);
}
}
it created directory for each of my posts in threads that that is read/write with owner apache:apache and inside there are files like 1.xml with owner root:root
why root:root? How can I make it apache:apache?
EDIT:
This is no a duplicate, I don't want to change permission to apache:apache from root:root, which can be done using chown, but I want to make it so it don't change it to root:root in first place, I also want to know why it changed to root:root. This looks like a bug in php or apache for me or some wrong configuration in apache on my side. I don't think is the code since it just open, write and close file.
Don't know why and I would like to know, but this solve the issue:
I've use this:
chmod($dir, 0775);
instead of:
chmod($dir, 0774);
directory was not executable for others and it make files in that directory owned by root when created. Very weird.

Using VPS server when I create directory using mkdir() it returns true but folder when I check using cpanel is blank,

Using VPS server when I create directory using mkdir() it returns true but folder when I check using cpanel is blank, I dont know why I even used scandir() and I noticed that those folders which I created is showing in an array in scandir(), why is that happening, why those folders are not showing ?
This is my code:
/creating directory/
if (!file_exists('public_html/members/1213121')) {
mkdir('public_html/members/1213121', 0777, true);
echo "file getting created";
}
else{
echo "file not getting created.";
}
/**this is the code I put to scan the members folder and it retuns array and showing the folder named 1213121 but in actual cpanel that directory is not there **/
$dir = "public_html/members/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($b);
since I did the testing with other folder names also so it returns in html as below::
file getting created
Array ( [0] => 1213121 [1] => 12131 [2] => 1213 [3] => .. [4] => . )
Also I did the testing with permissions as 0755, 0700 but none is working.
if your server folder permission is ok then this code is work for you.
this first script for deleting the '1213121' folder from your server.
script 1:
delete_files('/public_html/members/1213121/');
/*
* php delete function that deals with directories recursively
*/
function delete_files($target) {
if(is_dir($target)){
$files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach( $files as $file ){
delete_files( $file );
}
rmdir( $target );
} elseif(is_file($target)) {
unlink( $target );
}
}
?>
replace your script with this script 2:
$dir = 'public_html/members/1213121';
if (!file_exists($dir) && !is_dir($dir)) { //check dir is not exist
if (mkdir($dir, 0777, true)) { //check folder is created
echo "Folder created"; //display success message
} else {
echo "folder not created."; //if the dir is not created then show error message
}
}
/**this is the code I put to scan the members folder and it retuns array and showing the folder named 1213121 but in actual cpanel that directory is not there **/
$dir = "public_html/members/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir, 1);
print_r($b);
Note: before replacing 2nd script you must remove e first script.
The problem is solved actually the file which is creating the folder is in the subdomain and when I put the exact path , it was not pointing to it instead it created new public_html folder in subdomain and I was in main public_html.. it created a path like:-public_html/subdomainfolder/public_html/members/1213121 and instead i was thinking that path will be created as public_html/members/1213121 ... So my problem is solved now and Mahfuz answer is also right. thanks for the help.

Permission denied in PHP

My script generates a directory for photos that user uploads but permissions for that generated directory shows as r----x--t even though i specify 777 in php
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 777, true);
}
}
I should mention that i'm on hostinger free account that is running centos OS.
set your umask temporarily to zero, the umask acts as a set of permissions that applications cannot set on files. Source : http://php.net/manual/en/function.umask.php
$old = umask(0);
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 0777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 0777, true);
}
}
umask($old);
if ($old != umask()) {
die('An error occurred while changing back the umask');
}

php created in the root directory

I use account
dev_hq
I want to create a folder in the root called medias.
I use the command
$medias = 'medias';
$path = './root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
but the directory is created in
/var/www/public/root/medias
=> I want to create folders in
/home/dev_hq/
and link
/home/dev_hq/root/medias
What are you are doing is absolutely wrong. Please try this code
$medias = 'medias';
$path = '../../root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
Hope this helps you

Categories