Wilest
Wilest

Reputation: 1860

php uploading a csv file into a mysql table

I'm trying to create a script where a user can upload a csv file into a table. I got the tutorial from shotdev.com, I'm getting the following errors, what am I doing wrong?

**The following command is not allowed: copy

The following command is not allowed: fopen**

Page1.php

<form action="page2.php" method="post" enctype="multipart/form-data" name="form1">
      <input name="fileCSV" type="file" id="fileCSV">
      <input name="btnSubmit" type="submit" id="btnSubmit" value="Submit">
    </form>

Page2.php

<?
    copy($_FILES["fileCSV"]["tmp_name"],"shotdev/".$_FILES["fileCSV"]["name"]); // Copy/Upload CSV

include 'datalogin.php';

    $objCSV = fopen("shotdev/".$_FILES["fileCSV"]["name"], "r");
    while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
        $strSQL = "INSERT INTO customer ";
        $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
        $strSQL .="VALUES ";
        $strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
        $strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
        $objQuery = mysql_query($strSQL);
    }
    fclose($objCSV);

    echo "Import completed.";
?>

Upvotes: 0

Views: 249

Answers (2)

Pekka
Pekka

Reputation: 449475

This seems to be a limitation imposed by the hosting provider (they seem to have put fopen() and copy() on the list of disabled functions, which is rather silly), in which case you probably can't do anything about it except ask the provider to relax the restriction.

Generally though, you're not handling the uploaded files properly using move_uploaded_file(). Use it as shown in the example in the manual link.

Upvotes: 0

Tak
Tak

Reputation: 11704

It looks like you're running PHP in safe mode or with certain functions disabled. For security reasons web hosts often disable file commands.

You could discuss your requirements with your web host - some are flexible enough to enable the functions for you if you ask nicely.

Otherwise you'll have to achieve it without using the disabled file commands. One way would be to create a textarea in an HTML form in which you paste the CSV. SUbmit it to your PHP script which then runs the MySQL import.

Hope that helps

Upvotes: 1

Related Questions