我正在尝试使用代码点火器上传图像,但它没有显示任何失败消息的成功。 请帮忙。
这是一个文件上传表单
view- Upload_form.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="">
<?php echo $error; ?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br><br>
<input type="submit" value="upload"/>
</form>
</body>
</html>
这是一个视图成功文件
查看 - upload_success.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Upload form</title>
</head>
<body>
<h1> YOUR FILE WAS SUCCESSFULLY UPLOADED </h1>
<ul>
<?php foreach ($upload_data as $item => $value); ?>
<li><?php echo $item; ?> : <?php echo $value; ?></li>
<?phpendforeach ;?>
</ul>
<?php echo anchor('upload','upload Another file'); ?>
</body>
</html>
这是一个控制器文件。
controller - upload.php
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper (array('form','url'));
}
public function index(){
$this->load->view('Upload_form',array('error'=>' '));
}
public function do_upload(){
$config = array(
'upload_path' => './uploads/',
'allowed_types' => 'gif|jpg|png',
'max_size' => 100,
'max_width' => 1024, //Mainly goes with images only
'max_heigth' => 768
);
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form',$error);
}
else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
}
}
我在代码点火器的根目录中创建了uploads文件夹。
答案 0 :(得分:0)
你的upload_path可能不正确,所以这里是我使用的代码:
1.在控制器顶部添加此内容以声明upload_path:
function __construct()
{
parent::__construct();
$this->uploadPath = realpath(APPPATH . '../uploads');
}
2.从
更改$ config upload_path&#39; ./上传/&#39;
到
$这 - &GT; uploadPath
有关详细信息,请参阅我的uploadProfile控制器:
function upload_profilePic($userID)
{
//create a unique ID
$filename = $userID. '-' .uniqid();
//config upload
$config=array(
'allowed_types' => 'jpg|jpeg|png|PNG',
'upload_path' => $this->imagePath,
'max_size' => 90000,
'file_name' => $filename
);
//upload
$this->load->library('upload',$config);
$this->upload->do_upload('profilePic');
//get file data
$data = $this->upload->data();
$fileExtension = $data['file_ext'];
if($fileExtension=="")
$filename = '';
else
$filename=$data['file_name'];
//
$data = array(
'profilePic' => $filename,
);
$this->db->where('userID',$userID)
->update('tblusers',$data);
}
答案 1 :(得分:0)
请在代码中对控制器和查看页面进行一些更改。还要确保上传文件夹位于之外>应用程序 folder.below代码在您的视图中使用 Upload_form
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--<form action="" method="post">-->
<?php echo $error; ?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br><br>
<input type="submit" value="upload"/>
<?php
form_close();
?>
</body>
并尝试按照控制器中的代码上传
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('upload_form',array('error'=>' '));
}
public function do_upload(){
$config = array(
'upload_path' => './uploads/',
'allowed_types' => 'gif|jpg|png',
'max_size' => 100,
'max_width' => 1024, //Mainly goes with images only
'max_heigth' => 768
);
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form',$error);
}
else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
}
}