I am using the function i have written to copy directories and files. It just copies the directories and files once. How can i copy the files from source folder to destination folder only if the file does not exist?
here is the function. Basically i want to copy the folder labref in analyst_uploads and its content to the second folder store_for_issue. If a file is uploaded to the source folder on the same day, the files should be copied to the destination folder 'store_for_issue'. Create folder if does not exist or update the contents
public function full_copy( ) {
$labref= $this->uri->segment(3);
$source='analyst_uploads/'.date('Y').'/'.date('M').'/'.$labref;
$newfolder='store_for_issue';
if(is_dir($newfolder)){
mkdir($newfolder.'/'.$labref,0777,TRUE);
}
$target=$newfolder.'/'.$labref.'/';
copy( $source, $target );
}
How about this:
public function full_copy( ) {
$labref= $this->uri->segment(3);
$source='analyst_uploads/'.date('Y').'/'.date('M').'/'.$labref;
$newfolder='store_for_issue';
if(!is_dir($newfolder)){
mkdir($newfolder.'/'.$labref,0777,TRUE);
}
$target=$newfolder.'/'.$labref.'/';
shell_exec('cp -ur '. $source . ' ' . $target);
}
This uses linux cp -ur and will only copy files that don't exist in target directory, or replace files in target directory that are older than the source files. It will operate recursively.
Of course if you wanted a PHP-only solution, you could perhaps use a RecursiveDirectoryIterator or similar to iterate through all the files in the source directory and compare/copy them one by one.
if (!file_exists($target)) {
copy( $source, $target );
}
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');
Please answer this question and help me!!
I have found a dozen of code snippets about deleting a folder with its all files and sub folders in PHP. But I am facing difficulties to apply them.
This is an example code :
function rrmdir($path) {
// Open the source directory to read in files
$i = new DirectoryIterator($path);
foreach($i as $f) {
if($f->isFile()) {
unlink($f->getRealPath());
} else if(!$f->isDot() && $f->isDir()) {
rrmdir($f->getRealPath());
}
}
rmdir($path);
}
I can not understand how to write the source directory. I mean, how I set the $path variable? All the files of my website is inside 'public_html' folder. Will I include 'public_html' also?
When I want to delete a folder inside 'usercontent' folder, I am writing like this :
$path = "/usercontent/somefolder"
Please answer with an example directory path.
Update: What should be the permission of the folder which will be deleted.
i have a folder that i will save some files that their name will be the microtime that they will create.
there is any way to load just the group of the files names, that their name is bigger then the microtime that i will write?
(in glob() and in readdir() it load all the files names that in the dir, and just after that you can search whatever you want)
i will have in the folder a lot of files. so it will be inefficient to load all the files names first.
example:
if in the dir i have this files:
1413030375194
1413030375717
1413030377193 // for example, i want it to load from here
1413030377685
1413030378165
1413030378411
i want to load just the files that their name bigger then "1413030377000"
so it will load to me just the files names:
1413030377193
1413030377685
1413030378165
1413030378411
foreach(glob("*") as $file) {
if($file >= $YOUR_MIN_FILE_NAME) {
// do whatever you want...
}
}
$safer = escapeshellarg( $_REQUEST['search'] );
$results = glob( "$dir/*$safer*" );
Replace your file name with $_REQUEST['search']
I am trying to make a script that lists only folders within a folder. The thing is that I have users who can create folders within their folders so basically what I'm trying to do is to have a way for users to manage their folders and storing files... My problem is that I want to make php "think" that the root folder is their home directory and they cannot go upper than their home directory. Currently my php function doesn't do that, it only shows the content of the directory...and if the user goes one level up and again one level up ...and so on....he could browse the entire hard drive.
function directoryList($path) {
$dirStruct = array();
if(is_dir($path)) {
$handle = opendir($path);
while(($file = readdir($handle)) !== false) {
if(#opendir($path.$file)) {
chdir($path.$file);
$absolutepath = getcwd();
$dirStruct[] = array('path' => $absolutepath.'\\', 'name'=>$file);
}
}
}
return $dirStruct;
}
Instead of giving the user an absolute path, only allow them to specify paths which are relative to a given base path. Next, write a function which removes any "/../" for the relative path and you're safe (as long as users can't create links on the server ...).
If you want to be nice, you can match the ".." with the element before that (so "a/../b" would become "b", i.e. the ".." remove the "a") or ignore the ".." if there is no path element before it.
Here is a little something to expand on:
function listFolders($folderPath, $homeFolder)
{
$folderPath = realpath($folderPath);
$homeFolder = realpath($homeFolder);
if(strpos($folderPath, $homeFolder) === 0) {
return glob("$folderPath/*", GLOB_ONLYDIR);
}
}
$dirs = listFolders('/home/gordon/code/php', '/home/gordon');
print_r($dirs);
For $folderPath you pass in the folder you want to list the directories from. For $homeFolder pass in the folder you want to be the top most folder. By realpathing both paths you make sure they are resolved to absolute paths. If the $folderPath is below the $homeFolder, then the $folderPath string will start with and contain the entire $homeFolder string. If this is the case, we just glob all directories in the $folderPath and return their absolute pathes in an array.
To get the relative path of the $folderPath from the $homeFolder, just do
ltrim(str_replace('/home/gordon/', './', '/home/gordon/code/php/'), '/');
which would return ./code/php/.
If you want to do this with OOP, you might be interested in the SPL DirectoryIterator.
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)?