我想使用HTML输入类型文件上传文件,但它不起作用。该文件未上载到预期的文件夹和数据库(我想将文件名存储到表中)。当我从浏览器检查时,它没有出现错误。当我点击“上传”按钮时,网址就像http://localhost/mywebsite/ajax/upload? ,方法GET,状态代码200 OK。我做错了什么?
我正在使用mac,xampp,phpmyadmin,chrome。
以下是我的观点:
<form action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
<input type="file">
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-upload" aria-hidden="true" ></span> Upload
</button>
</form>
控制器:
function upload()
{
if (!empty($_FILES))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.'));
$file_ext = substr($filename, strripos($filename, '.'));
$tempFile = $_FILES['file']['tmp_name'];
$data = "admin".uniqid().$file_ext;
$targetPath = getcwd() . '/kirim_undangan/';
$targetFile = $targetPath . $data ;
move_uploaded_file($tempFile, $targetFile);
$data_user = array(
'request_id' => $request_id,
'status' => '1',
'filename_admin' => $uploadedFileName,
);
$this->load->model('excel');
$this->excel->upload_excel($data_user);
}
}
模特:
function upload_excel($allDataInSheet,$request_id)
{
$regex = "~\d{5}~";
array_shift($allDataInSheet);
foreach ($allDataInSheet as $key)
{
preg_match($regex, $key['B'], $result);
$data = array(
'request_id' => $request_id,
'to_name' => $key['A'],
'to_phone' => $key['C'],
'to_address' => $key['B'],
'to_zipcode' => $result[0],
'tariff' => '0'
);
$this->db->insert('excel', $data);
$this->db->update('request',$request_id);
}
}
答案 0 :(得分:4)
您必须为输入添加名称
<form method="post" action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-upload" aria-hidden="true" ></span> Upload
</button>
</form>