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.
Related
I made this code below to find the path of file in my locat system, I would like to change this code to locate my-file and return its absolute path knowing that the only thing we remember about this file is that it is saved in a sub-folder /tmp/documents:
/tmp/documents can contain nested sub-folders and my-file can belong to any one of them.
If my-file cannot be found then your program should return NULL
Example:
locateFile() returns /tmp/documents/a/b/c/my-file if my-file is found into the folder /tmp/documents/a/b/c.
<?php
function locatefile(){
$flags = 0;
$pattern = 'my-file';
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir)
{
$files = array_merge($files, locatefile($dir . '/' .basename($pattern), $flags));
}
return $files;
}
// code test do not change it
$folder = '/tmp/documents/' . rand() . '/' . rand();
mkdir($folder, 0700, true);
touch($folder . '/my-file');
echo "EXAMPLE:$folder/my-file\n";
echo "RESULT:" . locatefile();
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__
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 ]]] )
I'm trying to recursively iterate through a group of dirs that contain either files to upload or another dir to check for files to upload.
So far, I'm getting my script to go 2 levels deep into the filesystem, but I haven't figured out a way to keep my current full filepath in scope for my function:
function getPathsinFolder($basepath = null) {
$fullpath = 'www/doc_upload/test_batch_01/';
if(isset($basepath)):
$files = scandir($fullpath . $basepath . '/');
else:
$files = scandir($fullpath);
endif;
$one = array_shift($files); // to remove . & ..
$two = array_shift($files);
foreach($files as $file):
$type = filetype($fullpath . $file);
print $file . ' is a ' . $type . '<br/>';
if($type == 'dir'):
getPathsinFolder($file);
elseif(($type == 'file')):
//uploadDocsinFolder($file);
endif;
endforeach;
}
So, everytime I call getPathsinFolder I have the basepath I started with plus the current name of the directory I'm scandirring. But I'm missing the intermediate folders in between. How to keep the full current filepath in scope?
Very simple. If you want recursion, you need to pass the whole path as a parameter when you call your getPathsinFolder().
Scanning a large directory tree might be more efficient using a stack to save the intermediate paths (which would normally go on the heap), rather than use much more of the system stack (it has to save the path as well as a whole frame for the next level of the function call.
Thank you. Yes, I needed to build the full path inside the function. Here is the version that works:
function getPathsinFolder($path = null) {
if(isset($path)):
$files = scandir($path);
else: // Default path
$path = 'www/doc_upload/';
$files = scandir($path);
endif;
// Remove . & .. dirs
$remove_onedot = array_shift($files);
$remove_twodot = array_shift($files);
var_dump($files);
foreach($files as $file):
$type = filetype($path . '/' . $file);
print $file . ' is a ' . $type . '<br/>';
$fullpath = $path . $file . '/';
var_dump($fullpath);
if($type == 'dir'):
getPathsinFolder($fullpath);
elseif(($type == 'file')):
//uploadDocsinFolder($file);
endif;
endforeach;
}
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.