Author Topic: Browse to file and read contents  (Read 274 times)

0 Members and 1 Guest are viewing this topic.

Offline candycanesTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Browse to file and read contents
« on: July 30, 2010, 06:26:24 AM »
Hi All, sure this is simple but I'm still new to php/mySQL so errors are abundant!

I have a simple form with one input: a browse button so the user can find a .csv file on their computer.  User then clicks button, upon which I would like the file to be read and the contents put into an array, and the values to be added to the database. I do not need the file to be stored (uploaded).

So far my code is not erroring, it just doesn't add the values to the db.  I'm wondering if it's just that I HAVE to upload and save the file to the webserver else I can't read it?

if(isset($_FILES['file_path']['tmp_name'])&& $_FILES['file_path']['tmp_name'] !=''){

	
$file_to_import $_FILES['file_path']['tmp_name'];
	
$import_data file($file_to_import);
	
print_r($import_data);
	
if (
$import_data !=''){
	
	
for(
$index 1$index sizeof($import_data); $index++){
	
	
	
$order_data explode(",",$import_data[$index]);
	
	
	
tep_db_query("UPDATE orders SET act_shipping_cost = '" $order_data[8]. "' WHERE orders_id = '"$order_data[0]."'");
	
	
	
echo 
"done it";
	

	
	
}
	
}else{
	
	
echo 
'error cant read file!';
	
}



} else{ 
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">

etc etc etc

<form id="shipping_costs_upload" name="csv_file_reader" method="post" action="shipping_cost_upload.php"> 
	
	
<div class="main" id="info_text"><h2>Upload Actual Shipping Costs CSV</h2><p>To upload actual shipping cost data, browse to your csv and click the Upload Data button.<br /><br /></p>
   
	
	
<div class="date" id="file">
     
	
	
<strong>CSV File:  </strong><input type="file" name="file_path" id="file_url" />
   
	
 
	
</div>
   
	
 
	
<div class="date"><input type="submit" value="Upload Data">
   
	
 
	
</div>
   
	
 
	
</form>

etc etc etc


Apologies if I'm being a complete dunce!

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Browse to file and read contents
« Reply #1 on: July 30, 2010, 06:32:52 AM »
Quote
I'm wondering if it's just that I HAVE to upload and save the file to the webserver else I can't read it?

of course you need to upload it first. Reading files on a client would be a massive security hole.

Offline candycanesTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Browse to file and read contents
« Reply #2 on: July 30, 2010, 06:41:44 AM »
Quote
I'm wondering if it's just that I HAVE to upload and save the file to the webserver else I can't read it?

of course you need to upload it first. Reading files on a client would be a massive security hole.

In that case, what exactly is wrong in my code?  As I thought $_FILES['file_path']['tmp_name'] was the file's temporary position on the server, and I would be able to read this file?