supposed there is a folder named example, and in it there are some csv file eg(a.csv, b.csv....).
the test.php directory is the same as example folder. now i want to pass all the csv file name to the following if condition. namely, replace test.csv with all the csv file name
if (($handle = fopen("test.csv", "r"))
how do i do?
i using the following code:
$files= glob("./example/*.csv");
if (($handle = fopen("$files", "r"))
but it doesn't work. thank you.
$files is an array, you need to loop with it.
$files = glob("./example/*.csv");
foreach($files as $filepath) {
if ($handle = fopen($filepath, "r")) {
// ...
}
}
Related
Hi I wonder if it is possible to match a string to a file from folder using php.
For example I have a folder called uploads and inside, I have different files like image1.png, image2.jpg, doc1.doc, and doc2.pdf.
Assuming I have this code on my php file:
<?php
$string = "image2";
// I need some function to display the image2 on my webpage.
// If string "image2" is found in the uploads folder
// then it should display the image
?>
Thanks!
I think this one should do what you want
$dir = "uploads";//the path to your folder
if(file_exists($dir)){
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file)) {
if ($file == "image2"){
// your code
}
}
}
}
}
I need to make CSV file upload for eshop. I have goods in CSV files. They have images defined for example: http://www.servername/pictures/pic1.jpg
I am finding any function, I need that script upload a CSV (solved), open CSV and explode by ",[coma]" (solved), the script go to the link with image [another server], download it and save it into directory at my server.
Colleges helped me, that I should use get_file_contents() function, but on php.net in manual I find another things. How can I solve this problem?
Please try this
if (($handle = fopen("upload/myCSV.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$imageUrl = $data[1];
$contents = file_get_contents(trim($imageUrl));
if ($contents) {
file_put_contents('/path/to/save/pic.jpg', $contents);
}
fclose($handle);
}
In myCSV.csv file data is
"1","http://imagesus.homeaway.com/mda01/5fe39690-1cbf-469d-8525-b946ad1f4ba7.1.10"
The basic procedure is:
// get the image data
$image = file_get_contents('http://www.servername/pictures/pic1.jpg');
// write the image data
$fp = fopen('path_to_your_image_folder/pic1.jpg', 'w'); //not URL
fwrite($fp, $image);
fclose($fp);
You will probably have to add some validation checks to check if the folder is writable, if there is content in $image and so on.
I want to create a command line php script which would merge/join multiple CSV files from a folder into one.
Each CSV file has 2 columns delimited by comma (,) but multiple number of rows varies. Also each of the CSV file name is unique so when we merge the CSV files I want the file name of the CSV to be the first column for each rows in the file.
So eventually when the script it run it’ll join multiple CSV files under a folder to one. From 2 columns the output file will have 3 columns where the first column would be the file name.
<?php
$nn = 0;
foreach (glob("*.csv") as $filename) {
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$c = count($data);
$csvarray[$nn][] = $filename;
for ($x=0;$x<$c;$x++)
{
$csvarray[$nn][] = $data[$x];
}
$nn++;
}
fclose($handle);
}
}
$fp = fopen('../file.csv', 'w');//output file set here
foreach ($csvarray as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
I didn't make any test on it though, here is the logic and code you can follow.
There are many CSV file like the following:a.csv, b.csv, aab.csv etc.
They hold the same column and header. Now I want to put all the csv data into whole.csv. With only one header. How can I do it?
a.csv data:
header1 title post.....
test who posand
b.csv data:
header1 title post.....
head she pnow
etc .....
The whole.csv will contain all the csv data.
eg:
header1 title post.....
head she pnow
test who posand
I tried the following code.but not get I want to:
$csvs = glob("*.csv");
foreach($csvs as $csv) {
$row = 1;
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$fp = fopen("whole.csv", 'w');
fputcsv($fp, $data);
$row++;
}
fclose($handle);
}
}
I have put all CSV files in the same directory.
For every input csv file you are opening the resultant csv file in write mode:
$fp = fopen("whole.csv", 'w');
which wipes the content of the whole.csv!!
You need to open the whole.csv file just once outside the loop and keep writing into it.
$csvs = glob("*.csv");
$fp = fopen("whole.csv", 'w');
foreach($csvs as $csv) {
$row = 1;
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
fputcsv($fp, $data);
$row++;
}
fclose($handle);
}
}
Have a look at file_put_contents.
You would open each CSV file, then use file_put_contents passing whole.csv as the $filename parameter, the file handle as the $data parameter and use the FILE_APPEND flag to tell it to append the contents instead of overwriting.
there are two columns in my csv file,eg:
image gallery
/1.jpg /a.jpg;/b.jpg
..... .....
now i want to update the gallery content to /1.jpg;/a.jpg;/b.jpg. namely,add the content of image collumn and ; to the gallery content.
the following is my code.when i run it. it can't update the content of the csv.i am get stucked.
$dir = getcwd();
$files = scandir($dir);
foreach ($files as $file) {
$parts = pathinfo($file);
if ($parts['extension']!="csv") {
continue;
}
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
$data[1]=$data[0].";".$data[1];
fputcsv($file, $data);
}
fclose($handle);
}
open file in write or append mode and
fputcsv expects first parameter to be resource and you have given file path
which is causing problem
change it
fputcsv($handle, $data);
Please check the file permission first and after that you have to change the handle to both read and write and also please check whether the data[1] is having the values.
Because in Your code the the data[0] only will fetch the lines as a string which is separated with ";" so you have to explode it and after that do the operations.