CodeIgniter zip archive with exclude a folder - php

I'm trying to create full site backup but i want to exclude a folder from archive.
My directory:
/application/
/backups/ >>> I dont want to archive this folder because of nested archiving.
/themes/
/uploads/
.htaccess
index.php
Tried following codes:
$this->load->library('zip');
$this->zip->read_dir(FCPATH);
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');

You can workout something like this, in which you will never have to manually enter the new directory you create
$this->load->library('zip');
$data = array_diff(scandir(FCPATH), array('..', '.','backups'));
// 'backups' folder will be excluded here with '.' and '..'
foreach($data as $d) {
$path = FCPATH.$d;
if(is_dir($path))
$this->zip->read_dir($path, false);
if(is_file($path))
$this->zip->read_file($path, false);
}
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');

My solution is a little manual that setting each root directories and files like following:
$this->load->library('zip');
// Choose directory and files
$this->zip->read_dir(FCPATH.'application', false);
$this->zip->read_dir(FCPATH.'themes', false);
$this->zip->read_dir(FCPATH.'uploads', false);
$this->zip->read_file(FCPATH.'.htaccess', false);
$this->zip->read_file(FCPATH.'index.php', false);
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');
Tested on CodeIgniter 3.x and Wamp Server

Related

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

create directory in parent root with php in laravel

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

Php: creating directory

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.

recursive copying of files and folders and sub folders

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

creating directory using 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);

Categories