简体   繁体   English

PHP将CSV文件上传到MySQL表中

[英]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. 我正在尝试创建一个脚本,用户可以在其中将csv文件上传到表中。 I got the tutorial from shotdev.com , I'm getting the following errors, what am I doing wrong? 我从shotdev.com获得了教程, 出现以下错误,我在做什么错?

* *The following command is not allowed: copy * *不允许以下命令:复制

The following command is not allowed: fopen** 不允许使用以下命令:fopen **

Page1.php 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 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.";
?>

It looks like you're running PHP in safe mode or with certain functions disabled. 看起来您在安全模式下运行PHP或禁用了某些功能。 For security reasons web hosts often disable file commands. 出于安全原因,Web主机通常禁用文件命令。

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. 一种方法是在HTML表单中创建一个textarea ,在其中粘贴CSV。 SUbmit it to your PHP script which then runs the MySQL import. 将其提交到您的PHP脚本中,然后运行MySQL导入。

Hope that helps 希望能有所帮助

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. 这似乎是托管服务提供商强加的限制(他们似乎已将fopen()copy()放在禁用函数列表中,这很愚蠢),在这种情况下,您可能无能为力要求提供者放宽限制。

Generally though, you're not handling the uploaded files properly using move_uploaded_file() . 不过,通常来说,您无法使用move_uploaded_file()正确处理上传的文件。 Use it as shown in the example in the manual link. 如手册链接中的示例所示使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM