1. 程式人生 > >小程式上傳圖片多圖上傳

小程式上傳圖片多圖上傳

//上傳圖片 多圖 choosePic: function() { var that = this; wx.chooseImage({ count: 9, // 預設9 sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有 'original', sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有 success: function(res) { // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片 var tempFilePaths = res.tempFilePaths; //上傳圖片 var successUp = 0; //成功個數 var failUp = 0; //失敗個數 var length = res.tempFilePaths.length; //總共個數
var i = 0; //第幾個 that.uploadDIY(res.tempFilePaths, successUp, failUp, i, length); // console.log(that.data.imglist); console.log(that.data.upload_picture_list); }, }); },
uploadDIY(filePaths, successUp, failUp, i, length) { var that = this; var uniacid = app.siteInfo.uniacid; var upload_picture_list = that.data.upload_picture_list; wx.uploadFile({ url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=pinba', filePath: filePaths[i], name: 'upfile', formData: {}, success: (resp) => { console.log(resp.data);
successUp++;
upload_picture_list.push(resp.data); that.setData({ upload_picture_list: upload_picture_list }) console.log(upload_picture_list);
},
fail: (res) => { failUp++; }, complete: () => { i++; if (i == length) { wx.hideLoading(); console.log('總共' + successUp + '張上傳成功,' + failUp + '張上傳失敗!'); } else { //遞迴呼叫uploadDIY函式 this.uploadDIY(filePaths, successUp, failUp, i, length);
} },
});
},       釋出 formSubmit: function(e) { var that = this; var values = e.detail.value;
//需處理 if (e.detail.value.text == "") { wx.showToast({ title: '說點什麼吧', image: "../images/error.png" }) } else { var val = e.detail.value; var f_neirong = val.f_neirong var upload_picture_list = that.data.upload_picture_list; var openid = wx.getStorageSync('openid') console.log(val) app.util.request({ 'url': "entry/wxapp/Fabufenxiang", data: { openid: openid, f_neirong: f_neirong, f_slide: upload_picture_list, }, success: function(res) { wx.showToast({ title: "釋出成功!", }) wx.redirectTo({ url: '../sharefaxian/sharefaxian', }) console.log(res); }
}) } },           方法:

//釋出分享
public function doPageFabufenxiang(){
global $_W,$_GPC;
$uniacid = $_W['uniacid'];

// $idarr =htmlspecialchars_decode($wekinfo);
// $array =json_decode($idarr);
// $object =json_decode(json_encode($array),true);
// $arr =serialize(資料庫欄位);

$f_slide = htmlspecialchars_decode($_REQUEST['f_slide']);
// $arr=substr($f_slide,1,strlen($f_slide)-1);
// echo "<pre>";print_r($arr);echo "</pre>";
// $array=substr($arr,0,-1);
// echo "<pre>";print_r($array);echo "</pre>";


$array =json_decode($f_slide);//轉陣列
$object =json_decode(json_encode($array),true);
// echo "<pre>";print_r($object);echo "</pre>";


// $obj = implode(",", $object);

$data = array(
"uniacid"=>$uniacid,
"f_neirong"=>$_REQUEST['f_neirong'],
"f_slide"=>serialize($object),
"f_time"=>strtotime(date("Y-m-d H:i:s")),
);
$openid = $_REQUEST['openid'];
$users = pdo_fetch("SELECT * FROM ".tablename("pinba_userinfo")." where uniacid=:uniacid and openid=:openid",array(":uniacid"=>$uniacid,":openid"=>$openid));
$data['f_xid'] = $users['u_id'];
$res = pdo_insert("pinba_fenxiang",$data);
}
public function doPageUrl()
{
global $_W;
echo $_W['siteroot'];
}
public function doPageUpload()
{
global $_W, $_GPC;
$uniacid = $_W['uniacid'];
$uptypes = array('image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png');
$max_file_size = 2000000;
$destination_folder = '../attachment/';
if (!is_uploaded_file($_FILES['upfile']['tmp_name'])) {
echo '圖片不存在!';
die;
}
$file = $_FILES['upfile'];
if ($max_file_size < $file['size']) {
echo '檔案太大!';
die;
}
if (!in_array($file['type'], $uptypes)) {
echo '檔案型別不符!' . $file['type'];
die;
}
$filename = $file['tmp_name'];
$image_size = getimagesize($filename);
$pinfo = pathinfo($file['name']);
$ftype = $pinfo['extension'];
$destination = $destination_folder . str_shuffle(time() . rand(111111, 999999)) . '.' . $ftype;
if (file_exists($destination) && $overwrite != true) {
echo '同名檔案已經存在了';
die;
}
if (!move_uploaded_file($filename, $destination)) {
echo '移動檔案出錯';
die;
}
$pinfo = pathinfo($destination);
$fname = $pinfo['basename'];
echo $_W['attachurl'].$fname;
@(require_once IA_ROOT . '/framework/function/file.func.php');
@($filename = $fname);
@file_remote_upload($filename);
}

 

 

 

 

主要修改格式伺服器圖片

$f_slide = htmlspecialchars_decode($_REQUEST['f_slide']);
$array =json_decode($f_slide);//轉陣列

$object =json_decode(json_encode($array),true);

 

 

$data = array(
"uniacid"=>$uniacid,
"f_slide"=>serialize($object),

);