【发布时间】:2012-08-29 19:50:37
【问题描述】:
我在一个页面上有一个表单。它有 5 个文本字段和 3 个上传文件字段。我需要将文本和文件路径写入数据库。我在网上看过很多例子,但大多数是上传单个文件或从同一个上传字段上传多个文件。
我是 CodeIgniter 的新手,所以代码 sn-ps 会很有帮助。
非常感谢。
【问题讨论】:
标签: codeigniter
我在一个页面上有一个表单。它有 5 个文本字段和 3 个上传文件字段。我需要将文本和文件路径写入数据库。我在网上看过很多例子,但大多数是上传单个文件或从同一个上传字段上传多个文件。
我是 CodeIgniter 的新手,所以代码 sn-ps 会很有帮助。
非常感谢。
【问题讨论】:
标签: codeigniter
另一个建议是:
function upload()
{
$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size
$config['max_width'] = '1024';//if file type is image
$config['max_height'] = '768';//if file type is image
$this->load->library('upload', $config);
foreach($_FILES as $Key => $File)
{
if($File['size'] > 0)
{
if($this->upload->do_upload($Key))
{
$data = $this->upload->data();
echo $data['file_name'];
}
else
{
// throw error
echo $this->upload->display_errors();
}
}
}
}
这将自动适用于您发布的所有文件输入,它不关心名称或数量:)
【讨论】:
$data 包含从$this->upload->data() 返回的信息 - 请参阅codeigniter.com/user_guide/libraries/file_uploading.html 的用户指南
allowed_types 配置。如果您更新了allowed_types 以包含您想要接受的其他类型并上传混合图像,例如word 文档,则只有那些可识别为图像(即图像/gif、图像/png)的文件将根据@987654328 进行验证@ 和 max_height 限制。希望有帮助吗?
希望这有帮助
$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size
$config['max_width'] = '1024';//if file type is image
$config['max_height'] = '768';//if file type is image
//etc config for file properties, you can check all of them out on website
现在假设您有 3 个文件,您要保存为 1.jpg, 2.jpg,3.gif,它们通过 3 个输入字段上传,pic1, pic2, pic3 这就是你的工作
for($ite=1;$ite<=3;$ite++){
if(!empty($_FILES["pic".$ite]["name"])){ //if file is present
$ext = pathinfo($_FILES['pic'.$ite]['name'], PATHINFO_EXTENSION); //get extension of file
$config["file_name"]="$ite.$ext"; //rename file to 1.jpg,2.jpg or 3.jpg, depending on file number and its extension
$this->upload->initialize($config); //upload library of codeigniter initialize function with config properties set earlier
if(!$this->upload->do_upload("pic".$ite)){
//error code
}
}
}
【讨论】: