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();
Related
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.
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");
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
}
}
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to merge multiple CSV files in to one csv files in PHP or joomla?
Merge all data from the csv files in a folder into a text file
with a few small changes you can also use this for txt files. Replace *.csv for *.txt
To do this in PHP you would do something in the lines of:
Open a file handle where the merged csv data can be written to
Read all the filenames from the source dir
For every file that ends with ".csv", append its content to the merge file
Ex.
$csvdir = './csvdir';
$result = fopen('./merge.csv', 'w');
if ($handle = opendir($csvdir)) {
while (false !== ($entry = readdir($handle))) {
if (substr($entry, -4) === ".csv") {
$csvcontent = file_get_contents($entry);
fwrite($result, $csvcontent);
}
}
closedir($handle);
}
fclose($result);
Merge all data from the csv files in a folder into a text file
Note: with a few small changes you can also use this for txt files. Replace *.csv for *.txt
1) Windows Start Button | Run
2) Type cmd and hit enter ("command" in Win 98)
3) Go to the folder with the CSV files (for help how to do that enter "help cd")
4) Type copy *.csv all.txt and hit enter to copy all data in the files into all.txt.
5) Type exit and hit enter to close the DOS window
Now we must import the text file all.txt into Excel.
1) Open Excel
2) When you use File Open to open all.txt the Text Import Wizard will help you import the file
3) Choose Delimited
4) Next
5) Check Comma
6) Finish