rseigel Posted January 21, 2013 Share Posted January 21, 2013 This one has me stumped. I need to copy a file from another server (one of my suppliers) to mine. Problem is the URL lloks like this: http://www.mysupplier.com/utilities/cgen.lasso?an8=10012538&fmt=csv which creates a file called GeneratedList.csv. I'd rather not bother reading it into a table and then parsing and saving it. Just a direct copy would be ideal. The rest of the script knows what to do with it once it's there. Any and all help appreciated. Cheers, Ron Quote Link to comment Share on other sites More sharing options...
kicken Posted January 21, 2013 Share Posted January 21, 2013 There is nothing special about a URL with parameters in this case. You'd just do something like: copy('http://www.mysupplier.com/utilities/cgen.lasso?an8=10012538&fmt=csv', 'GeneratedList.csv'); and PHP will download it and save the contents to the local file GeneratedList.csv. If your stuck with something else you'll need to try and explain better, I'm not sure what issue you are having. Quote Link to comment Share on other sites More sharing options...
rseigel Posted January 22, 2013 Author Share Posted January 22, 2013 That works perfectly. Thanks! The problem I'm running into now is that the first row is the column names which need to be skipped. What would I add to this code to get that to happen? $handle = fopen("GeneratedList.csv", "r+"); while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { if ($c = 1) { $supplier_reference = $data[($c-1)]; } if ($c = 2) { $quantity = $data[($c-1)]; mysql_query("UPDATE stock_available, product SET stock_available.quantity = $quantity WHERE stock_available.id_product = product.id_product AND product.supplier_reference = '$supplier_reference'") or die(mysql_error()); mysql_query("UPDATE stock_available, product_attribute SET stock_available.quantity = $quantity WHERE stock_available.id_product_attribute = product_attribute.id_product_attribute AND product_attribute.supplier_reference = '$supplier_reference'") or die(mysql_error()); } if ($c = 3) { } if ($c = 4) { } } } fclose($handle); Thanks again. Ron Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2013 Share Posted January 22, 2013 There's two ways, one is you could set a variable to true before entering the loop, and in the while loop check to see if it's true. If it is, skip the row and set that variable to false. Then from then on it will be false and not skip the row. Probably the simpler way. Quote Link to comment Share on other sites More sharing options...
rseigel Posted January 22, 2013 Author Share Posted January 22, 2013 Worked perfectly: $handle = fopen("GeneratedList.csv", "r+"); $header = 'true'; while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { if ($header = 'true') { $header = 'false'; break; } $num = count($data); for ($c=0; $c < $num; $c++) { Thanks a million. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2013 Share Posted January 22, 2013 Good job interpreting that logic and writing the code. Quote Link to comment Share on other sites More sharing options...
kicken Posted January 22, 2013 Share Posted January 22, 2013 That code is not quite correct. Your if condition needs to use == to compare, not =. Also you should use real boolean values rather than the strings 'true' and 'false'. Lastly you'd want to use continue;, not break; since break would exit the loop entirely rather than just skip one iteration. $handle = fopen("GeneratedList.csv", "r+"); $header = true; while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { if ($header == true) { $header = false; continue; } $num = count($data); for ($c=0; $c < $num; $c++) { //... } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2013 Share Posted January 23, 2013 Thanks Kicken, I didn't look too closely at it because he said it worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.