CodeIgniter 3通过表单获取文件内容

时间:2018-02-23 14:20:15

标签: php codeigniter codeigniter-3

当我上传文件时,我想要读取它的内容,而不是实际将它放在我的服务器上。 该文件未上传到服务器。

我的PHP:

public function uploadFile()
{
    $this->view->postFile($this->input->post("thefile"));
}

public function postFile($file)
{
    $data = json_decode(file_get_contents($file), true);
}

但是当我提交表格时,它说

Message: file_get_contents(myfile.json): failed to open stream: No such file or directory

从其他地方读到,似乎你应该使用file_get_contents来解决这个问题,所以我很困惑。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

传递实际的文件路径+文件名而不仅仅是文件名

$acualFile = 'uploads/'.$file; # should set where acual file folder 
$data = json_decode(file_get_contents($acualFile), true);

答案 1 :(得分:1)

当您上传文件时,他们不会进入帖子部分。服务器将它们存储在名为$FILES的全局变量中。如果您只是想阅读内容,请访问其下面的字段:

   $file_id = 'thefile';
   echo $_FILES[$file_id]['name'];
   echo $_FILES[$file_id]['type'];
   echo $_FILES[$file_id]['tmp_name'];
   echo $_FILES[$file_id]['error'];
   echo $_FILES[$file_id]['size'];

有关上述每个字段的详细说明,请查看POST method upload