PHP copy all images folder from one folder to another - php

I am working on some code that will take all the images in one folder and copy them into another folder and then delete the original folder and it content.
I have:
copy('images/old-folder/*', 'images/new-folder/');
unlink('images/old-folder/');
but this does not work :( but it doesn't work I mean the files dont copy over and the old-folder is not deleted :(
I even tried:
system('cp images/old-folder/* images/new-folder/');
and that didn't work either :( Please help.
I have even tried to change the permissions of the two folders:
chmod('images/old-folder/', 0777);
chmod('images/new-folder/', 0777);

foreach(glob('images/old-folder/*') as $image) {
copy($image, 'images/new-folder/' . basename($image)); unlink($image);
}
rmdir('images/old-folder');
check the docs: glob, rmdir, also you might find user comments on rmdir useful.
EDIT:
added basepath to the second parameter to the copy function which has to be an actual path and not a directory.

<?php
$src = 'pictures';
$dst = 'dest';
$files = glob("pictures/*.*");
foreach($files as $file){
$file_to_go = str_replace($src,$dst,$file);
copy($file, $file_to_go);
}
?>
Found here: PHP copy all files in a directory to another?

Here is a modified version from #Prasanth that should work (not tested)
<?php
$oldfolder = 'images/new-folder';
$newfolder = 'images/old-folder';
$files = glob($oldfolder . '/*');
foreach($files as $file){
$filename = basename($file);
copy($file, $oldfolder . '/' . $filename);
unlink($file);
}
rmdir($oldfolder);
?>

Related

How to delete a file which have such type of extensions in php?

What I'm trying to do is, to delete files which includes only such type of extensions.
Let's say a folder Images/ contains following files,
a.jpg
b.jpeg
c.php
d.php2
c.png
So now I want to delete only those c.php,d.php2. Is there any way to do this in a single step or any ideas ?
Note: In this case, I do not know exact name of the file or extension.
Thank you in advance.
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>

Adding all subdirectories to a zip archive

In this case I am trying to add all files and subdirectories to my zip file.
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
// Adding all files
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
$filename = substr($file, strrpos($file, '/') + 1);
echo $filename . '<br>';
$zip->addFile($file, 'wordpress/wp-content/themes/' . $title . '/' . $filename);
}
// Adding all subdirectories
$directories = glob(CLIENT_PATH . '/*' , GLOB_ONLYDIR);
foreach (glob(CLIENT_PATH . '/*', GLOB_ONLYDIR) as $dir) {
$zip->addEmptyDir('wordpress/wp-content/themes/' . $title . '/' . $dir);
}
$zip->close();
Adding all files works perfectly well but adding all subdirectories won't work as expected.
This is how my unzipped file looks:
The subdirectory of my main directory is called assets and it also includes some more subdirectories and they also have some subdirectories. But as you see in the image above, assets includes just nothing. And also, I don't understand why it starts with home/pomcanys/ etc. instead of assets.
How can I fix this? Any help would be appreciated!
To automatically include files from the sub-directories use the -r parameter for recursiveness:
zip -r foo.zip *

PHP Remove all Files From a Directory - Exclude File Extension

I am trying for the life of me to find the best way to delete all files in a single directory excluding a single file extension, ie anything that is not .zip
The current method I have used so far which successfully deletes all files is:
$files = glob('./output/*');
foreach($files as $file)
{
if(is_file($file))
unlink($file); // delete file
}
I have tried modifying this like so:
$files = glob('./output/**.{!zip}', GLOB_BRACE);
foreach($files as $file)
{
if(is_file($file))
unlink($file); // delete file
}
However, I am not hitting the desired result. I have changed the line as follows which has deleted only the zip file itself (so I can do the opposite of desired).
$files = glob('./output/*.{zip}', GLOB_BRACE);
I understand that there are other methods to read directory contents and use strpos/preg_match etc to delete accordingly. I have also seen many other methods, but these seem to be quite long winded or intended for recursive directory loops.
I am certainly not married to glob(), I would simply like to know the simplest/most efficient way to delete all files in a single directory that are not a .zip file.
Any help/advice is appreciated.
$exclude = array("zip");
$files = glob("output/*");
foreach($files as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if(!in_array($extension, $exclude)) unlink($file);
}
This code works by having an array of excluded extensions, it loads up all files in a directory then checks for the extension of each file. If the extension is in the exclusion list then it doesn't get deleted. Else, it does.
This should work for you:
(I just use array_diff() to get all files which are different to *.zip and then i go through these files and unlink them)
<?php
$files = array_diff(glob("*.*"), glob("*.zip"));
foreach($files as $file) {
if(is_file($file))
unlink($file); // delete file
}
?>
How about calling to the shell? So in Linux:
$path = '/path/to/dir/';
$shell_command = escapeshellcmd('find ' . $path .' ! -name "*.zip" -exec rm -r {}');
$output = shell_exec($shell_command);
I would simply like to know the simplest/most efficient way to delete all files in a single directory that are not a .zip file.
SPL Iterators are very effective and efficient.
This is what I would use:
$folder = __DIR__;
$it = new FilesystemIterator($folder, FilesystemIterator::SKIP_DOTS);
foreach ($it as $file) {
if ($file->getExtension() !== 'zip') {
unlink($file->getFilename());
}
}
Have you tried this:
$path = "dir/";
$dir = dir($path);
while ($file = $dir->read()) {
if ($file != "." && $file != ".." && substr($file, -4) !== '.zip') {
unlink($file);
}
}

php rmdir or unlink file from folder

The following code deletes the files in a folder uploads.How do I delete the folder as well when a user clicks Delete Folder (or similar).
I tried using rmdir but I am not getting errors only blank move.php file.
What's the correct/recommended way of doing it ? Please advice.
<?php
$actfolder = $_REQUEST['folder'];
require_once("models/config.php");
if(!securePage($_SERVER['PHP_SELF'])){
die();
}
require("models/db-settings.php");
if(isset($_GET['file'])){
$filename = "uploads/$loggedInUser->username$actfolder/" . ltrim($_GET['file'], '/\\');
// make sure only deleting a file in files/ directory
if (dirname(realpath($filename)) == realpath("uploads/$loggedInUser->username$actfolder/")) {
unlink($filename);
}
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Just try something like this:
$filename = "uploads/$loggedInUser->username$actfolder/";
if (is_dir($filename) === true)
{
$files = array_diff(scandir($filename), array('.', '..'));
foreach ($files as $file)
{
unlink(realpath($filename) . '/' . $file);
}
rmdir($filename); //remove directory
}

How to pass url to read png files in php

If I do
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
echo $file;
}
?>
And store the php file in the same folder as images I would get correct png files.
If I change location of php file How can I read png files located in other folder?
If I do
$dir = 'www.site.com/content/images';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
echo $file;
}
it does not display anything, is it necessary to the php file with code to be in the same folder of images?
You shouldn't add "www.site.com" to path. You should use absolute path from the root of your server (beginning with "/": /content/images) or relative path from current location of php script (content/images).
If you are in fact trying to retrieve files on a remote server, look at this:
http://www.php.net/manual/en/features.remote-files.php

Categories