Delete all files with a php script [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I delete all files with a PHP script on a server? My problem is that I have many files that need to be deleted and over the FTP client it takes quite a long time.

you can delete all files using for loop, example:
`
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
`

Untested but this should work:
$dir = "dir/to/purge/"; // Directory you want to remove all the files from
$listing = scandir($dir); // Array containing all files and directories inside the provided directory
foreach($listing as $file) {
if(is_file($dir . $file)) {
unlink($dir . $file); // Removes the file from the listing
}
}
Note that with a lot of files this could use up all your memory.

Related

Laravel Reading files in storage [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have saved images in a file in the storage. I would like to get the files saved into an array using exec command in the controller and then pass the array into the blade. Is there any way to do it?
$images= [];
// $filesInFolder = \File::files('images'); getting files from the folder
$files = file_get_contents( 'filename' ); // replace here the filename with you full path of the file
foreach($files as $path)
{
$images[] = pathinfo($path);
}
Then you can pass the images/files in your blade.

Create multiple CSV file with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm following this answer to create a csv file with PHP. But how can I create multiple csv files? Everyone said that those csv files must be sent into zip file. But I don't know how to.
I tried to double the code to make 2 csv files, and then add this at the end, but the zip contains nothing after I downloaded it.
Firstly you will need to create the ZIP folder, create and save each CSV to a temporary location, then add them to the ZIP, delete the temporary CSV file, and finally download the ZIP.
As I don't know what code you're working with exactly here is some code that you should be able to put into your project
// create the ZIP file
$zip_name = 'csv.zip';
$zip = new ZipArchive;
$zip->open($zip_name, ZipArchive::CREATE);
$files = array('file_1.csv', 'file_2.csv', 'file_3.csv');
$temp_directory = 'path to directory';
// create each CSV file and add to the ZIP folder
foreach($files as $file) {
$handle = fopen($temp_directory . $file, 'w');
$data = array(); // data maybe from MySQL to add to your CSV file
// add your data to the CSV file
foreach($data as $d) {
fputcsv($handle, $d);
}
fclose($handle);
// add this file to the ZIP folder
$zip->addFile($temp_directory . $file);
// now delete this CSV file
if(is_file($temp_directory . $file)) {
unlink($temp_directory . $file);
}
}
$zip->close();

How to display all photos in a folder in a folder? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have stumbled upon a problem while developing a simple website. I wanted to know how could I access and display all images stored in a folder inside another folder ?
Thanks for the input! =)
you could use scandir() function to read all files in a folder then filter that files and get only images from theme
<?php
$folder = 'images';
$files = scandir($folder);
$types = array('jpg','png','gif');
foreach($files as $file) {
$exp = explode('.', $file);
$ex = end($exp);
if(in_array($ex , $types)) {
echo '<img src="'. $folder .'/'.$file.'" />';
}
}
?>
or use glob() to read only images in a dir the line of code below gets all .jpg images
$files = glob("images/*.jpg");

Checking if data equal one of my file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a folder in my website, is it possible to check if a file exist for comparing with a data query ?
I mean if $donnees["DATA"] == one of the files of the folder.
I don't know any function or something about that.
For instance if Data = 25478
In my folder :
Differents name of file :
24788
24777
25478
$listOfFiles = glob("*");
if(in_array($donnees["DATA"], $listOfFiles) {
//do stuff
}}
You can use glob("*.txt") for .txt files etc etc.
Yes, you can use glob function:
//path to directory to scan
$directory = "somefolder/";
//get all folders/files in specified directory
$files = glob($directory . "*");
//get ach directory/file name
foreach($files as $file)
{
if($donnees["DATA"]==$file){
//do something
}
}

How to assign a value to a variable from a text file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using a script to back up files from ftp. code is below.
include "recurseZip.php";
//Source file or directory to be compressed.
$src='source/images/black.png';
//Destination folder where we create Zip file.
$dst='backup';
$z=new recurseZip();
echo $z->compress($src,$dst);
Now I want to get values to $src from source/files.txt which contains a list of file names.
My .txt file:
index.php.bk-2013-12-02
index.php.bk-2013-12-07
index.php.bk-2013-12-10
index.php.bk-2013-12-20
index.php.bk-2013-12-26
function.php.bk-2013-12-20
function.php.bk-2013-12-23
contact.php.bk-2013-12-23
contact.php.bk-2013-12-30
my source/files.txt contains 10 file names those need to be assigned as values to the variable $src I am using this script http://ramui.com/articles/php-zip-files-and-directory.html
how can I do that.?
any help will be very much appreciated.
Thanks.
Okay, you want to get the file name from each line of the .txt file.
<?php
$myFile = "files.txt";
$lines = file($myFile);
foreach($lines as $line){
$file = basename($line);
echo $file;
}
?>
Answer to your old question variant
You can use the basename() function. The manual says, "given a string containing the path to a file or directory, this function will return the trailing name component".
Now, you said "I want to get file name to $src from source/files.txt", so assuming from this, you are looking to get the file name i.e. black.png. This could be achieved using the basename() function as mentioned before.
<?php
$src='source/images/black.png';
$file = basename($src);
echo $file;
?>
Output
black.png
http://ideone.com/p2b4sr

Categories