在代码点火器中上传多张图片



HTML Code

<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" size="20" />
<input type="file" name="file2" size="20" />
<input type="submit" value="upload" />
</form>

控制器代码

public function do_upload()
{
$pic1 = "Name One";
$pic2 = "Name Two";
$RealName = array('file1', 'file2' );
$ChangeName = array($pic1, $pic2 );
$arrlength = count($ChangeName);
for($x = 0; $x < $arrlength; $x++)
{
$config['upload_path']          = './uploads/';
$config['file_name']          = $ChangeName[$x];
$config['allowed_types']        = 'gif|jpg|jpeg|png';
$config['max_size']             = 1909900;
$this->load->library('upload', $config);
$this->upload->do_upload($RealName[$x]);
echo $ChangeName[$x]; echo "<br>";
echo $RealName[$x]; echo "<br>";
}
}

尝试上传多张图片。代码运行正常,但我在保存所有图片时遇到了一些问题。问题是保存所有具有相同名称的图片(名称一(。

试试这个。 这段代码是我正在使用的...

private function upload_files($path, $title, $files)
{
$config = array(
'upload_path'   => $path,
'allowed_types' => 'jpg|gif|png',
'overwrite'     => 1,                       
);
$this->load->library('upload', $config);
$images = array();
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$fileName = $title .'_'. $image;
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('images[]')) {
$this->upload->data();
} else {
return false;
}
}
return $images;
}

相关内容

  • 没有找到相关文章

最新更新