Php 上传CSV文件并使用Laravel将其导入数据库

Php 上传CSV文件并使用Laravel将其导入数据库,php,pdo,laravel-4,Php,Pdo,Laravel 4,我有一种上传CSV格式文件的方法,但现在我想知道如何将其上传到数据库中的列中 我的方法: public function postUpload () { if (Input::hasFile('file')){ $file = Input::file('file'); $name = time() . '-' . $file->getClientOriginalName(); // Moves file to folder on

我有一种上传CSV格式文件的方法,但现在我想知道如何将其上传到数据库中的列中

我的方法:

 public function postUpload ()
{
    if (Input::hasFile('file')){

        $file = Input::file('file');
        $name = time() . '-' . $file->getClientOriginalName();
        // Moves file to folder on server
        $file->move(public_path() . '/uploads/CSV', $name);


        return 'Ok'; // return for testing

     }
}

因此,我的问题是在这个方法中,我如何才能将其放入数据库?

这个方法应该适合您,它使用PDO+mySql的“加载数据”方法

因此,结合您的代码,它可以是如下所示

public function postUpload ()
{
    if (Input::hasFile('file')){

        $file = Input::file('file');
        $name = time() . '-' . $file->getClientOriginalName();

        //check out the edit content on bottom of my answer for details on $storage
        $storage = '/some/world/readible/dir'; 
        $path = $storage . '/uploads/CSV';

        // Moves file to folder on server
        $file->move($path, $name);

        // Import the moved file to DB and return OK if there were rows affected
        return ( $this->_import_csv($path, $name) ? 'OK' : 'No rows affected' );            

     }
}
编辑 需要注意的是,根据您在评论中报告的错误,可能是一些权限问题(操作系统错误代码13:权限被拒绝)

请参阅:

“出于安全原因,在读取服务器上的文本文件时, 文件必须位于数据库目录中或可读 另外,要在服务器文件上使用加载数据填充,您必须 文件权限。请参阅第5.7.3节“由提供的权限” MySQL”

正如mySql bug tracker()上所报告的,您似乎需要对csv文件路径中的所有文件夹具有特定权限:

我认为,infle的所有父目录都需要世界可读性 以及目录和填充

因此,对于此处的填充:/tmp/imports/site1/data.file

你需要(我想,755个)r+x来表示这些“其他” 目录:/tmp/tmp/imports

以及主要的两个:/tmp/imports/site1 /tmp/imports/site1/data.file

总而言之:

要解决“sqlstate hy000常规错误13无法获取…的状态”问题,您必须将上载的文件移动到具有适当权限的位置(因此不一定是您当前使用的文件),请尝试类似“/tmp/import”的操作

虽然加载数据填充是最快的方法,但出于两个原因,我更喜欢使用类库or

  • 它是可扩展的,如果您的数据源更改为excel文件或mongo db或其他方法,该怎么办

  • 它是可伸缩的,如果您需要转换日期、字符串或数字,您可以有条件地进行转换,而这是批处理命令无法完成的


  • 我的2c

    我正在获取SQLSTATE[HY000]:一般错误:13无法获取我的上传路径的统计数据,我已将所有与此相关的文件夹设置为755,因此这应该可以正常工作,如果是+1,并接受答案,以便其他人将来可以轻松找到它。干杯。检查一下对初始代码的另一个小编辑-它现在应该可以工作了。我的问题是,它对您正在转储到数据库中的数据提供了最小或没有验证,如果此函数对一般用户输入开放,这将非常糟糕。这实际上只适用于知道自己在做什么的可信用户的一次性导入。但是OP没有指定任何一种方式,所以我将保留它。值得注意的是,
    'files'=>true
    作为表单属性是必需的(例如,
    {form::open(array('class'=>'create user','role'=>'form','files'=>true))}
    public function postUpload ()
    {
        if (Input::hasFile('file')){
    
            $file = Input::file('file');
            $name = time() . '-' . $file->getClientOriginalName();
    
            //check out the edit content on bottom of my answer for details on $storage
            $storage = '/some/world/readible/dir'; 
            $path = $storage . '/uploads/CSV';
    
            // Moves file to folder on server
            $file->move($path, $name);
    
            // Import the moved file to DB and return OK if there were rows affected
            return ( $this->_import_csv($path, $name) ? 'OK' : 'No rows affected' );            
    
         }
    }