Php 照片上传在CodeIgniter中不工作
我试图上传照片使用codeigniter,但这是不工作的。我没有得到文件名,它只显示错误。助手文件也在我的系统文件夹中可用,但我不上传照片 控制器:upload.phpPhp 照片上传在CodeIgniter中不工作,php,codeigniter,image-uploading,Php,Codeigniter,Image Uploading,我试图上传照片使用codeigniter,但这是不工作的。我没有得到文件名,它只显示错误。助手文件也在我的系统文件夹中可用,但我不上传照片 控制器:upload.php <?php class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url','
<?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>
上传表单
上传_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>
上传表单
您的文件已成功上载!
- :
调用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
}
}
另外,确保上传目录是可写的。从
$this->upload->display_errors()
返回的错误是什么?尝试在控制器本身中打印错误:if(!$this->upload->do_upload()){$error=array('error'=>this->upload->display_errors());print__($error);die;$this->load->view('upload_form',$error);}默认情况下,文件名为userfile
。因此,如果您没有在do_upload()
中指定文件名,默认情况下需要userfile
。