mkdir and subfolders - php

Hi i'm not able to test this on the server, but if folder date(Y) does not exist, is this coding write for it to be created. or do I have to do mkdir("/o_rec/" . date(Y) by itself first? Will supporting subfolders all be created if not present
if(!is_dir("/o_rec/" . date(Y) . "/" . date(m) . "/" . $id)) {
mkdir("/o_rec/" . date(Y) . "/" . date(m) . "/" . $id);
}

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
you would need to set recursive to true

You should use set the "recursive" flag on you mkdir call to have it make the entire path you want.
http://php.net/manual/en/function.mkdir.php
Be advised that a google search makes it looks like there may be an existing bug, depending on your php version.

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 mkdir() not working correctly

i have this code:
$a ="/Assets/ProductImages/oa/91/2239754/6/5151010073180_1_org_zoom.jpg";
$b ="/home/cfnic/domains/modmania.ir/public_html/image/Assets/ProductImages/oa/91/2239754/6/5151010073180_1_org_zoom.jpg";
$path = '';
$directories = explode('/', dirname($a));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir('/home/cfnic/domains/modmania.ir/public_html/image/' . $path)) {
mkdir('/home/cfnic/domains/modmania.ir/public_html/image/' . $path, 0777,true);
}
}
it only create directory (Assets) and (ProductImages)
what am i doing wrong?????
If your purpose is to create directories recursively you can use mkdir once .
This is the prototype of the function taken from the PHP manual
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
so the only thing you really need is:
mkdir('/home/cfnic/domains/modmania.ir/public_html/image'.dirname($a), 0777,true);
without the foreach loop.

chmod(): No such file or directory

this might be a duplicate but dont know the reason why i got this error.. this code is fine when im using another laptop which the php version is 5.6 but when im using a laptop with php version 5.4 i got an error..
here is the code that im using..
public function uploadImg($file, $newname){
$path = "../valenciamd/captured_images/";
$fileparts = pathinfo($file["name"]);
$name = $newname . $fileparts["extension"];
if( is_dir($path) === false ){
mkdir($path);
}
$i = 0;
$parts = pathinfo($name);
// while (file_exists($path . $name)) {
// $i++;
// $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
// }
$name = $parts["filename"]. "." . $parts["extension"];
$success = move_uploaded_file($file["tmp_name"],
$path . $name);
chmod($path . $name, 0777);
return $path . $name;
}
$img = $reg->uploadImg( $_FILES['image'], $patientID.'.');
Are you sure that your php version is 5.4, maybe it's below 5.2 $path_parts['filename'] is only added since php 5.2 so you will get nothing. This is not an error, just update your php version.
Or if you do not want to update your php version, you can use
$parts['basename'] instead of $parts["filename"]. "." . $parts["extension"]
try to use the recursive directory creation
read the mkdir manual http://php.net/manual/en/function.mkdir.php
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive =
false [, resource $context ]]] )

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.

Categories