Codeigniter 3.1.6是我正在使用的版本。当我尝试以jpg格式上传文件时它是成功的但是以png格式上传文件不起作用这里是我的代码。我正在使用dropzone.js进行文件上传
视图
<form action="admin/addimg" enctype="multipart/form-data" class="dropzone" style="margin-bottom: 5%; border: 2px dashed #0087F7; border-radius: 5px; background: white;" id="imgupld"></form>
<script>
Dropzone.options.imgupld = {
paramName: "file",
maxFilesize: 20, // MB
acceptedFiles: "image/*", // Accept images only
dictDefaultMessage: "Drag your image here"
};
</script>
控制器
function addimg()
{
if (!empty($_FILES))
{
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = './assets/img/';
$targetFile = $targetPath . $_FILES['file']['name'];
move_uploaded_file($tempFile, $targetFile);
}
}
答案 0 :(得分:1)
使用CI上传库可以轻松完成此操作。首先,您需要在config-&gt; autoload.php中加载库
$autoload['libraries'] = array('upload');
或在您的控制器中。示例
class ImageUpload extends CI_Controller
{
public function Upload()
{
if($_FILES["uploadImage"]["error"]==4)//check if user select input any image to upload
{
$config['Upload_Path']='';//File upload path
$config['allowed_types']='jpeg|jpg|png';//set the preferred file formats here
$config['max_size']=100000;// Max File Size
if($this->upload->do_upload('uploadImage'))//uploading the data
{
$uploadData=$this->upload->data();//get the details of uploaded data
}else
{
$error=$this->upload->display_errors();//this return the error message if any error thrown
}
}
}
}