php mkdir folder for every 3 number combination - php

I want do some function mkdir folder for every 3 number combination. for example, 502341 will mkdir a new forder 502/341, 10023049132 will mkdir a new forder 10/023/049/132 I use number_format and explode, my problem is how to check how many unit were explode and wright some thing like
if(!is_dir(dirname(__FILE__) . '/'.$bbb[0])){
mkdir(dirname(__FILE__) . '/'.$bbb[0],0777);
}
$aaa = '502341';//10023049132
$bbb = explode(',',number_format($aaa));
echo $bbb[0];
if(!is_dir(dirname(__FILE__) . '/'.$bbb[0])){
mkdir(dirname(__FILE__) . '/'.$bbb[0],0777);
}
if(!is_dir(dirname(__FILE__) . '/'.$bbb[0]. '/'.$bbb[1])){
mkdir(dirname(__FILE__) . '/'.$bbb[0]. '/'.$bbb[1],0777);
}
...//how to check more $bbb[2], $bbb[3] or even more?

All you need is chunk_split and mkdir with recursive option
$path = __DIR__;
if (! is_writable($path))
trigger_error("$path is not writeable");
$str = "502341";
$arr = chunk_split($str, 3, "/");
mkdir($path . DIRECTORY_SEPARATOR . $arr, 0777, true);
^--------- Recrusive

You're looking for str_split(). Make sure to validate the input beforehand!

Use the recursive mode of mkdir (http://php.net/manual/en/function.mkdir.php) to allow the creation of nested directories

I would use recursive folder creation. For example:
<?php
$aaa = "502341";
$bbb = explode(',',number_format($aaa));
print count($bbb); // prints the depth of your folder tree
mkdir(implode("/",$bbb), 0644, true); // creates recursive folder
?>
where 0644 would be the permissions to the folder. Set this accordingly to your needs.

Related

PHP creating a folder with the right path

<?php
if (isset($_POST['filename']) && isset($_POST['editorpassword']) && isset($_POST['roomname'])) {
$dir = $_POST['filename']; // This must match the "name" of your input
$path = "evo/" . $dir;
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
}
?>
I have this script where I'm trying to create a new folder. The script itself is ran inside of a folder called /evo and by using this code, it creates the folder in there. Where it needs to go is ../../creative however even if I try and use
$path = "./rooms/creative/" . $dir;
or something to that effect it creates it with the base folder as evo so it appears at:
../evo/rooms/creative (creating the folders that don't exist there with it as it should)
I'm just unsure what to write in for the path on where I need it created to find the right location.
Simplest solution is to remove the "evo" in $path = "evo/" . $dir;

rename file is not working on php

Hi i have problem with this code
with the code i can change whole my folder file to number like 1.mp4 2.mp4 ect...
i test the code and i print the name of the file from it and every thing is right
but rename function is not working
this is my code
$dir = opendir('.');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
// if the extension is '.mp4'
if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'mp4')
{
echo $file ;
// do the rename based on the current iteration
$newName = $i . '.mp4';
rename($file, $newName);
// increase for the next loop
$i++;
}
}
// close the directory handle
closedir($dir);
?>
what is the problem now ?
NEW iNFO
i tried the code inside my localhost and it's work but it's not working inside the server
It's more efficient to use glob()
foreach(glob(__DIR__ . '/*.mp4') as $key => $file) {
if(!rename($file, __DIR__ . '/' . ($key + 1) . '.mp4')) {
throw new Exception('Unable to write to '. $file);
}
}
I'll hazard a guess it's a write permissions issue though - I don't see anything directly wrong with the script.
Getting the real path can help:
$file = realpath($filename);
but it sounds like it could be a permissions issue or an SELinux issue also. You will need apache to be the group on the files in question, and the files should be writeable by the group.
Also, if you have SELinux enabled, you will need to use
semanage fcontext -a -t httpd_sys_rw_content_t "/path/to/images/folder(/.*)?"
and
restorecon -R -v /path/to/images/folder

PHP mkdir get dynamic path

I am using mkdir like so
mkdir('somePath\\' . $this->name. '-' . $this->generateRandomString(), 0777, true);
The output can be something like
C:\xampp\htdocs\someFolder\templates\generated\Nick-ycolYWzdin
So, I append a name and random string as the folder name. Problem is, I now need to use PHP to put a file in this folder.
Is there any way to get the path of the folder I just created, including the folder name (with the name and generated string)?
Thanks
store the mkdir parameter in a variable prior to calling the mkdir function.
$path = 'somePath\\' . $this->name. '-' . $this->generateRandomString();
mkdir($path, 0777, true);
/*
Other stuff happens
*/
move_uploaded_file($file, $path);
You should store the path in a variableand pass it to mkdir function
$new_path = ''somePath\\' . $this->name. '-' . $this->generateRandomString()';
if (mkdir($new_path)) {
copy($file, $new_path."/".$file);
}

Is it possible to make a folder based on user input?

Is it possible to make directory based on user input, i want to store their cookies and also create directory for them to access on the site.
User input are the below
$cid = $_POST['cid'];
$gid = $_POST['gid'];
Original of the code :
mkdir('/home/user/public_html/ref/', 0777 );
I want the code to exist like this :
mkdir('/home/user/public_html/ref/',$cid."_".$gid, 0777 );
Instead of , you should concat directory path with .
Just try with:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777);
Try it:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777 );
Like others have said before me use:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777, true);
Notice the "true" 3rd variable.
This will allow mkdir() to create recursively any missing directories.

How to create a folder in site->default->files with write read permissions with Drupal?

I'm working on Ubuntu and I need to create a folder in Drupal's default->files folder with write permission so that I would be able to add files to that folder later on.
Here is the code I have:
drupal_mkdir('public://' . $new_dir . '/');
$file = file_copy($file, 'public://' . $new_dir . '/' . $file_name);
drupal_mkdir('public://' . $new_dir , 0777);
if you need it to be recursive set third argument to true.
update:
$oldumask = umask(0);
drupal_mkdir("public://". $new_dir , 0777);
umask($oldumask);
fire this command and than try your code ..
chmod -R 0700 sites/default/files

Categories