I'm beginner in php. With a scenario i wanted to create directory with forward slash in name(09/01/2017). How can i resolve it?
$my_date = "09/01/2017"
$new_path = "../Images/".$my_date;
if(!file_exists($new_path)) {
mkdir($new_path , 0777);
}
EDIT: I'm using macos with php server in it. In macos it is possible to create folder with slashes.
<?php
$my_date = "09/01/2017";
$new_path = "images/".$my_date;
if (!is_dir($new_path))
{
mkdir($new_path , 0777,true);
}
?>
mkdir($new_path , 0777,true);//true for recursive directory creation.
In your case if you create directory with 09/01/2017 it will be created
File Tree:
images
--09
--01
--2017
because file system not allowed forward slash as directory name.Instead of this you can create 09012017 or 09-01-2017.
Hey #Milan Mendpara i would like to tell you that you can not make any folder with name char /:*?"<>|, even you can not make a directory in you OS as well. when you try it in you OS then below case will arise
So i think you should change your directory from 09/01/2017 to 09-01-2017, IN my case i dont have ../'Image' directory so i just make a directory where my php file is present so below is you code
<?php
$my_date = "09-01-2017";
$tempDir = __DIR__ . DIRECTORY_SEPARATOR . $my_date; // __DIR__ means a path where your php file is present and DIRECTORY_SEPARATOR means __DIR__.'/' and then give you directory name like __DIR__ . DIRECTORY_SEPARATOR . $my_date
if(!is_dir($temp_dir)){
mkdir($temp_dir);
}
?>
Related
I need to set $destination path to outside project need to copy folder from project file to computer location i have already tried using copy directory function
public function approovenew()
{
$source = "themes/call";
$destination = "/new-theme";
File::copyDirectory(base_path($source), base_path($destination));
}
Try to do this it will help you out
File::copyDirectory(__DIR__ . '/../whatever'$source, __DIR__ . '/../../$destination');
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
I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.
I need some help with concepts and terminology regarding website 'root' urls and directories.
Is it possible to determine a website's root, or is that an arbitrary idea, and only the actual server's root can be established?
Let's say I'm writing a PHP plugin that that will be used by different websites in different locations, but needs to determine what the website's base directory is. Using PHP, I will always be able to determine the DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and absolute directory path of the server (or virtual server). But I can't know if the website itself is 'installed' at the root directory or in a sub directory. If the website was in a subdirectory, I would need the user to explicitly set an "sub-path" variable. Correct?
Answer to question 1: Yes you need a variable which explicitly sets the root path of the website. It can be done with an htaccess file at the root of each website containing the following line :
SetEnv APP_ROOT_PATH /path/to/app
http://httpd.apache.org/docs/2.0/mod/mod_env.html
And you can access it anywhere in your php script by using :
<?php $appRootPath = getenv('APP_ROOT_PATH'); ?>
http://php.net/manual/en/function.getenv.php
Will $url and $dir always be pointing to the same place?
Yes
<?php
$some_relative_path = "hello";
$server_url = $_SERVER["SERVER_NAME"];
$doc_root = $_SERVER["DOCUMENT_ROOT"];
echo $url = $server_url.'/'. $some_relative_path."<br />";
echo $dir = $doc_root.'/'. $some_relative_path;
Output:
sandbox.phpcode.eu/hello
/data/sandbox//hello
You shouldn't need to ask the user to provide any info.
This snippet will let your code know whether it is running in the root or not:
<?php
// Load the absolute server path to the directory the script is running in
$fileDir = dirname(__FILE__);
// Make sure we end with a slash
if (substr($fileDir, -1) != '/') {
$fileDir .= '/';
}
// Load the absolute server path to the document root
$docRoot = $_SERVER['DOCUMENT_ROOT'];
// Make sure we end with a slash
if (substr($docRoot, -1) != '/') {
$docRoot .= '/';
}
// Remove docRoot string from fileDir string as subPath string
$subPath = preg_replace('~' . $docRoot . '~i', '', $fileDir);
// Add a slash to the beginning of subPath string
$subPath = '/' . $subPath;
// Test subPath string to determine if we are in the web root or not
if ($subPath == '/') {
// if subPath = single slash, docRoot and fileDir strings were the same
echo "We are running in the web foot folder of http://" . $_SERVER['SERVER_NAME'];
} else {
// Anyting else means the file is running in a subdirectory
echo "We are running in the '" . $subPath . "' subdirectory of http://" . $_SERVER['SERVER_NAME'];
}
?>
I have just had the same problem. I wanted to reference links and other files from the root directory of my website structure.
I tried the following but it would never work how I wanted it:
$root = $_SERVER['DOCUMENT_ROOT'];
echo "Link";
echo "Link";
But the obvious solution was just to use the following:
echo "Link";
echo "Link";
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)?