i have ran into bit of a problem, i want to create a temp dir for some image uploads.
here is what i have:
if i run root#dev:/var/www/media# tree draft/ it returns
draft/ [error opening dir]
the dir is not there (and thats correct), so i want php to create the dir
then i use this:
if(!is_dir(MEDIA_PATH . "draft")){
create_dir(MEDIA_PATH . "draft");
}
if(!is_dir(MEDIA_PATH . "draft/product")){
create_dir(MEDIA_PATH . "draft/product");
}
if(!is_dir(MEDIA_PATH . "draft/product/" . $temp_id)){
create_dir(MEDIA_PATH . "draft/product/" . $temp_id);
}
function create_dir($file){
echo "create_dir: {$file}" . PHP_EOL;
$dir = dirname($file);
if(!is_dir($dir)){
umask(0000);
if(false === mkdir($dir, 0777, true) && !is_dir($dir)){
throw new RuntimeException(sprintf("Unable to create the directory (%s).", $dir));
}
umask(0022);
}elseif(!is_writable($dir)){
throw new RuntimeException(sprintf("Unable to write in the directory (%s).", $dir));
}
}
(https://gist.github.com/keja/840291d1abb7355f07aa)
that results in
create_dir: /var/www/media/draft
create_dir: /var/www/media/draft/product
create_dir: /var/www/media/draft/product/56d038fe-0906-9442-c2d7-ab2b841467af
then if i run the tree command again
root#dev:/var/www/media# tree draft/
draft/
└── product
for some reason it will not create the last dir, any ideas?
the /var/www/media is a NFS mount, but should not have anything todo with the problem i guess.
EDIT: Problem is solved.
the solution was to remove the $dir = dirname($filename); and change the $file param to $dir instead.
function create_dir($dir){
if(!is_dir($dir)){
umask(0000);
if(false === mkdir($dir, 0777, true) && !is_dir($dir)){
throw new RuntimeException(sprintf("Unable to create the directory (%s).", $dir));
}
umask(0022);
}elseif(!is_writable($dir)){
throw new RuntimeException(sprintf("Unable to write in the directory (%s).", $dir));
}
}
Related
I have the following recursive method to delete a directory and all its sub-directories and files:
protected function _rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir . '/' . $object) == 'dir') {
_rrmdir($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
On occasion, the get a Warning, "Directory not empty".
The directory is actually created as a temporary holder for files. The files are downloaded from the Internet using the following snippet:
file_put_contents($filename, file_get_contents($file))
After they are downloaded (a write operation), they are then uploaded to a website (a read operation). Once done uploading, the temporary folder and its files are then deleted.
The odd thing is that when I look inside the temporary folder, there are no files there. It's as if the code tried to delete the folder while the last file was in the process of being deleted?
Any ideas what might be wrong and how to resolve it? I need this code to run on Windows and *nix, so a *nix only solution is not an option.
The constant DIRECTORY_SEPARATOR might help you with Windows/Unix compatibility.
For the folder not empty, try this:
protected function _rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
_rrmdir($dir . DIRECTORY_SEPARATOR . $object);
} else {
if( is_file($dir . DIRECTORY_SEPARATOR . $object) ) {
if(!unlink($dir . DIRECTORY_SEPARATOR . $object)) {
// code in case the file was not removed
}
// wait a bit here?
} else {
// code for debug file permission issues
}
}
}
}
reset($objects);
rmdir($dir);
}
}
It might happen that you try to remove a file which permissions are not at php exec level.
The is_file() method will return FALSE only if no read permissions, mind that write permissions are needed by the execution owner to delete files.
I want to create a new directory(folder) inside the webroot so i can create/edit files inside that new folder.
My code is:
$path = 'files' . DS . 'pathtofolder';// $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) { //this is to verify if created
echo "Exists";
}
else{
echo "Doesnt exist";
}
The result is always doesnt exist. And i cant find any created folder inside my cakephp folders.
I tried to change 'files' for 'webroot' but it doesnt work.
Is this the correct code to create a directory?
try this
$path = 'files' . DS . 'pathtofolder';
$folder = new Folder($path, true, 0755);
if ($folder->path) {
echo "Exists";
}
else{
echo "Doesnt exist";
}
There is a direct PHP call to create directories. So, you can use that. I dont know the cakephp version.
if (!mkdir($structure, 0777, true)) {
die('Failed to create folders...');
}
Also, check if your web-server user www-data has permissions to create directories. Otherwise, on command prompt run this command
addgroup www-data
That will add www-data to enable permissions. You might need to add sudo in some cases to above command if you get permission denied error.
$path = WWW_ROOT . 'webroot' . DS . 'FolderNameYouWantToSet' . DS;
if (!file_exists($path)) {
$oldMask = umask(0);
mkdir($path, 0777, true);
chmod($path, 0777);
umask($oldMask);
}
This will create a folder with 777 permission, you can set the folder permission as per your requirement.
WWW_ROOT is the global cake variable which stores the path up to the project.
The PHP version is 5.5.12 ( under WAMP 2.5 )
I want to create a directory recursively , it is on my development computer Windows7 at the moment but the production system is Linux :
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
At first run of the script the directory is created , but when I rerun the script then the code execution goes to the else block !
So how to make the mkdir always succeed ?
Do this:
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if(is_dir($dir)){
echo 'directory already exists';
}
else if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
There are all these resources for recursively looping through sub directories, but I haven't found ONE that shows how to do the opposite.
This is what I want to do...
<?php
// get the current working directory
// start a loop
// check if a certain file exists in the current directory
// if not, set current directory as parent directory
// end loop
So, in other words, I'm searching for a VERY specific file in the current directory, if it doesn't exist there, check it's parent, then it's parent, etc.
Everything I've tried just feels ugly to me. Hopefully someone has an elegant solution to this.
Thanks!
Try to create a recursive function like this
function getSomeFile($path) {
if(file_exists($path) {
return file_get_contents($path);
}
else {
return getSomeFile("../" . $path);
}
}
The easiest way to do this would be using ../ this will move you to the folder above. You can then get a file/folder list for that directory. Don't forget that if you check children of the directory above you then you're checking your siblings. If you just want to go straight up the tree then you can simply keep stepping up a directory until you hit root or as far as you are permitted to go.
<?php
$dir = '.';
while ($dir != '/'){
if (file_exists($dir.'/'. $filename)) {
echo 'found it!';
break;
} else {
echo 'Changing directory' . "\n";
$dir = chdir('..');
}
}
?>
Modified mavili 's code:
function findParentDirWithFile( $file = false ) {
if ( empty($file) ) { return false; }
$dir = '.';
while ($dir != '/') {
if (file_exists($dir.'/'. $file)) {
echo 'found it!';
return $dir . '/' . $file;
break;
} else {
echo 'Changing directory' . "\n";
chdir('..');
$dir = getcwd();
}
}
}
I am making a intranet customer manager in PHP. For each customer a directory is created for the shop to add files into. What my script is supposed do is if no directory exists create it, if it does exists dont create it.
What is actually happening is if the directory already exists I am getting the following error :
Warning: mkdir() [function.mkdir]: File exists in C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo m_code\custom_code.php(18) : eval()'d code on line 14
So what is happening it is trying to create it anyway, even though the if statement should stop it ?, im confused on what I am doing wrong :-S .
<?php
$customerID = $_GET['cfid'];
$directory = "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
$thisdir = getcwd();
mkdir($thisdir ."/customer-files/$customerID" , 0777); }
?>
Replace:
if(file_exists($directory) && is_dir($directory)) {
with:
$thisdir = getcwd();
if(file_exists($thisdir.$directory) && is_dir($thisdir.$directory)) {
or better:
<?php
$customerID = $_GET['cfid'];
$directory = "./customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
mkdir($directory , 0777); }
?>
Just took a short look but i would try this:
$directory = $thisdir . "/customer-files/$customerID";
and remove $thisdir from mkdir();
also you should move your $thisdir before the $directory declaration
The function file_exists() does not use relative paths, where is_dir() can. So instead, use the common denominator and pass an absolute path to these functions. Additionally you can move the call to getcwd() into the $directory assignment and reuse $directory later for creating the directory.
<?php
$customerID = $_GET['cfid'];
// Get full path to directory
$directory = getcwd() . "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
// Do nothing
}
else {
// Directory doesn't exist, make it
mkdir($directory , 0777); }
}
?>