creating directory using PHP - php

I need to create a directory using php with write permission...currently am using the following code
$folder_name = $this->input->post('foldername', true);
$path = '/home/temp/workspace/My_folder/documents/'.$folder_name;
mkdir($path,'0222');
But this is not working...

Does your application (Apache probably) have the right to write in that directory? Does the directory exist?
try this maybe:
$path = '/home/temp/workspace/My_folder/documents/'.$folder_name;
mkdir($path, 0222, $recursive=true);

Related

Using file_get_contents() php file do not get executed when placed in a directory only root?

Using file_get_contents($filePath) to get php file executed via Url
is working fine as long as the php file - $sassConfigName - is in the root
question:
if the file is in a directory, how can i use the file the same way in the url?
working example:
$sassConfigName = 'sassconfig.php'; // sassconfig.php is in the root
$filePath = 'http://bootstrap4.dev/'.$sassConfigName.'/main.scss';
file_get_contents($filePath);
Not working example:
$sassConfigName = './config/sassconfig.php'; // sassconfig.php is in directory config. But now wrong parameters in Url.
$filePath = 'http://bootstrap4.dev/'.$sassConfigName.'/main.scss';
file_get_contents($filePath);
Regards
<?php
$sassConfigName="/php/";
$filePath='http://www.w3schools.com'.$sassConfigName.'func_filesystem_file_get_contents.asp';
echo file_get_contents($filePath);
?>

PHP: using full URL in mkdir();?

I need to create a folder using mkdir(); in PHP.
But the issue that I have is that the $path is a full URL as opposed to full path on the server.
So i did a search on Google and some say it is possible and some say its not and I found this small code spinets on STO which is here:
Php mkdir() is not creating a directory in my web directory
$path = "https://wwww.domain.com/astuces/uploads/products/NICE;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
However, the code above doesn't really do anything.. it doesn't create any folder called NICE in the given $path string.
My question is, is this even possible USING PHP and if so how.. and what are my other options to achieve this if this is not possible using PHP?
Thanks in advance.

How to get all images in a folder using CakePHP

I am trying get all images which are located in standard cakePHP image folder. I am using:
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
$dir = new Folder('/app/webroot/img/');
$files = $dir->find('.*\.png');
pr($files);
but i always get empty array. Where is the problem?
Ina addition when I try make dir in that folder i get error:
mkdir(): No such file or directory [CORE\Cake\Utility\Folder.php, line 515]
By doing new Folder('/app/webroot/img/'); you're actually saying your app folder is in the root of the drive, and since it isn't, CakePHP will try and create it, which it can't (that mkdir error).
You probably need to do something like
$dir = new Folder(App.imageBaseUrl);
or
$dir = new Folder(APP_DIR . DS . "webroot" . DS . "img");.
Check the constants CakePHP gives you to handle paths http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#core-definition-constants should be usefull
Knowing the answer is given and accepted,
This is a right way given by the documents.
<?php
// Find all .png in your app/webroot/img/ folder and sort the results
$dir = new Folder(WWW_ROOT . 'img');
$files = $dir->find('.*\.png', true);
http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::find

JFolder::create: Could not create directory Joomla

I'm using below PHP code here to create a particular folder if it didn't exist. I'm using joomla 2.5
$path = my/path/goes/here;
$folder_permissions = "0755";
$folder_permissions = octdec((int)$folder_permissions);
//create folder if not exists
if (!JFolder::exists($path)){
JFolder::create($path, $folder_permissions);
}
But this code throws below error
JFolder::create: Could not create directory
What could be the reason for this?
2 things I think might be causing the problem:
You forgot a ; on the end of octdec((int)$folder_permissions)
Try removing the whole line $folder_permissions = octdec((int)$folder_permissions)
Update:
This is what I used to create a simple folder:
$destination = JPATH_SITE.'/'."modules/mod_login";
$folder_name = "new_folder";
JFolder::create($destination .'/'. $folder_name, 0755);
What could the reason be?
Simple: The user that tries to create that directory doesn't have permission (or your Joomla is broken).
The user that runs the code is probably www-data (on most *nix/Apache). ALso 'nobody' or 'apache' are possible.
If you have rootpermission try this:
1) become the webuser (eg www-data)
2) Jump to the directory my/path/goes/here
3) type: mkdir mynewdir
And you will probably find out that the user doesn't have sufficient permissions to do so.
edit your configuration.php file under Joomla home direcory, change:
From:
var $tmp_path = '/home/public_html/your_name/tmp';
To:
var $tmp_path = 'tmp';
and tmp should have 777 permission

Creating a file inside a folder that doesn't exist yet in php

I want my php script to create an output file in a folder based on the date. The way I'm doing this is that its supposed to get the foldername/filename from a text file outputted by another program which I am unable to edit.
So the file its grabbing the data from looks like this:
data/newfolder/10302008/log_for_Today.txt | 24234234
Only with multiple lines, I just need the script to go through it line by line, grab the folder/filename and create an empty file with that name in that location.
The directories are all 777. Now I know how to create a new empty exe file in a folder but can't seem to figure out how to create the folder first then the exe inside of it, any ideas?
if(!file_exists(dirname($file)))
mkdir(dirname($file), 0777, true);
//do stuff with $file.
Use the third parameter to mkdir(), which makes it create directories recursively.
With
$directories = explode( '/', $path );
you can split the path to get single directory names. Then go through the array and create the directories setting chmod 777. (The system user, who executes php must have the ability to do that.)
$file = array_pop( $directories );
$base = '/my/base/dir';
foreach( $directories as $dir )
{
$path = sprintf( '%s/%s', $base, $dir )
mkdir( $path );
chmod( $path, 777 );
$base = $path;
}
// file_put_contents or something similar
file_put_contents( sprintf( '%s/%s', $base, $file ), $data );
The problem here is that you might not set chmod from your php script.
An alternative could be to use FTP. The user passes FTP login data to the script and it uses FTP functionality to manage files.
http://www.php.net/FTP
It's little too late but I found this question and I have a solution for this, here is an example code and it works good for me no matter how deep is your file. You should change directory separator for lines 1 and 3 if you're running it on Windows server.
$pathToFile = 'test1/test2/test3/test4/test.txt';
$fileName = basename($pathToFile);
$folders = explode('/', str_replace('/' . $fileName, '', $pathToFile));
$currentFolder = '';
foreach ($folders as $folder) {
$currentFolder .= $folder . DIRECTORY_SEPARATOR;
if (!file_exists($currentFolder)) {
mkdir($currentFolder, 0755);
}
}
file_put_contents($pathToFile, 'test');
Best regards, Georgi!
Create any missing folders using mkdir(), then create the empty file using touch().
You can use absolute paths in both cases, meaning:
mkdir('data');
mkdir('data/newfolder');
mkdir('data/newfolder/10302008');
touch('data/newfolder/10302008/log_for_Today.txt');
if you're curious about where it's starting-point it will be, you can use getcwd() to tell you the working directory.
Can't you just do this by creating the dir with mkdir (http://nl.php.net/manual/en/function.mkdir.php) then chmod it 777 (http://nl.php.net/manual/en/function.chmod.php) the change directory with chdir (http://nl.php.net/manual/en/function.chdir.php) and then create the file (touch)?

Categories