PHP mkdir get dynamic path - php

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);
}

Related

How can a create a folder and save a image on my server in php?

Below I have left my code. It currently works in my development environment (localhost), but when I push the changes to my live server it seems like my php doesn't create the folder/file.
public static function saveImage($image, $name, $path = '')
{
$img_data = explode(',', $image);
$mime = explode(';', $img_data[0]);
$data = $img_data[1];
$extension = explode('/', $mime[0])[1];
if(!file_exists('../public/media/img/' . $path)){
mkdir('../public/media/img/' . $path, 0755);
echo('Test1');
}
echo('test2');
file_put_contents('../public/media/img/' . $path . $name . '.' . $extension, base64_decode($data));
return 'media/img/' . $path . $name . '.' . $extension;
}
Locally it will hit echo('test1') the first time, then it will only hit echo('test2'). When its on the server it always hits the echo('test1')
By default mkdir is not create a path recursively. An example if on your server you dont have a ../public/media folder, mkdir returns false and dont create a path.
To solve this pass a third parameter to mkdir as true:
mkdir('../public/media/img/' . $path, 0755, true);
Do yourself a favour and use absolute pathes...
You can use the constant __DIR__ to evaluate the folder in which the script actually resides.
Relative pathes are calculated from the current working directory, which can be different than __DIR__

php rename (move) from one dir to another that is two levels back

my script file path is:
C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php
I need to run code from this path that will move images from path:
C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04
to:
C:\xampp\htdocs\wordpress\wp-content\uploads\images
My problem is that I have no idea how to force "rename" go two directories back in path.
I was trying something like:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
error I'm getting atm (I've changed my folder permissions, so maybe it is something with path?):
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied
. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
edit rename code:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
$destPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/images');
/*$srcDir = opendir($srcPath);*/
echo $srcPath ;
sleep(1);
rename($srcPath, $destPath);
From looking at it, it looks wrong
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
it should be
$srcPath = realpath(dirname(__FILE__) . '../..' . '/uploads/2017/04/obrazek.png');
You have an extra / slash at the beginning.
It seems that you're in a Windows machine, so you're using the wrong type of slashes: /../..' . '/uploads/2017/04/obrazek.png'
You could try to replace them with backslashes:\..\..\uploads\2017\04\obrazek.png'
Or you could try something like this:
$path = substr($srcPath, 0, strrpos($srcPath, '\20'));
// $path now contaits: 'C:\xampp\htdocs\wordpress\wp-content\uploads';
// Then you can do:
$srcPath = $path . '\images';
Regarding the warning error:
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
It seems that you try to rename a file to directory, so probably you forgot to append the file name to the new path.
$srcPath contains a file. $destPath contains a directory, but it should contain the file name.
I've finally after many hours.. find out how to fix it so here you go:
$srcPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\2017\04') . '\obrazek2.png');
$destPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\images') . '\obrazek2.png');
rename ($srcPath , $destPath );
The key was adding file name . '\obrazek2.png' after dirname (), because if filename is inside dirname () and it is destination path in rename, then it returns empty... :)
Not sure if I'm clear enough, because of my English, but it works and hopes it will help someone.

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;

php mkdir folder for every 3 number combination

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.

mkdir function, doesn't appear to be working

I have created a mkdir function in my php webpage but it doesn't appear to be working.
Here it is:
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
user and name are defined above. Here's the snippet of code:
if (isset($_POST['name'])){
if (isset($_POST['desc'])){
$name = mysql_real_escape_string($_POST['name']);
$desc = mysql_real_escape_string($_POST['desc']);
$user = $check_user['Username'];
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
mysql_query("INSERT INTO `games`(`creator`, `name`, `desc`) VALUES ('$user', '$name', '$desc')");
header('Location: Games.php');
}
}
It is correctly running those queries into the database, but it isn't creating those directories.
Can you help?
check the current directory with:
echo __FILE__;
or
echo getcwd();
and build your path relative to this reference.
or
you can use chdir("/") to set the root directory as the current directory, then try to create your path.
Do you know where PHP executes? You have a relative system path (starting with Game). Chances are, your directory is getting created (if permissions allow), but in a location relative to the working directory, which is not necessarily the same place where your PHP script lives.
You using relative paths, it may be not the place you think directories are created but current directory/web-root/etc.
Try:
$path = "Game/" . $user . "/" . $name . ".actibuild";
is_writable('.') || die(realpath('.') . ' is not writable');
mkdir($path, 0777, true) || die(realpath($path).' directory not created');
print_r(realpath($path));

Categories