【发布时间】:2014-08-09 19:26:51
【问题描述】:
假设一个文件会自动更新产品或客户,现在我们需要一个脚本来将文件上传到 magento 并每天运行。我有要上传的代码,但我有两个问题。
- 我们如何将此脚本设置为自动运行?...Cron?
-
有比这个脚本更好的方法吗?
if ($_FILES[csv][size] > 0) { // the table fields $tbl_fields = array('customerCode', 'postCode,Name', 'Address1', 'Address2'); //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); // get the first line into a fields map $csv_fields = fgetcsv($handle,1000,",",'"'); // we will insert only common fields $tbl_fields = array_intersect($tbl_fields,$csv_fields); // if there's not at least one common field, don't go on if(count($tbl_fields)>0) { // we need the table's field names as keys (see below) $tbl_fields = array_flip($tbl_fields); // now let's go after the data while($data = fgetcsv($handle,1000,",",'"')) { $data=array_map('addslashes',$data); // apply addslashes() to all values $data=array_combine($csv_fields,$data); // csv fields assoc (key=>value) $data=array_intersect_key($data,$tbl_fields); // discard redundant $tbl_fields_str=implode("`,`",array_keys($data)); $tbl_vals_str=implode("','",array_values($data)); $q="INSERT INTO `PowerFlex` (`$tbl_fields_str`) VALUES ('$tbl_vals_str')"; mysql_query($q); } } }
【问题讨论】: