php导入、导出csv

导入和导出注意编码的问题,不行的话就转码。

导出(参考travelhk_lbs、arte):

$filename = "report.csv";
$fs = fopen("../../upload/".$filename, "w");

// header
$header = array("style_code","product_type","status","collection","collection_description","product_name","product_description","color","selling_price_HKD","selling_price_RMB","selling_price_TWD","selling_price_SGD","selling_price_AED","selling_price_EUR","small_image_name","large_image_name");

fputcsv($fs, $header);

// data
$query = RunQuery("select * from `product` where ".$_SESSION['product_where_clause']);

while ($record = mysql_fetch_assoc($query))
{
	$data = array();
	array_push($data, $record['style_code']);
	array_push($data, $record['type']);
	array_push($data, $record['status']);
	array_push($data, $record['collection']);
	array_push($data, $record['description_collection']);
	array_push($data, $record['name']);
	array_push($data, $record['description_product']);
	array_push($data, $record['color']);	
	array_push($data, $record['price_hkd']);
	array_push($data, $record['price_rmb']);
	array_push($data, $record['price_twd']);
	array_push($data, $record['price_sgd']);	
	array_push($data, $record['price_aed']);
	array_push($data, $record['price_eur']);
	array_push($data, $record['file_small']);
	array_push($data, $record['file_large']);
	
	fputcsv($fs, $data);
}
mysql_free_result($query);

fclose($fs);

header("Content-Type: text/csv");
header("Content-Disposition: attachment;filename=report.csv");

$fs = fopen("../../upload/".$filename, "r");
while (!feof($fs))
{
	//echo iconv("UTF-8", "BIG5//TRANSLIT", fread($fs, 8192));
	echo iconv("UTF-8", "gbk//IGNORE", fread($fs, 8192));
}
fclose($fs);

exit;

导入:

<?php

/**
* Require Core File
*/
require_once("../../common/initialize.php");

/**
* Form Submit
*/
if (@$_POST["formstatus"] == "submit")
{
	@mkdir($DOCUMENT_ROOT."upload/product/", 0777, true);
	$location = $DOCUMENT_ROOT."upload/product/";
	
	if (is_uploaded_file($_FILES['file_shop']['tmp_name']) && !$_FILES['file_shop']['name'] == "")
	{
		// save feed file
		$temp = explode(".", $_FILES['file_shop']['name']);
		$ext = $temp[1];
		if ($ext != "csv")
		{
			Redirect("import.php", "Invalid file type.");
		}
		$filename = "file_shop_".date("YmdHis`").".".$ext;
		@unlink($location.$filename);
		copy($_FILES['file_shop']['tmp_name'], $location.$filename);
		
		// parse feed file
		$row = 1;
		if (($handle = fopen($location.$filename, "r")) !== FALSE)
		{
			while (($data = fgetcsv($handle)) !== FALSE)
			{
				if ($row > 1)		// skip header row
				{
				    /* echo "<pre>";
					print_r($data);
					echo "</pre>";
					exit; */
					array("style_code","product_type","status","collection","collection_description","product_name","product_description","color","selling_price_HKD","selling_price_RMB","selling_price_TWD","selling_price_SGD","selling_price_AED","selling_price_EUR","small_image_name","large_image_name");
					$style_code = $data[0];
					$product_type = $data[1];
					$status = $data[2];
					$collection = $data[3];
					$collection_description = iconv("gbk", "UTF-8//IGNORE", $data[4]);
					$product_name = $data[5];
					
					$product_description = iconv("gbk", "UTF-8//IGNORE", $data[6]);
					$color = $data[7];
					$price_hkd = $data[8];
					$price_rmb = $data[9];
					$price_twd = $data[10];
					$price_sgd = $data[11];
					$price_aed = $data[12];
					$price_eur = $data[13];
					
					RunQuery("insert into `product` (`id`, `style_code`, `type`, `status`, `collection`, `description_collection`, `name`, `description_product`, `color`, `quantity`, `price_hkd`, `price_rmb`, `price_twd`, `price_sgd`, `price_aed`, `price_eur`, `file_small`, `file_large`, `file_specification`, `create_user`, `create_date`, `update_user`, `update_date`) values ('0', '".addslashes($style_code)."', '".addslashes($product_type)."', '".addslashes($status)."', '".addslashes($collection)."', '".addslashes($collection_description)."', '".addslashes($product_name)."', '".addslashes($product_description)."', '".addslashes($color)."', '', '".addslashes($price_hkd)."', '".addslashes($price_rmb)."', '".addslashes($price_twd)."', '".addslashes($price_sgd)."', '".addslashes($price_aed)."', '".addslashes($price_eur)."', '', '', '', '".addslashes($_SESSION['user']['id'])."', '".time()."', '".addslashes($_SESSION['user']['id'])."', '".time()."')");				
				}
				
				$row++;
			}
			fclose($handle);
		}
		
		
		// redirect
		if ($row - 1 > 0)
			Redirect("product_list.php", "Import ".($row - 2)." products successfully.");
		else
			Redirect("import.php", "No product found. Please check the feed format.");
	}
	
	Redirect("import.php", "No file uploaded.");
}


/**
* Initialization
*/
include("../../common/fckeditor/fckeditor.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ECCMS Ver1.0 (Powered by Econnective)</title>
<script language="javascript" src="../../common/CalendarPopup.js"></script>
<script language="javascript">

function ValidateForm()
{
	return true;
}

</script>
<link href="../../common/admin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
	margin-left: 5px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style></head>
<body bgcolor="#ffffff">
<form id="form" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>" method="post" enctype="multipart/form-data" onsubmit="javascript:return ValidateForm()">
<table width="100%" border="0" cellpadding="2" cellspacing="0" align="left">
	<tr>
		<td class="table_header" colspan="2">Import record</td>
	</tr>
	<tr>
		<td width="25%" class="table_subheader">Product:</td>
		<td width="75%" class="table_content">
			<input type="file" name="file_shop" id="file_shop" class="content" size="50" />
			<br />
			<font color="#CC0000">(.csv format encoding. Please click <a href="report.csv" taget="_blank">here</a> to download sample file.)</font>
		</td>
	</tr>
	<tr>
		<td class="table_subheader"> </td>
		<td class="table_content">
			<input type="submit" name="submit" id="submit" class="admin_button" value="Import" />
			<input type="reset" name="reset" id="reset" class="admin_button" value="Cancel" />
		</td>
	</tr>
</table>
<input type="hidden" name="formstatus" value="submit" />
</form>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值