0

Hi, I have been trying to import the data from a CSV file (10,000 rows) into a database using SQL and PHP

<body>
<div id="container">
<div id="form">
<?php

$user="root";
$database="test";

mysql_connect("localhost",$user);
@mysql_select_db($database) or die( "Unable to select database");

//Upload File
if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    }

$query = <<<eof
    LOAD DATA INFILE '$filename' <<<<<<<<<<ERROR HERE
     INTO TABLE importing
     FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
    (text,number)
    IGNORE 1 LINES
eof;

mysql_query($query);


print "Import done";

    //view upload form
}else {

    print "Upload new csv by browsing to file and clicking on Upload<br />\n";

    print "<form enctype='multipart/form-data' action='upload.php' method='post'>";

    print "File name to import:<br />\n";

    print "<input size='50' type='file' name='filename'><br />\n";

    print "<input type='submit' name='submit' value='Upload'></form>";

}
?>
</div>
</div>
</body>
</html>

the only error being returned is "Notice: Undefined variable: filename" and I can't see how to fix it no matter how much I look...
any help would be appreciated, I think i have been staring at code too long today lol

Edited by Pidgetastic

3
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by akmozo
0

You get that because $filename is undefined, it has no value set. Somewhere you need to have:

$filename = ''; // not an empty string of course, but pointing to the uploaded file

Edited by pritaeas

0

Hi,

I think that you should add

$filename = $_FILES['filename']['name'];

after

$query = <<<eof
0

but wont the form be defining it? i tried various different things. inluding trying to retrieve it with POST but had no luck

0

@akmozo

it just throws up "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" if i do that

0

The file will be uploaded to a temporary location, and you have to use move_uploaded_file to put it somewhere permanent. The filename you use there, you can use to import.

UPDATE: Just found this link on the MySQL forum.

Edited by pritaeas

0

The file will be uploaded to a temporary location, and you have to use move_uploaded_file to put it somewhere permanent. The filename you use there, you can use to import.

does it need a location to move to, because i'm not moving the file at all?
if not, something like:
move_uploaded_file ($filename) would work?
and where would it be poisitoned in the code?

Thanks

0

When we upload a file, the server will put it in a temporary directory (like /server/web_server/temp ) so it's not accessible from the web ( for the visitors of your site ), that's why we should move it in another directory which is accessible.

Try this at the beginnig of upload.php ( before the query ):

    $temp_file = $_FILES['Filedata']['tmp_name']; 

    $origin_file = $_FILES['Filedata']['name'];

    $target_file = '/uploads/'.$origin_file;

    move_uploaded_file($temp_file, $target_file);

    $file_name = $target_file;

Edited by akmozo

0

so does that mean that after the user has left the file would be left where I moved it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.