PHP Copying a set of files to a differnt directory - php

I am trying to copy a bunch of xlsx files to a different directory, I want to know what is that I am doing wrong
// Move all xlsx files
$src = './';
// echo "<pre>";print_r($src);die;
$dst = 'files/';
//echo "<pre>"; print_r($dst);die;
$files = glob("*.xlsx");
//echo "<pre>"; print_r($files);die;
foreach($files as $file){
$file_to_go = ($dst,$file);
echo "<pre>";print_r($file_to_go);die;
copy($file, $file_to_go);
}

Related

How to Save Results from Foreach into Txt File

I would like to write all files from specific folder into .txt file. The output working just fine, but I'm getting only one file saved into my txt file. Any idea? Thanks!
$fileList = glob('C:\users\John\Documents\*pdf');
foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>';
$save_files = fopen("list.txt", "wb");
fwrite($save_files,$filename);
fclose($save_files);
}
}
Here is one way to do it...
foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>';
$list[] = $filename;
}
}
$capture = implode("\r\n",$list);
file_put_contents("list.txt",$capture);

How to move files to another folder in php

I have 4 csv files in "files" folder & I want to move these files with content to another folder (i.e. backups),Files are given below.
abc.csv
abc1.csv
abc2.csv
$files = scandir('files');
$destination = 'backups/';
$date = date('Y-m-d');
foreach($files as $file){
$rename_file = $file.'_'.$date;
move_uploaded_file($rename_file, "$destination");
}
Since you are not uploading any files, try rename() function instead.
$Ignore = array(".","..","Thumbs.db");
$OriginalFileRoot = "files";
$OriginalFiles = scandir($OriginalFileRoot);
$DestinationRoot = "backups";
# Check to see if "backups" exists
if(!is_dir($DestinationRoot)){
mkdir($DestinationRoot,0777,true);
}
$Date = date('Y-m-d');
foreach($OriginalFiles as $OriginalFile){
if(!in_array($OriginalFile,$Ignore)){
$FileExt = pathinfo($OriginalFileRoot."\\".$OriginalFile, PATHINFO_EXTENSION); // Get the file extension
$Filename = basename($OriginalFile, ".".$FileExt); // Get the filename
$DestinationFile = $DestinationRoot."\\".$Filename.'_'.$Date.".".$FileExt; // Create the destination filename
rename($OriginalFileRoot."\\".$OriginalFile, $DestinationFile); // rename the file
}
}
The function move_uploaded_file is relevant for uploading files, not for other things.
To move file in the filesystem you should use the rename function:
$files = scandir('files');
$destination = 'backups/';
$date = date('Y-m-d');
foreach($files as $file){
if (!is_file($file)) {
continue;
}
$rename_file = $destination.$file.'_'.$date;
rename($file, $rename_file);
}

How to get all text from multiple files

I'm trying to get all of the text from diffrent text files in a certain folder using PHP. I've already figured out how to do it for all the images in a folder but now i need a way to do the same for text files. This is the code i have so far.
<?php
$dir = 'Nieuws';
$file_display = ['txt'];
if (file_exists($dir) == false) {
return ["Directory \'', $dir, '\' not found!"];
} else {
$dir_contents = scandir($dir);
foreach ($dir_contents as $file) {
echo $file;
echo file_get_contents($file);
}
}
?>

Delete existing zip file

I have a working code which can download all images from a website. I grab all the images, put them in a cache folder, create the zip file and download them. The problem what I can't solve is to delete the zip after I downloaded it. Here is my code:
foreach($image as $im){
$info = explode('/',$im);
$file_name = 'cache/'.$info[count($info)-1];
copy($im,$file_name);
$to_zip[] = $file_name;
}
// Zipping
$result = create_zip($to_zip,'kepek.zip');
// Clear cache
$files = glob('cache/*');
foreach($files as $file) if(is_file($file)) unlink($file);
// Download
Header('Location: kepek.zip');
Use unlink after the header
foreach($image as $im){
$info = explode('/',$im);
$file_name = 'cache/'.$info[count($info)-1];
copy($im,$file_name);
$to_zip[] = $file_name;
}
// Zipping
$result = create_zip($to_zip,'kepek.zip');
// Clear cache
$files = glob('cache/*');
foreach($files as $file) if(is_file($file)) unlink($file);
// Download
Header('Location: kepek.zip');
unlink('Location: kepek.zip');

PHP rename folder in directory

How can i get $filename to be one specific path? Not a specific folder?
I want to rename several folders in folder named output.
I tried this:
$fileName = '/path/folder/output';
Here is my original code:
<?php
$fileName = '351437-367628';
$newNametemp = explode("-",$fileName);
if(is_array($newNametemp)){
$newName = $newNametemp[0];
print_r($newName); // lar navnet stå att etter første bindestrek
rename($fileName, $newName);
}
?>
$file_path = "uploads/";
$input = $_POST['name_of _the_folder_You WIsh']; // this is the new folder you'll create
$file_path .= $input . '/';
if (!file_exists($file_path)) {
mkdir($file_path);
}
chmod($file_path, 0777);
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
Here is a sample script that does (I think) what you're looking for. It loops through a directory, renaming all subdirectories.
Save code to demo.php, and chmod +x demo.php && ./demo.php.
If you'd prefer an object-oriented approach, or need any changes let me know and I'll modify the code.
#!/usr/bin/php
<?php
//root directory
$outputDir = '/path/folder/output';
//get all subdirectories in the root directory
$dirs = scandir($outputDir);
if ($dirs === FALSE) {
echo "scandir() failed.\n";
exit(1);
}
//loop through each subdirectory and rename.
foreach($dirs as $dir) {
$newName = "${dir}.tmp"; //rename as needed; here we append ".tmp" to the subdirectory.
$success = rename($dir, $newName);
if (!$success)
echo "Rename of $dir failed.\n"; //there was an error during rename, handle it.
}

Categories