2 回答
TA贡献1802条经验 获得超4个赞
使用 $this->load->library('upload', $config); 加载库;当您只上传一个文件时有效。但是,当您上传多个文件时,您需要使用配置重新初始化库。
就像这样:
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('one'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('normal_upload', $error);
}
$data['one'] = array('upload_data' => $this->upload->data());
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('two'))
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('normal_upload', $error);
}
$data['two'] = array('upload_data' => $this->upload->data());
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('three'))
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('normal_upload', $error);
}
$data['three'] = array('upload_data' => $this->upload->data());
然后您可以使用以下命令访问文件数据:
$data['one']['upload_data']['full_path'];
$data['two']['upload_data']['full_path'];
$data['three']['upload_data']['full_path'];
TA贡献1806条经验 获得超5个赞
事实证明它并不是那么简单。下面的代码:
仅上传图像文件和 pdf,仅允许上传图像和 pdf
重命名上传的文件
删除上传文件中的所有空格
public function normal_upload(){
$uploadLocation = "uploads/";
$uploadMainTo = null;
if(isset($_FILES['MainImage'])){
$path = $_FILES['MainImage']['name']; // file means your input type file name
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {
echo "Upload successful";
}else{
// your invalid code here like...
echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";
header('Location: http://localhost/upload_app/info/nu');
exit;
}
$main_image_name = $_FILES['MainImage']['name'];
$main_image_size = $_FILES['MainImage']['size'];
$main_image_tmp = $_FILES['MainImage']['tmp_name'];
$uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));
$moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);
echo $uploadMainTo.'<br/>';
}
$uploadSecondTo = null;
if(isset($_FILES['SecondImage'])){
$path = $_FILES['SecondImage']['name']; // file means your input type file name
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {
// your code here like...
echo "Upload successful";
}else{
// your invalid code here like...
echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";
header('Location: http://localhost/upload_app/info/nu');
exit;
}
$second_image_name = $_FILES['SecondImage']['name'];
$second_image_size = $_FILES['SecondImage']['size'];
$second_image_tmp = $_FILES['SecondImage']['tmp_name'];
$uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));
$moveSecond = move_uploaded_file($second_image_tmp,$uploadSecondTo);
echo $uploadSecondTo.'<br/>';
}
$uploadPdfTo = null;
if(isset($_FILES['PDF'])){
$path = $_FILES['PDF']['name']; // file means your input type file name
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {
// your code here like...
echo "Upload successful";
}else{
// your invalid code here like...
echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";
header('Location: http://localhost/upload_app/info/nu');
exit;
}
$pdf_name = $_FILES['PDF']['name'];
$pdf_size = $_FILES['PDF']['size'];
$pdf_tmp = $_FILES['PDF']['tmp_name'];
$uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));
$movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);
echo $uploadPdfTo.'<br/>';
}
}
定义一个助手sa_helper.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('sa')){
function sa($string){
return str_replace(' ', '', $string);
}
}
?>
这是你的观点
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('info/normal_upload');?>
<input type="file" name="PDF">
<input type="file" name="MainImage">
<input type="file" name="SecondImage">
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
要使用 jquery 上传文件,请使用此表单,它也有效
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form_img").submit(function(e){
e.preventDefault();
var formData = new FormData($("#form_img")[0]);
$.ajax({
url : $("#form_img").attr('action'),
type : 'POST',
data : formData,
contentType : false,
processData : false,
success: function(resp) {
console.log(resp);
}
});
});
});
</script>
<form action="http://localhost/upload_app/info/normal_upload" id="form_img" method="GET" role="form" enctype="multipart/form-data">
<input type="file" name="PDF">
<input type="file" name="MainImage">
<input type="file" name="SecondImage">
<button type="submit">Submit </button>
</form>
- 2 回答
- 0 关注
- 135 浏览
添加回答
举报