create directory in parent root with php in laravel - php

my site is in /var/www/mysite directory .i use laravel and want to create directory in /uploads directory .so /uploads is besides of /var .
/var/www/mysite
/uploads
i set 0777 permission for /uploads but permission denied happened.
I try with this code
<?php mkdir('/uploads', 0777 , true) ; ?>

Your web user (www-data, apache or whichever) is restricted by the operating system to use /var/www. You can't read,write,delete any files outside /var/www. So that's not a laravel issue.

Function that create all directories (folders) of given path. No need to write code create each directories (folders) of given path. it will create all directories (folders).
Like : If you want to create directory structure like
organizations / 1 / users / 1 /
So you only need to call this function with directories path like
$directories_path = 'organizations/1/users/1/';
createUploadDirectories($directories_path);
/*
* Method Name : createUploadDirectories
* Parameter : null
* Task : Loading view for create directries for upload
*/
if ( ! function_exists('createUploadDirectories')){
function createUploadDirectories($upload_path=null){
if($upload_path==null) return false;
$upload_directories = explode('/',$upload_path);
$createDirectory = array();
foreach ($upload_directories as $upload_directory){
$createDirectory[] = $upload_directory;
$createDirectoryPath = implode('/',$createDirectory);
if(!is_dir($createDirectoryPath)){
$old = umask(0);
mkdir($createDirectoryPath,0777);// Create the folde if not exist and give permission
umask($old);
}
}
return true;
}
}

Do this:
Use File;
File::makeDirectory(base_path() . '/path/to/directory', 0775, true);
This will attempt to create /path if it doesn't exist. Then /path/to if it doesn't exist. And finally /path/to/directory if it doesn't exist. If all created directories are successfully created then true will be returned.
If you want to place the directory in your public path; use:
File::makeDirectory(public_path() . '/path/to/directory', 0775, true);
Source: Laravel Recipes

Related

PHP mkdir returns 1 but directory does not exist

I am creating directory in php with mkdir it returns true but when i ssh into my server i cannot find directory in specified path.
I have checked in different locations in server.
if (!file_exists('/tmp/tmpfileeee')) {
mkdir('/tmp/tmpfileeee',0755);
echo 'created';
}
Does tmp exist where this is executing? Is tmpfileee a file or directory you are trying to create? If tmp does not exist and neither does tmpfileee, I believe you are trying to make 2 directories without a recursive parameter in the call.
My PHP is definitely rusty so maybe someone else can answer better but that was just my initial thoughts looking at it.
Just try as that:
if (!file_exists('tmp/tmpfileeee') AND !is_dir('tmp/tmpfileeee')) {
mkdir('tmp/tmpfileeee',0755, true);
echo 'created';
}
mkdir creates a folder not file.
If you want to create an file:
if (!file_exists('tmp/tmpfileeee') AND !is_file('tmp/tmpfileeee')) {
$fp = fopen('tmp/tmpfileeee', 'w');
echo 'created';
}
or best way:
// 1. Check folder and xreate if not exists
if (!file_exists('tmp') AND !is_dir('tmp')) {
mkdir('tmp',0755, true);
echo 'folder created';
}
// 2. Check file and create if not exists
if (!file_exists('tmp/tmpfileeee') AND !is_file('tmp/tmpfileeee')) {
$fp = fopen('tmp/tmpfileeee', 'w');
echo 'file created';
}
UPDATE
On some servers, the tmp and temp folders are restricted.
Check for open_basedir.
PHP manual states:
If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.

PHP mkdir(); not working

I've been trying the function Mkdir that will be usefull in the project i'm working on. I've tried the simplest code possible but I can't get it to create the folder I want.
I've tried to changes my folder permissions, but that doesn't change (Nor 755 or 777) and the code keeps returning a fail.
Please have a look at my code :
<?php
if(!mkdir($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777, true))
{
echo("echec");
}
chmod($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777);
?>
The parent folder is "admin" and it permissions are set to 755.
Do you have any clue why this isn't working ?
EDIT : I remade it and it worked, no clue what the problem was.
Code
mkdir('/2017', 0777, true)
creates folder 2017 is a root folder of a file system.
Always set ethier full path to your folder, e.g.:
mkdir($_SERVER['DOCUMENT_ROOT'] . '/2017', 0777, true);
// or
mkdir('/var/www/mysite/2017', 0777, true);
Or use . or .. to define proper location:
// folder will be created in a same directory
// as a script which executes this code
mkdir('./2017', 0777, true);
// folder will be created in a directory up one level
// than a script which executes this code
mkdir('../2017', 0777, true);
So, in your case it is obviously:
mkdir($_SERVER['DOCUMENT_ROOT'] . '/admin/2017', 0777, true);
Example #1 mkdir() example
<?php
mkdir("/path/to/my/dir", 0700);
?>

ftp_mkdir(): Can't create directory: No such file or directory

I am having trouble making remote directory using ftp.
I am getting error as Warning: ftp_mkdir(): Can't create directory: No such file or directory
I am trying this way.
$connection = ftp_connect($hostname) or die('Couldn\'t connect to ftp server');
$login = ftp_login($connection, $username, $password) or die('Couldn\'t log_in to ftp server');
ftp_pasv($connection, true);
if ($login){echo 'Connected Successfully.';}else{echo 'Cannot connect.';}
$dir = '2014/04/09';
if(!#ftp_chdir($connection, '/public_html/images/'.$dir)){
$ftp_mkdir = ftp_mkdir($connection, '/public_html/images/'.$dir);
if($ftp_mkdir){
echo 'Remote directory created successfully';
}
else{
echo 'Error creating remote directory';
}
}
else{
echo 'Remote directory exist';
}
Please see and suggest any possible way to do this.
Thanks.
Had a similar problem and fixed by creating one folder at a time. I was not able to make ftp_chdir create the entire path on a single command.
So to create this:
$dir = '2014/04/09';
I had to create first:
$dir = '/2014/';
Then:
$dir = '/2014/04/';
At last:
$dir = '/2014/04/09/';
And then submit your file '/2014/04/09/image.jpg'.
Worked like a charm.
In Windows * . " / \ [ ] : ; | = , are reserved characters and are not allowed to be used in your file names.
For Linux I think it's just / and \0
The / character is used to traverse file directories in Linux hence why it is forbidden to use as a file name.
For more info on illegal file names:
http://en.wikipedia.org/wiki/Filename#Reserved%5Fcharacters%5Fand%5Fwords
You will have to substitute your / symbol with something else such as -
In replay to your comment:
To make a directory like images/2014/04/09/image.png
You just need to create your folders on your server. So images/2014/04/09/image.png is a file called image.png which is inside the folder 09 which is inside the folder 04 which is inside the folder 2014 which is inside the folder images
Hope that makes scene?

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

Forcing file-creation in public directory?

Here's an idea. I'm trying to create file from PHP script. File may be created in any public place on server (in public root or in its subdirectories). Bottom of line, I want to write function like this (it must work on both unix and windows servers).
function create_file($path, $filename, $content, $overwrite=false) {
# body
...
}
Parameters:
$path - file-path (empty or ends with /)
$filename - any valid file-name (requires dot+extension)
$content - file-content (anything)
$overwrite - if set true, existing file will be overwritten (default. false).
Result:
Function returns TRUE and creates file with content, or FALSE if file-creation was not possible for any reason.
Description of problem:
I expect this function to return true and create file $filename on path $path, or false if it wasn't possible for any reason. Also if file was successfully opened for writing, need to put $content in it.
Argument $path is relative path to public directory, and $filename is any valid filename. Of course, I want to avoid creating files if path points outside of public directory. Function may be called from subdirectory-scripts like this example
# php script: /scripts/test.php
create_file('../data/', 'test.info', 'some contents');
# result file: /data/test.info
What have I tried so far?
I’ve tried doing this with fopen() and fwrite() functions and that works on some servers, and doesn’t work on some. I guess there’s problem with writing privileges and chmod() but to be honest I’m not very familiar with chmod attributes. Also I couldn't check if $path points outside of server's public directory.
In short, I want this function to create file and return TRUE if file doesn't exist, or file exists and $owerwrite=true. Otherwise, nothing happens and function returns FALSE.
Additionally, I would like to know reason why file can't be created on some path (just in theory). Incorrect path/filename is only thing I have on my mind and I'm sure there's more about this problem.
Thanks in advance for any sample/example/suggestion.
update code
So far I have this code...
function create_file($path, $filename, $content, $overwrite=false) {
$reldir = explode('/', trim(str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])), '/'));
$dirdepth = sizeof($reldir);
$rpadd = $dirdepth > 0 ? '(\.\./){0,' . $dirdepth . '}' : '';
$ptpath = '#^/?' . $rpadd . '([a-z0-9]\w*/)*$#i';
$ptname = '/^[a-z]\w*\.[a-z0-9]+$/i';
if ($res = preg_match($ptpath, $path) && preg_match($ptname, $filename)) {
$res = false;
if ($overwrite === true || !file_exists($path.$filename)) {
if ($f = #fopen($path.$filename, 'w')) {
fwrite($f, $content);
fclose($f);
$res = true;
}
}
}
return $res;
}
Some suggestions:
set the owner of the web server document root: chown -R apache:apache /var/www (i suppose /var/www is your document root and that the web server apache runs with user apache). Set the privilegies of the document root like this in order to have all directories under document look with privilegies 755 (only owner which is user apache can write in folders /var/www and sub folders)
Block paths that point out of your /var/www document root: you are under the issue known as http://en.wikipedia.org/wiki/Directory_traversal_attack. What about if the $path is something like: /var/www/../../../etc/passwd?
Basename php function can help you identifying this kind of malignous paths. Look this post: php directory traversal issue
To check wheter a file already exists or not: http://php.net/manual/en/function.file-exists.php
All file functions in php will not work properly in two circumstances
If you don't have enough user privileges for applications, then
You may not used/passed the arguements/parameters correctly .

Categories