PHP - Extracting all files of rar archive to main folder - php

I have a rar file with the many files and folders. I want to extract files in the sub-folders of the rar file to the main folder.
I have tried this:
$archive = RarArchive::open('example.rar');
$entries = $archive->getEntries();
foreach ($entries as $entry)
$entry->extract($dir);
$archive->close();
However this extracts the files to the same folder, rather than the main folder.
Any suggestions?

i tried an own solution, and it's works:
$archive = RarArchive::open('example.rar');
$entries = $archive->getEntries();
foreach ($entries as $entry)
{
$fileinfo = pathinfo($entry->getName());
copy("rar://".$file."#".$entry->getName(), $dir.'/'.$fileinfo['basename']);
}
$archive->close();
for don't extract folders (empty folders), we can put
if(!empty($fileinfo['extension']))
before copy function.
thanks to me :-)

Related

phpSPO library - How to copy subfolders in Sharepoint

I am using the phpSPO library for sharepoint: https://github.com/vgrem/phpSPO
I am able to copy a folder along with it's files using:
$credentials = new ClientCredential("MyCredentialsGoHere","MyCredentialsGoHere");
$ctx = (new ClientContext("https://MyURL.sharepoint.com/enquiries"))->withCredentials($credentials);
$sourceFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$targetFolder = $sourceFolder->copyTo("Shared Documents/".$test, true)->executeQuery();
However it does not copy any subfolders.
I assume that I have to iterate through the subfolders of my source directory manually doing a copy for each one to the new target directory.
My starting point for this was to list the subfolders in a SharePoint folder:
$credentials = new ClientCredential("MyCredentialsGoHere","MyCredentialsGoHere");
$ctx = (new ClientContext("https://MyURL.sharepoint.com/enquiries"))->withCredentials($credentials);
$sourceFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$subfolders = $sourceFolder->getFolders()->executeQuery();
foreach ($subfolders as $folder) {
print_r($folder);
}
However this doe not work as I expected (it produces no output) and my resources are exhausted.
Any pointers to help me solve the issue or an example of a finished solution would be very helpful.
This will loop the folders in a folder:
$parentpath = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$folders = $parentpath->getFolders();
$ctx->load($folders);
$ctx->executeQuery();
foreach ($folders->getData() as $folder) {
$display = $folder->getProperty("ServerRelativeUrl");
echo ("File name: ".$display."<br/>");
}

"the system could not find the file specified" when loops on folder and rename each file

I'm looping unto directories and each directory consist of multiple files that yet to be rename. Below is the code
<?php
$path = __DIR__.'/';
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $f ){
$files2 = array_diff(scandir($path.'/'.$f), array('.', '..'));
foreach( $files2 as $f2 ){
rename($path.'/'.$f.'/'.$f2, $path.'/'.$f.'/'.strtolower(str_replace(' ','_',$f2)));
echo 'success<br>';
}
}
above codes return an error of
The system cannot find the file specified. (code: 2)
in each directory, some of the files has the name that consist of special character(s) e.g. Velāyat-e Nūrestān.json.
Any ideas ?
Your code,in the actual state can't work because there are a lot of issues in it.I suggest to use native Directory Iterator to achieve this properly .
You can use:
$root=__DIR__.'/';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));//create a recursive directory iterator
$it->rewind();
while($it->valid())
{
if (
!$it->isDot()//if file basename not in ['.','..']
&&$it->isFile()//and is really a file and not a directory
)
{
rename(str_replace('/','\\',$it->getPathname()),str_replace('/','\\',$it->getPath().'\\'.mb_strtolower(str_replace(' ','_',$it->getBasename()))));//try to rename it
echo "success";
}
$it->next();
}

Load files from Plugins directory

I need to load index.php file from each of the plugins' folders. There is the main folder "plugins" and inside there are, sub folders (plugins) e.g blog, members etc. Inside each plugin folder there is an index.php file which i need to load. How can i load the directory and search for these files. The plugin folders are not static and might change.
What i have tried
$dir_iterator = new RecursiveDirectoryIterator($this->plugin_dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish
foreach ($iterator as $file) {
echo $file, "\n";
}
and..the glob function (which didn't help much
$list = glob('index.php', GLOB_BRACE);
foreach($list as $files){
echo $files;
}
print_r($list);
I used a double listing way..
private function loadPlugins(){
$dir = array_diff(scandir($this->plugin_dir), array('..', '.'));
foreach($dir as $ds){
$list = glob($this->plugin_dir.'/'.$ds.'/index.php', GLOB_BRACE);
foreach($list as $files){
require $files;
}
}
}

Displaying files from resources folder and making them downloadable

I am trying to create a sort of download list where I grab all the files from the resources folder.. my php file is in a different folder to that..
and then I want to make them like links so people can download those files
I tried this:
<?php
foreach(glob('*.*') as $filename) {
echo $filename."<br />";
}
?>
However I don't know how to grab files from my resources folder or make them downloadable :(
Cheers!
You can scan the directory and grab the the files with scandir
$dir = '/tmp';
$files1 = scandir($dir);
//Check if file is not . OR ..
$ignore = array(".", "..");
foreach($files1 as $key => $value){
if (!in_array($value, $ignore)) {
echo $value;
}
}
For more information read :
http://php.net/manual/en/function.scandir.php
In order to make a downloadable link I would like to know what kind of files you want to download.

PHP, different destination adding files to .zip?

Okay let me explain. I have two folders on my server, let's say they're called f1/ and f2/.
Both folders have several files. I'd like to ZipArchive both folders. However, the best I can do is getting the .zip to contain both folders. What I want is to take all the files and folders WITHIN both f1/ and f2/ and archive them, thus having the content of f1/ and f2/ in the root of the .zip, not the two folders.
This is the code I'm currently using, which, like I said, doesn't do what I want:
$zipname = 'ZipArc.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($array as $file => $value)
{
$zip->addFile("f1/" . $file . ".ini");
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("f2/Data/"));
foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $key);
}
}
$zip->close();
I've searched and searched, but I can't seem to hit the right keywords to find a solution.
Read the docs closer: http://php.net/manual/en/ziparchive.addfile.php
bool ZipArchive::addFile($filename, $localname, ....)
^^^^^^^^^^
so
$zip->addFile('/real/path/on/your/server/file.txt', '/path/within/zip/foo.bar');

Categories