使用angular 7和php codeigniter上传图像文件

使用angular 7和php codeigniter上传图像文件,php,angular,codeigniter,Php,Angular,Codeigniter,php代码点火器 在上面的代码中,我不明白为什么路径在数据库中插入了2次,有2个不同的路径,或者2个路径没有显示任何图像,我不知道代码有什么问题,我是新来的,请帮助我解决这个问题 棱角的 component.html 服务台 这是我上传图像文件的角度代码,我对这段代码没有问题,它在控制台上得到了正确的输出,但唯一的问题是后端codeigniter代码的问题 每次您在输入中插入图像时,它都会上载到后端(可能您希望在单击按钮后移动该图像) 你只想上传一张图片还是多张 在php中解码图像时,您应该忽略

php代码点火器

在上面的代码中,我不明白为什么路径在数据库中插入了2次,有2个不同的路径,或者2个路径没有显示任何图像,我不知道代码有什么问题,我是新来的,请帮助我解决这个问题

棱角的

component.html

服务台

这是我上传图像文件的角度代码,我对这段代码没有问题,它在控制台上得到了正确的输出,但唯一的问题是后端codeigniter代码的问题

  • 每次您在输入中插入图像时,它都会上载到后端(可能您希望在单击按钮后移动该图像)

  • 你只想上传一张图片还是多张

  • 在php中解码图像时,您应该忽略这一点,因为您正在上载一个带有formData而不是字符串的二进制文件(我不是php专家,可能我错了,但在节点中,只需将二进制文件保存在文件夹中)


  • 请提供您的角度代码
    public function upload(){
        $image = base64_decode($this->input->post("p_image"));
        $image_name = md5(uniqid(rand(), true));
        $filename = $image_name . '.' . 'png';
        //rename file name with random number
        $path = "./uploads/images/".$filename;
        //image uploading folder path
        file_put_contents($path , $image);
        // image is bind and upload to respective folder
    
        $data_insert = array('p_image'=>$filename);
        if(!empty($data_insert )){
        $success=$this->db->insert('projects', $data_insert);
    
        }else{ } 
        if($success){
            $b = "User Registered Successfully..";
        }
        else
        {
            $b = "Some Error Occured. Please Try Again..";
        }
        echo json_encode($b);
    
    }
    
    
    <div class="admin-page">
      <div class="add-projects-form">
        <form [formGroup]="myForm"  enctype="multipart/form-data">
        <!-- image upload start -->
            <div class="form-group">
            <div class="upload-image">
            <div #image class="image-wrapper"></div>
            <div class="browse-btn">
            <input type="file" name="cameraImg" id="cameraImg" (change)="onSelectedFile($event)" >
            </div>
            </div>
            </div>
            <div style="text-align: center;margin-top:40px;">
            <button class="btn btn-success" type="submit" (click)="addProject()">Add Project</button>
          </div> 
      </form>
    </div>
    </div>
    
    onSelectedFile(event) {
      if (event.target.files.length > 0) {
      const productImage = event.target.files[0];
      const formData = new FormData();
      formData.append('p_image', productImage);
      console.log(productImage.name);
      console.log(productImage);
      this.adminService.uploadImage(productImage).subscribe(
      res => {
      if (res){
      console.log('success')
      this.uploadError = '';
      } else {
      // this.uploadError = res.response.message;
      }
      },
      err => this.error = err
      );
      }
      } 
    
      uploadImage(p_image){
        return this.http.post(`${CONFIG.API_URL}/Welcome/upload`, p_image);
      }