【问题标题】:Why when I upload some images, and the result is only one image uploaded为什么我上传了一些图片,结果只上传了一张图片
【发布时间】:2017-05-08 14:54:40
【问题描述】:

在这里我使用“Codeigniter”,在我的方法中...... 为了上传图片,我使用了 Ajax 调用,我的 ajax 数据取自标签 'img' 的值 .attr ('src'), 数据值为base64码。

page_ajax_call.php

if ( isset($_POST['data_image_post']) ) {
   $to_upload   = 'users/VegetaSSJ/images/user_pp.png';
   $to_convert  = 'users/VegetaSSJ/images/'.time().'.jpg';
   $img_base64  = $this->input->post('data_image_post');

    list( $type, $data ) = explode(';', $img_base64);
    list( , $data )          = explode(',', $img_base64);
    $decode      = base64_decode($data);

    file_put_contents($to_upload, $decode);
    $ori_img_base64 = imagecreatefrompng($to_upload);
    imagejpeg($ori_img_base64, $to_convert, 100);
    unlink($to_upload);
}

Ajax.js

$('.wrap-img-attachment img').each(function(){
    $.ajax({
      url     : "home",
      type    : "post",
      data    : {'data_image_post': $(this).attr('src')},
      success : function(data) {
        console.log(data);
      }
    });
  });

请更正我的代码中的错误

【问题讨论】:

    标签: php jquery ajax codeigniter


    【解决方案1】:

    当单个 ajax 调用被发送到服务器时,它将文件保存到 'users/VegetaSSJ/images/user_pp.png' 所以下一个请求将覆盖这个文件 所以你需要通过添加时间戳来改变它,就像你在转换文件路径中所做的那样

      $to_upload   = 'users/VegetaSSJ/images/'.time().'user_pp.png';
    

    所以每个请求都会将图像保存到唯一的文件而不重叠

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 2013-07-28
      • 2016-11-22
      • 2017-05-21
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多