[英]Codeigniter file upload error: You did not select a file to upload
[英]Codeigniter File Upload error: 'You did not select a file to upload'
我正在使用codeigniter 3,出現錯誤消息,例如You did not select a file to upload.
在do_upload()
函數中,但是普通的php函數move_uploaded_file()
可以正常工作。 我從stackoverflow引用了大多數答案,但沒有得到解決方案。
我認為這可能是wamp
問題,但我不清楚它在哪里。 如果此代碼在您的計算機上工作,那么它將在我的wamp php或Apache設置中出現。
查看:(upload.php)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php echo form_open_multipart('welcome/do_upload'); ?>
<input id="sfile" type="file" name="file">
<input type="submit" value="Submit">
<?php echo form_close(); ?>
</body>
</html>
控制器:(welcome.php)
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()) //not working
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
echo $basename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],$config['upload_path'].$basename);
// move_uploaded_file() works fine
}
根據CI 3,您需要在$this->upload->do_upload('')
傳遞名稱屬性。
因此,您需要在其中傳遞名稱屬性
$this->upload->do_upload('file');
檢查您上傳的文件的大小,因為您設置了$ config['max_size'] = '100';
。 這意味着您將無法上傳超過100 kb的文件大小
讀取文件上傳類
首先加載上傳庫,然后在初始化配置數據之后。
並在$ this-> upload-> do_upload('file')中傳遞輸入文件類型的屬性名稱。
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); # Load upload library
$this->upload->initialize($config); # Initialize
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
這可能與301錯誤導致的職位損失有關。
嘗試更換
<?php echo form_open_multipart('welcome/do_upload'); ?>
與(解決)
<form action="do_upload" method="post" enctype="multipart/form-data">
如果可行,則路由配置中存在一些錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.