Reduce time in Uploading and Deleting when managing files through ftp! Making a full directory into .zip and unzipping it on server!
Suppose you are going to upload a big size folder containing a huge number of files on it. Think about joomla. Then if you use ftp client software such as filezilla, it will take about 2 hours if you have internet connection that has avg. speed of 15kbps.
Again if you want to delete such files or directory from that hosting place then it will take 1 hour on that speed configuration.
This is happened because filezilla checks the permission and additional facts of each file by some commands every time it uploads or deletes a file.
We will just give a single file to filzilla to upload and will make several files into a single file when deleting. Thus we can reduce the time that filzilla takes for several files.
Here we go:
First make the desired directory to be uploaded into .zip file on your pc. For example Joomla.zip
The simply upload it to the server by Filezilla. It will take no more than 15 mins in a speed of 15kbps.
Then create a php file:
-
// Write an URL on the browser like
-
// http://www.example.com/project/unzip.php?archive=Joomla.zip
-
-
$filename = $_GET['archive'];
-
$zip = new ZipArchive;
-
if ($zip->open($filename) === TRUE) {
-
$zip->extractTo('./');
-
$zip->close();
-
echo 'The file <strong>'.$filename.'</strong> Extracted successfully';
-
} else {
-
echo 'failed';
-
}
-
?>
Run this script on that same directory and follow the instruction above the code. It will extract that Joomla.zip into Joomla. Then Delete the Joomla.zip in a sec. Thats it.
Now guess, you have uploaded a wrong folder wasting a huge time or you need to replace that directories with your customized files and folders. Now here we talk when deleting such a big directory. We dont know how big and recursive it is.
Create this file:
-
function removedir($dirname)
-
{
-
if (is_dir($dirname))
-
$dir_handle = opendir($dirname);
-
if (!$dir_handle)
-
return false;
-
while($file = readdir($dir_handle)) {
-
if ($file != "." && $file != "..") {
-
if (!is_dir($dirname."/".$file))
-
unlink($dirname."/".$file);
-
else
-
{
-
$a=$dirname.'/'.$file;
-
removedir($a);
-
}
-
}
-
}
-
closedir($dir_handle);
-
rmdir($dirname);
-
return true;
-
}
-
-
$dirname='Joomla';
-
removedir($dirname);
-
?>
This will delete that whole directory whether it is empty or not.
Here is a bonus function that will make a directory into a .zip file. You better know its usage. Just run it on the server and make a .zip copy of a desired directory.
-
// increase script timeout value
-
ini_set('max_execution_time', 300);
-
-
// create object
-
$zip = new ZipArchive();
-
-
// open archive
-
if ($zip->open('file.zip', ZIPARCHIVE::CREATE) !== TRUE)
-
{
-
die ("Could not open archive");
-
-
}
-
-
// initialize an iterator
-
// pass it the directory to be processed
-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("Joomla/"));
-
-
// iterate over the directory
-
// add each file found to the archive
-
foreach ($iterator as $key=>$value) {
-
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
-
}
-
// close and save archive
-
$zip->close();
-
echo "Archive created successfully Named file.zip";
-
?>




