Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/273462-copying-dynamic-file/
Share on other sites

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.

 

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

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.

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.

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++) {
     //...
   }
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.