我正在尝试使用codeigniter上传照片,但这不起作用。我没有得到文件名它只显示错误。帮助文件也可以在我的系统文件夹中找到,但我没有上传照片。
控制器:upload.php
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','file'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$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);
}
}
}
?>
查看:upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?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
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
答案 0 :(得分:0)
我总是在调用do_upload()方法时指定输入字段名。这是一个来自工作示例的片段,遵循模式,它应该可以工作。
$field_name = 'userfile';
if (isset($_FILES[$field_name]) && is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field_name)) {
//upload failed
} else {
//upload passed
}
}
另外,请确保您的上传目录是可写的。