我有两个CSV: InputCSV和OutputCSV
InputCSV.csv
name col1 col2 col3 col4 col5
john qOJY OHXl vIOH Tdnm Z7OH
greg YRc1 hyFB pW8m 5LSE Yo4r
saly Dy4o 51Ui tuKI 02VQ RVgB
OutputCSV.csv
name url
john site.com/JcRIeyFCEl.mp4
greg site.com/TTwF4Cue2B.mp4
saly site.com/ouroANTtAC.mp4
在processor。php中写入
if (($csvFile = fopen("InputCSV.csv", "r")) !== false && ($resultCsv = fopen("OutputCSV.csv", 'w')) !== false) {
while (($data = fgetcsv($csvFile)) !== false) {
// do stuff to generate $thumbfile
// this bit needs to use parts of $data like $data[3], $data[5] to generate $thumbfile
// which in the end should look like $thumbfile = 'site.com/rAndOmsTRing.jpg';
}
$outputData = fgetcsv($resultCsv);
$outputData[] .= $thumbfile_url; //Append to end of line. is this what I'm doing wrong?
fputcsv($resultCsv, $outputData);
我需要做的是向OutputCSV.csv添加一个新列,并在每行/行末尾附加$thumbfile。目前,它所做的是删除现有的两个列,并只写入$thumbfile作为一个列。
OutputCSV.csv需要在脚本运行后看起来像这样:OutputCSV.csv
name url thumbfile
john site.com/JcRIeyFCEl.mp4 site.com/snTflxaqNI.jpg
greg site.com/TTwF4Cue2B.mp4 site.com/ELrg6vwKEr.jpg
saly site.com/ouroANTtAC.mp4 site.com/xTjfqCEoIZ.jpg
注意:我使用的实际csv文件没有标题。我把它们放在那里只是为了方便阅读。
要制作所需的内容,您需要将OutputCSV.csv的数据保存在数组中,然后在同一数组中添加$thumbfile。
这里是一个例子,我使用您的CSV文件以","分隔。
if (($csvFile = fopen("InputCSV.csv", "r")) !== false && ($resultCsv = fopen("OutputCSV.csv", 'r')) !== false) {
while (($data = fgetcsv($csvFile,4096,",")) !== false) {
// do stuff to generate $thumbfile
$thumbfile[]=$data[3].$data[5].".jpg";
}
$x=0;
while (($dataoutput = fgetcsv($resultCsv,4096,",")) !== false) {
$dataotputcsv[$x]=$dataoutput; //here is [0]=> "john" [1]=> "site.com/JcRIeyFCEl.mp4"
array_push($dataotputcsv[$x], $thumbfile[$x]); //here i add the $thumbfile
//and now is [0]=> "john" [1]=> "site.com/JcRIeyFCEl.mp4" [2]=> "vIOHZ7OH.jpg"
$x++;
}
$file = fopen("OutputCSV.csv", 'w');
foreach($dataotputcsv as $string) {
fputcsv($file, $string); //save all lines to file
}
}