Php: creating directory - php

I am trying to create folder and sub folder on website. Code is pretty simple. Not sure why does not work.
<?php
$domain = "officeactionuspto.com";
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain), 0777, true);
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images'), 0777, true);
$folder= $_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images';
if(is_dir($folder))
{
echo ("$folder is a directory");
}
else
{
echo ("$folder is not a directory");
}
?>

You don't have to use absolute path to create a directory.
You can just do it with following code:
mkdir('images', 0777, true);
$folder= 'images';
if(is_dir($folder)){
echo ("$folder is a directory");
}else{
echo ("$folder is not a directory");
}
you can also get the absolute path after created, if you desired:
$full_path = realpath('images');
PS: I'm supposing you are executing this code on /index.php, if was on another different structure, you need to write the relative path for it.
EDIT: I tested and eliminate a parentheses on mkdir and works.

You don't need to use the entire, absolute filename. You just need to use the path relative to the folder where the script being executed is located.
Although I don't know your file structure, lets pretend that the PHP script is in the crc folder: Your command would be: mkdir(('/website_templates/client_files/'.$domain), 0777, true);
EDIT: With the recursive parameter, you can create the images subfolder in the same command.

Related

failed to open stream: Permission denied open server

My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely

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);
?>

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 map file path from different folder location easily?

how to map the path to the file easily?
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt
so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt
addlog.php
<?php
include("../../api/function.php");
writetologfile('hahaha');
?>
How I can able to easily point to this text file path, no matter where my php calling script is from.
I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path,
something like $filename='/r/admin/logfile/testing_randomstring.txt',
but it is not working
Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
This uses getenv to read the contents of the environment variable $HOME.

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