如何在 PHP 中将 CSV 文件写入并保存到服务器?

我正在尝试使用 PHP 创建一个新的 CSV 文件并将其上传或移动到服务器的新部分,但它返回的电子表格是一个电子表格,第一行中只有第一个单元格的值为 404 或 1。我究竟做错了什么?


我的代码附在下面。


// genrate new general spreadsheet

$filepath = substr($file_path, 1);

$data = load_csv_file($filepath);


header('Content-type: text/csv');

header('Content-Disposition: attachment; filename="file-saved.csv"');


$fp = fopen('php://output', 'wb');


foreach ($data as $row) {

$output = fputcsv($fp, $row);

}


$filename = "file-saved.csv";

file_put_contents( $filename, $output);


fclose($fp);

$data 变量是来自另一个 CSV 文件的值数组。


largeQ
浏览 391回答 2
2回答

holdtom

$output = [];foreach($data as $row) { $output[] = .. }...

慕桂英546537

&nbsp;error_reporting(0);&nbsp; $file_n = public_path('/csv_file/product_details.csv');&nbsp; $infoPath = pathinfo($file_n);&nbsp; if($infoPath['extension'] == 'csv'){&nbsp; &nbsp; &nbsp; $file = fopen($file_n, "r");&nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; $all_data = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( ($filedata = fgetcsv($file, null, "|")) !==FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $num = count($filedata );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($c=0; $c < $num; $c++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $all_data[$i][] = $filedata [$c];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($all_data as $importData){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $insertData = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_number"=>$importData[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_name"=>$importData[1],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_description"=>$importData[2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_price"=>$importData[3],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_manufacturer"=>$importData[6],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_productgroupkey"=>$importData[7],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_productgroup"=>$importData[8],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_ean"=>$importData[9],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_hbnr"=>$importData[10],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_shippingcosttext"=>$importData[11],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_amount"=>$importData[12],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_paymentinadvance"=>$importData[13],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_maxdeliveryamount"=>$importData[14],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_energyefficiencyclass"=>$importData[15]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertData($insertData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }else{&nbsp; &nbsp; echo "Invalid file extension.";&nbsp; }函数插入数据($数据){if($article_number->count() == 0){&nbsp; &nbsp; //write your insert query here for $data&nbsp; }elseif($article_number->count() > 0){&nbsp; &nbsp;//article_number already present then update the table.UPDATE QUERY&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP