I have the following code :
if (!file_exists('/public_html/'.'classic/'.'test'.'/')) {
if(!mkdir('/public_html/'.'classic/'.'test'.'/', 0777, true)) {
return false;
}
}
This create only the folder /classic witch have the permission 0755 and another owner. How to change to create recusively 2 folders : /classic/test/ ? Thx in advance and sorry for my english
I found a solution by using umask :
$oldumask = umask(0);
mkdir('mydir', 0777); // or even 01777 so you get the sticky bit set
umask($oldumask);
Related
Am using faker an i would like to generate images and save them but am getting an error
mkdir(): No such file or directory
So i have
$factory->define(App\User::class, function (Faker $faker) {
$filepath = storage_path('images/fakerusr/'); //this fails
if(!File::exists($filepath)){
File::makeDirectory($filepath);
};
return [
'profile_pic' => $faker->image($filepath,400,300)
];
});
BUt when i use
$filepath = public_path('images/fakerusr/'); //this works
But the path saved in the db starts from ./var/www... but i would like the path from images in the public folder.
I have added both read and write permissions to public folder
sudo chmod a+rw -R /var/www.../public
How do i go about this.
Ensure the images and child directory fakerusr already exists. By default the unix command mkdir requires the -p flag to "Create intermediate directories as required" and the PHP function mkdir also requires the 3rd parameter to be true to create nested directories . File::makeDirectory probably works this way too.
$filepath = storage_path('images/fakerusr/'); //this fails
if(!File::exists($filepath)){
File::makeDirectory($filepath, 0755, true, true);
};
you can check using Storage::has
$directory='images/fakerusr';
if (!Storage::has($directory)) {
$resp= Storage::makeDirectory($directory);
dd($resp);
} else{
echo "already exist";
};
Also note that directory will be created inside storage\app\ .And make sure you have 755 permission to these folders
if you want to create directory inside public folder
$directory='images/fakerusr';
if (File::isDirectory($directory)) {
echo "already exist";
} else{
$result = File::makeDirectory($directory, 0775, true);
}
For Check Directory Exists or Not And making the Directory as Public, I am using..
use Illuminate\Http\File;
$path = public_path('upload/imgaes');
if(!File::isDirectory($path)){
File::makeDirectory($path, 0777, true, true);
}
Hope this work
Now in my code one folder is createed like private/119 when I logged into my application. The code is,
if (!is_dir('private/'.$q->row()->userId)) {
$oldmask = umask(0);
$q=mkdir('private/' .$q->row()->userId,0777,true);
umask($oldmask);
copy('public/images/default_user.png','private/'.$q->row()->userId.'/default-profile_pic.png');
}
Now I want to create a one more folder inside that userId(119) folder. How to do that? I have tried something like that $q=mkdir('private/' .$q->row()->userId .'/beforeconvert',0777,true); but it is not working.
Or is the following code is correct?
if (!is_dir('private/'.$q->row()->userId) && !is_dir('private/'.$q->row()->userId .'/beforeconvert')) {
$oldmask = umask(0);
$q=mkdir('private/' .$q->row()->userId,0777,true);
$create_folder = mkdir('private/' .$q->row()->userId .'/beforeconvert',0777,TRUE);
umask($oldmask);
copy('public/images/default_user.png','private/'.$q->row()->userId.'/default-profile_pic.png');
}
According to your code direcoty beforeconvert will not created if parent directory : $q->row()->userId already exist.
Also you don't need to create first parent directory then child. You can directly create child directory with mkdir it will create parent directory also.
Change your code as below:
<?php
if (!is_dir('private/'.$q->row()->userId .'/beforeconvert')) {
$oldmask = umask(0);
$create_folder = mkdir('private/' .$q->row()->userId .'/beforeconvert',0777,TRUE);
umask($oldmask);
copy('public/images/default_user.png','private/'.$q->row()->userId.'/default-profile_pic.png');
}
I have a AWS EC2 server with phpMyAdmin to manage it.
Everything is working correctly but I would like to be able to create another folder in the /var/www/html directory to add files..
This is my code but it just keeps returning the error to me! any ideas??
// STEP 2.2 Create a folder in server to store posts'pictures
$folder = "/var/www/html/bloggerFiles/Posts/" . $id;
if(!file_exists($folder)){
if (!mkdir($folder, 0777, true)) {//0777
die('Failed to create folders...');
}
}
I would normally create that folder in the terminal by using sudo mkdir, but when I add sudo Nothing works!
Any help is appreciated!
Thanks in advance.
Make sure the folder(s) you are accessing are set to read and write folder permissions, then use this function:
function newFolder($path, $perms)
$path = str_replace(' ', '-', $path);
$oldumask = umask(0);
mkdir($path, $perms); // or even 01777 so you get the sticky bit set (0777)
umask($oldumask);
return true;
}
This fixed it for me.
You can create new folder doing this: newFolder('PathToFolder/here', 0777);
EDIT: Please have a look at: https://www.youtube.com/watch?v=7mx2XOFBp8M
EDIT: Also have a look at http://php.net/manual/en/function.mkdir.php#1207
EDIT: Storing functions in classes and safely use the function
class name_here
{
public function newFolder($path, $perms, $deny_if_folder_exists){
$path = 'PATH_TO_POSTS/'.$path; // This is for setting the root to PATH TO POSTS
$path = str_replace('../', '', $path); // Deny the path to go out of var/www/html/PATH_TO_POSTS/$path
if( $deny_if_folder_exists === true ){
if(file_exists($path)){return false;}
$old_umask = umask(0);
mkdir($path, $perms);
umask($old_umask);
}elseif( $deny_if_folder_exists === false ){
$old_umask = umask(0);
mkdir($path, $perms);
umask($old_umask);
}else{
return false; // Unknown
}
}
}
/* Call the function by doing this: */
$manage = new name_here;
$manage->newFolder('test', 777, true); // Test will appear in /var/www/html/PATH_TO_POSTS/$path, but if the folder exists it will return false and not create the folder.
EDIT: If this file is called from html it will re create the path, so I will it has to be called from /html/
EDIT: How to use the name_here class
/*
How to call the function?
$manage = new name_here; Creates a variable to an object (The class)
$manage->newFolder('FolderName', 0777, true); // Will create a folder to the path,
but this fill needs to be called from the html the root directory is set to the
"PATH_TO_POSTS/" basicly means you cannot do this function from "html/somewhere/form.php",
UNLESS the "PATH_TO_POSTS" is in the same directory as form.php
*/
i wanna try this
link : How to check using PHP FTP functionality if folder exists on server or not?
tell me if my code is right;
i cant seem to find the folder and it echoes the failed
sample ftp account is
user: admin#mywebsite.com
pass: name#pasword
ftp file root is : /home/mywebsite/public_html/admin
path folder i want to find is: public_html/admin/userfiles
i set the path ftp account path on the admin from cpanel
if(is_dir('ftp://admin#mywebsite.com:name#pasw0rd#mywebsite.com/userfiles'))
{
echo 'found';
}
else
{
echo 'failed';
}
There are some prerequisites to using 'ftp://` this way:
The PHP directive allow_url_fopen must be set to true;
The target server must support passive ftp
Assuming both of those things, your URL must be accurate. The convention is ftp://name:password#server.example.com/folder. Your URL appears to have a spurious :name# in it.
ftp://admin#mywebsite.com:name#pasw0rd#mywebsite.com/userfiles'
^^^^^^
FTP function checks if a directory exists
<?php
function ftp_is_dir( $dir ) {
global $ftpcon;
// get current directory
$original_directory = ftp_pwd( $ftpcon );
// test if you can change directory to $dir
// suppress errors in case $dir is not a file or not a directory
if ( #ftp_chdir( $ftpcon, $dir ) ) {
// If it is a directory, then change the directory back to the original directory
ftp_chdir( $ftpcon, $original_directory );
return true;
}
else {
return false;
}
}
?>
Is it for me or everyone that the thumbnails generated by CI have 777 permissions ?
how can I set it to have just 644 by default ??
Thanks
in your application/configs/constants.php file you have 4 constants:
define('FILE_READ_MODE', 0644);
define('FILE_WRITE_MODE', 0666);
define('DIR_READ_MODE', 0755);
define('DIR_WRITE_MODE', 0777);
For the Image_Lib library codeigniter stores the image file using DIR_WRITE_MODE for some unknown reason.
You should double check this with the CI Guys as i believe they should be using FILE_WRITE_MODE.
you can open the library file and manually modify the sections such as
if ($this->dynamic_output === FALSE)
{
if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
{
if ($this->source_image != $this->new_image)
{
if (#copy($this->full_src_path, $this->full_dst_path))
{
#chmod($this->full_dst_path, DIR_WRITE_MODE);
}
}
return TRUE;
}
}
and change the line #chmod($this->full_dst_path, DIR_WRITE_MODE); to FILE_WRITE_MODE so that it writes correctly.
there is a few sections of the file like this so you may have to search for others