Php 当我使用codeigniter将多个图像上载到数据库时,文件url错误,但图像插入到文件夹和数据库中
我知道这样问可能是错误的,但当我使用ajax插入多个图像并将其插入数据库文件时,会被插入,但url是错误的,让我向您展示 “”http://localhost/P_Display/uploads/bike-926023_192048.jpg“” 这是我从文件保存的控制台获得的url,但这可能是因为我用于上载多个文件的for循环 让我给你看看控制器代码Php 当我使用codeigniter将多个图像上载到数据库时,文件url错误,但图像插入到文件夹和数据库中,php,mysql,codeigniter,Php,Mysql,Codeigniter,我知道这样问可能是错误的,但当我使用ajax插入多个图像并将其插入数据库文件时,会被插入,但url是错误的,让我向您展示 “”http://localhost/P_Display/uploads/bike-926023_192048.jpg“” 这是我从文件保存的控制台获得的url,但这可能是因为我用于上载多个文件的for循环 让我给你看看控制器代码 public function post_data_multimage() { $data = array(); $data['title']
public function post_data_multimage()
{
$data = array();
$data['title'] = 'Multiple file upload';
if($this->input->post())
{
// retrieve the number of images uploaded;
$number_of_files = sizeof($_FILES['multimage']['tmp_name']);
$files = $_FILES['multimage'];
$errors = array();
for($i=0;$i<$number_of_files;$i++)
{
if($_FILES['multimage']['error'][$i] != 0) $errors[$i][] = 'Couldnt upload file '.$_FILES['multimage']['name'][$i];
}
if(sizeof($errors)==0)
{
// now, taking into account that there can be more than one file, for each file we will have to do the upload
// we first load the upload library
$this->load->library('upload');
// next we pass the upload path for the images
$config['upload_path'] = FCPATH . './uploads/';
// also, we make sure we allow only certain type of images
$config['allowed_types'] = 'gif|jpg|png|jpeg';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['multimage']['name'] = $files['name'][$i];
$_FILES['multimage']['type'] = $files['type'][$i];
$_FILES['multimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['multimage']['error'] = $files['error'][$i];
$_FILES['multimage']['size'] = $files['size'][$i];
//now we initialize the upload library
$this->upload->initialize($config);
$image= $_FILES['multimage']['name'];
$post = $this->input->post();
unset($post['submit']);
$this->load->model('Pmodel');
$multi_data= $this->Pmodel->post_data_multimage($post,$image);
// we retrieve the number of files that were uploaded
if ($this->upload->do_upload('multimage'))
{
$data['uploads'][$i] = $this->upload->data();
$upload_data=$data['uploads'][$i];
$image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
echo json_encode($image_path);
}
else
{
$data['upload_errors'][$i] = $this->upload->display_errors();
}
}
}
else
{
print_r($errors);
}
echo '<pre>';
print_r($data);
echo '</pre>';
}
else
{
echo "string";
}
}
public函数post_data_multimage()
{
$data=array();
$data['title']=“多文件上传”;
如果($this->input->post())
{
//检索上载的图像数量;
$number_of_files=sizeof($_files['multimage']['tmp_name']);
$files=$_文件['multimage'];
$errors=array();
对于($i=0;$iload->library('upload');
//接下来,我们传递图像的上传路径
$config['upload_path']=FCPATH./uploads/”;
//此外,我们确保只允许某些类型的图像
$config['allowed_types']='gif | jpg | png | jpeg';
对于($i=0;$i<$u文件数;$i++){
$_FILES['multimage']['name']=$FILES['name'][$i];
$\u文件['multimage']['type']=$FILES['type'][$i];
$\u FILES['multimage']['tmp\u name']=$FILES['tmp\u name'][$i];
$_FILES['multimage']['error']=$FILES['error'][$i];
$_文件['multimage']['size']=$FILES['size'][$i];
//现在我们初始化上传库
$this->upload->initialize($config);
$image=$\u文件['multimage']['name'];
$post=$this->input->post();
未设置($post['submit']);
$this->load->model('Pmodel');
$multi_data=$this->Pmodel->post_data\u multimage($post,$image);
//我们检索上载的文件数
如果($this->upload->do_upload('multimage'))
{
$data['uploads'][$i]=$this->upload->data();
$upload_data=$data['uploads'][$i];
$image_path=base_url(“uploads/”$upload_data['raw_name']。$upload_data['file_ext']);
echo json_编码($image_路径);
}
其他的
{
$data['upload_errors'][$i]=$this->upload->display_errors();
}
}
}
其他的
{
打印错误($errors);
}
回声';
打印(数据);
回声';
}
其他的
{
回声“字符串”;
}
}
如何从我的url中删除这些斜杠?保存相同的地址
'Assets/image\u upload/。$image=$\u文件['multimage']['name']
若存在数据库的图像表,则输入字段
当你展示它的时候
echo(base\u url($item->image))//数据库中显示的图像字段。
我认为这可能会起作用。我们需要的只是向控制器添加一个html元素,该元素将从控制器回显图像,而不需要将值传递给视图,从而结束错误url的冲突
这是我的密码
$data=array();
$data['title']=“多文件上传”;
如果($this->input->post())
{
//检索上载的图像数量;
$number_of_files=sizeof($_files['multimage']['tmp_name']);
$files=$_文件['multimage'];
$errors=array();
对于($i=0;$iload->library('upload');
//接下来,我们传递图像的上传路径
$config['upload_path']=FCPATH./uploads/”;
//此外,我们确保只允许某些类型的图像
$config['allowed_types']='gif | jpg | png | jpeg';
对于($i=0;$i<$u文件数;$i++){
$_FILES['multimage']['name']=$FILES['name'][$i];
$\u文件['multimage']['type']=$FILES['type'][$i];
$\u FILES['multimage']['tmp\u name']=$FILES['tmp\u name'][$i];
$_FILES['multimage']['error']=$FILES['error'][$i];
$_文件['multimage']['size']=$FILES['size'][$i];
//现在我们初始化上传库
$this->upload->initialize($config);
$image=$\u文件['multimage']['name'];
$post=$this->input->post();
未设置($post['submit']);
$this->load->model('Pmodel');
$multi_data=$this->Pmodel->post_data\u multimage($post,$image);
//我们检索上载的文件数
如果($this->upload->do_upload('multimage'))
{
$data['uploads'][$i]=$this->upload->data();
$upload_data=$data['uploads'][$i];
$user_image=array('src'=>base_url(“uploads/”)$image),
“宽度”=>“100px”,
“高度”=>“100px”,
);
回声“;
}
其他的
{
$data['upload_errors'][$i]=$this->upload->display_errors();
}
}
}
其他的
{
打印错误($errors);
}
}
其他的
{
回声“字符串”;
}
}
请参阅,我只是用其路径回显图像,从而消除了问题。这是我的解决方案。您的解决方案是正确的,但我不需要向数据库添加地址。我只想显示我的图像,无需插入图像路径即可实现。您可以将基本url('Assets/image\u upload/')。$image=$FILES['multimage']['name']保存到数据库字段中。
$data = array();
$data['title'] = 'Multiple file upload';
if($this->input->post())
{
// retrieve the number of images uploaded;
$number_of_files = sizeof($_FILES['multimage']['tmp_name']);
$files = $_FILES['multimage'];
$errors = array();
for($i=0;$i<$number_of_files;$i++)
{
if($_FILES['multimage']['error'][$i] != 0) $errors[$i][] = 'Couldnt upload file '.$_FILES['multimage']['name'][$i];
}
if(sizeof($errors)==0)
{
// now, taking into account that there can be more than one file, for each file we will have to do the upload
// we first load the upload library
$this->load->library('upload');
// next we pass the upload path for the images
$config['upload_path'] = FCPATH . './uploads/';
// also, we make sure we allow only certain type of images
$config['allowed_types'] = 'gif|jpg|png|jpeg';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['multimage']['name'] = $files['name'][$i];
$_FILES['multimage']['type'] = $files['type'][$i];
$_FILES['multimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['multimage']['error'] = $files['error'][$i];
$_FILES['multimage']['size'] = $files['size'][$i];
//now we initialize the upload library
$this->upload->initialize($config);
$image= $_FILES['multimage']['name'];
$post = $this->input->post();
unset($post['submit']);
$this->load->model('Pmodel');
$multi_data= $this->Pmodel->post_data_multimage($post,$image);
// we retrieve the number of files that were uploaded
if ($this->upload->do_upload('multimage'))
{
$data['uploads'][$i] = $this->upload->data();
$upload_data=$data['uploads'][$i];
$user_image = array( 'src' => base_url("uploads/" .$image),
'width' => '100px',
'height'=> '100px',
);
echo "<a href='#'>" . img($user_image)."</a>";
}
else
{
$data['upload_errors'][$i] = $this->upload->display_errors();
}
}
}
else
{
print_r($errors);
}
}
else
{
echo "string";
}
}