why this load data infile doesn't work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnny

    why this load data infile doesn't work?

    hi all,

    I wonder why this little script doesn't work, maybe it's the provider
    not allowing the use of load data infile ( I know some don't let users
    to run some tasks ), could you please tell me if the script it's right
    before I ask the provider ?
    I can insert data into the table using fgetcsv as well with no
    problems,but I would like to understand.

    TIA

    johnny

    <?php

    $table = "name of table here";
    $file = "absolute path to file uploaded /" ;
    $new_name = "list.csv"; // new name of the file uploaded


    $file_name = ($new_name) ? $new_name : $_FILES["upfile"]["name"];


    $path = $file.$file_nam e ;


    if (copy($upfile, $path))
    {
    echo "upload Ok";

    } else {
    echo "upload failed";
    }


    $sql = "LOAD DATA INFILE '$path' FIELDS INTO TABLE $table
    (field1,field2) TERMINATED BY ';' LINES TERMINATED BY '\r\n' IGNORE 1
    LINES " or die();

    ?>

  • Erwin Moller

    #2
    Re: why this load data infile doesn't work?

    johnny wrote:
    [color=blue]
    > hi all,
    >
    > I wonder why this little script doesn't work, maybe it's the provider
    > not allowing the use of load data infile ( I know some don't let users
    > to run some tasks ), could you please tell me if the script it's right
    > before I ask the provider ?
    > I can insert data into the table using fgetcsv as well with no
    > problems,but I would like to understand.
    >
    > TIA
    >
    > johnny[/color]

    Hi Johnny,
    [color=blue]
    >
    > <?php
    >
    > $table = "name of table here";
    > $file = "absolute path to file uploaded /" ;
    > $new_name = "list.csv"; // new name of the file uploaded
    >
    >
    > $file_name = ($new_name) ? $new_name : $_FILES["upfile"]["name"];[/color]

    I do not understand the reason for this line/test.
    It always evalute to true, so $file_name will always be $new_name.

    Did you copy it from another file that needs this test?

    [color=blue]
    >
    >
    > $path = $file.$file_nam e ;[/color]

    OK, so path is now:absolute path to file uploaded/list.csv

    [color=blue]
    >
    >
    > if (copy($upfile, $path))[/color]

    What is $upfile?
    Udefined as far as I can see.
    [color=blue]
    > {
    > echo "upload Ok";
    >
    > } else {
    > echo "upload failed";
    > }
    >
    >
    > $sql = "LOAD DATA INFILE '$path' FIELDS INTO TABLE $table
    > (field1,field2) TERMINATED BY ';' LINES TERMINATED BY '\r\n' IGNORE 1
    > LINES " or die();[/color]

    You just create a string here.
    Why add or die(); ???

    I would advise you to put a few echo's in your script to check if the values
    you use contain what you expect them to contain.
    For starters: The name of an uploaded file in the tempdirectory is not the
    filename specified by the sender.

    Regards,
    Erwin Moller

    Comment

    • Al

      #3
      Re: why this load data infile doesn't work?

      Erwin Moller wrote:[color=blue]
      > Hi Johnny,
      >[color=green]
      > >
      > > <?php
      > >
      > > $table = "name of table here";
      > > $file = "absolute path to file uploaded /" ;
      > > $new_name = "list.csv"; // new name of the file uploaded
      > >
      > >
      > > $file_name = ($new_name) ? $new_name : $_FILES["upfile"]["name"];[/color]
      >
      > I do not understand the reason for this line/test.
      > It always evalute to true, so $file_name will always be $new_name.
      >
      > Did you copy it from another file that needs this test?
      >
      >[color=green]
      > >
      > >
      > > $path = $file.$file_nam e ;[/color]
      >
      > OK, so path is now:absolute path to file uploaded/list.csv
      >
      >[color=green]
      > >
      > >
      > > if (copy($upfile, $path))[/color]
      >
      > What is $upfile?
      > Udefined as far as I can see.
      >[color=green]
      > > {
      > > echo "upload Ok";
      > >
      > > } else {
      > > echo "upload failed";
      > > }
      > >
      > >
      > > $sql = "LOAD DATA INFILE '$path' FIELDS INTO TABLE $table
      > > (field1,field2) TERMINATED BY ';' LINES TERMINATED BY '\r\n' IGNORE 1
      > > LINES " or die();[/color]
      >
      > You just create a string here.
      > Why add or die(); ???
      >
      > I would advise you to put a few echo's in your script to check if the values
      > you use contain what you expect them to contain.
      > For starters: The name of an uploaded file in the tempdirectory is not the
      > filename specified by the sender.
      >
      > Regards,
      > Erwin Moller[/color]


      Yeah, basically I don't think your filecopy is actually working.
      Assuming you're relying on $upfile to be automatically globaled from
      the $_FILES upload, then I still don't think just using copy() works
      like that. You need to give copy() the filename, not the array
      containing the file's details (which $upfile is if register_global s is
      on).

      Also, as Erwin said, you never actually execute an SQL query, you just
      make the string.

      You need something more like:

      $sql = "LOAD DATA INFILE '$path' FIELDS INTO TABLE $table
      (field1,field2) TERMINATED BY ';' LINES TERMINATED BY '\r\n' IGNORE 1
      LINES";

      $result = mysql_query($sq l) or die("Query failed");

      See http://uk.php.net/manual/en/function.mysql-query.php for examples
      on executing SQL statements (and slightly better ways of dealing with
      query failure.)

      Comment

      Working...