>假设帖子请求经过验证,并且功能代码足够好,只是一个观察,主要是多个文件上传 代码点火器 示例代码如下所示,
令人担忧的是为什么像 $_FILES 这样的超级全局变量会被覆盖?这是一个好的做法吗???
$filesCount = count($_FILES['files']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
// File upload configuration
$uploadPath = 'uploads/files/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if($this->upload->do_upload('file')){
// Uploaded file data
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s");
}
}
实际上,do_upload()
函数只接受表单名称。
默认情况下,上传例程要求文件来自您案例中名为files
的表单字段,并且表单的类型必须为multipart
(enctype="multipart/form-data"(。
这是我们覆盖$_FILES
变量的主要原因。
CodeIgniter 中的文件上传类