public function mysave($img, $target = "articles") { if ($img != '') { DB::table($this->table)->insert(array('img' => $img, 'cus_id' => Auth::id(), 'target' => $target)); //===释放用户空间容量=== $customer = Auth::user()->name; $filepath = public_path('customers/' . $customer . '/images/l/' . $target . '/' . $img); if (is_file($filepath)) { $size = filesize($filepath); //===images/l不一定有,要推送后才有图片 } else { $size = 0; } $Capacity = new CapacityController(); $Capacity->change_capa($size, 'free'); //===end=== return $Capacity->format_bytes($size); // return DB::table($this->table)->insert(array('img' => $img, 'cus_id' => Auth::id(), 'target' => $target)); } }
/** * ===点击保存按钮后动作=== * @return type */ public function img_upload() { $customer = Auth::user()->name; $cus_id = Auth::id(); $target = Input::get('target'); $this->check_dir($target, $customer); $files = explode(',', ltrim(Input::get('files'), ',')); $dir = public_path('customers/' . $customer . '/cache_images/'); $img_size = Input::get('imgsize') ? Input::get('imgsize') : 400; $destinationPath = public_path('customers/' . $customer . '/images/'); $weburl = Customer::where('id', $cus_id)->pluck('weburl'); $suf_url = str_replace('http://c', '', $weburl); if ($files) { $data = array(); $i = 0; //同步到客户服务器 $customerinfo = Customer::find($cus_id); $ftp_array = explode(':', $customerinfo->ftp_address); $port = $customerinfo->ftp_port; $ftpdir = $customerinfo->ftp_dir; $ftp = $customerinfo->ftp; $ftp_array[1] = isset($ftp_array[1]) ? $ftp_array[1] : $port; $conn = ftp_connect($ftp_array[0], $ftp_array[1]); $filesize = 0; //===文件大小=== if (file_exists(public_path('customers/' . $customer . '/img.zip'))) { @unlink(public_path('customers/' . $customer . '/img.zip')); } if ($conn) { ftp_login($conn, $customerinfo->ftp_user, $customerinfo->ftp_pwd); ftp_pasv($conn, 1); $zip = new ZipArchive(); if ($zip->open(public_path('customers/' . $customer . '/img.zip'), ZipArchive::CREATE) === TRUE) { foreach ((array) $files as $fileName) { $filepath = public_path('customers/' . $customer . '/cache_images/' . $fileName); if (file_exists($filepath)) { $filesize += filesize($filepath); //===累加大小 $file = explode('.', $fileName); $type = end($file); $up_result = copy(public_path('customers/' . $customer . '/cache_images/' . $fileName), $destinationPath . '/l/' . $target . '/' . $fileName); if ($up_result) { $s_path = $destinationPath . '/s/' . $target . '/' . $fileName; $img_info = getimagesize($destinationPath . '/l/' . $target . '/' . $fileName); switch ($img_info[2]) { case 1: $type = 'gif'; break; case 2: $type = 'jpg'; break; case 3: $type = 'png'; break; } $this->resizeImage($destinationPath . '/l/' . $target . '/' . $fileName, $type, $s_path, $img_size, $img_size); copy($destinationPath . '/l/' . $target . '/' . $fileName, public_path('customers/' . $customer . '/mobile/images/l/' . $target . '/' . $fileName)); $mobile_s_path = public_path('customers/' . $customer . '/mobile/images/s/') . $target . '/' . $fileName; $this->resizeImage(public_path('customers/' . $customer . '/mobile/images/l/') . $target . '/' . $fileName, $type, $mobile_s_path, $img_size, $img_size); if (trim($ftp) == '1') { $zip->addFile($destinationPath . '/l/' . $target . '/' . $fileName, 'images/l/' . $target . '/' . $fileName); $zip->addFile($destinationPath . '/s/' . $target . '/' . $fileName, 'images/s/' . $target . '/' . $fileName); // $zip->addFile(public_path('customers/' . $customer . '/mobile/images/s/') . $target . '/' . $fileName,'mobile/images/l/' . $target . '/' . $fileName); // $zip->addFile(public_path('customers/' . $customer . '/mobile/images/l/') . $target . '/' . $fileName,'mobile/images/s/' . $target . '/' . $fileName); // ftp_put($conn, $customer . '/images/l/' . $target . '/' . $fileName, $destinationPath . '/l/' . $target . '/' . $fileName, FTP_BINARY); // ftp_put($conn, $customer . '/images/s/' . $target . '/' . $fileName, $destinationPath . '/s/' . $target . '/' . $fileName, FTP_BINARY); // ftp_put($conn, $customer . '/mobile/images/l/' . $target . '/' . $fileName, public_path('customers/' . $customer . '/mobile/images/l/') . $target . '/' . $fileName, FTP_BINARY); // ftp_put($conn, $customer . '/mobile/images/s/' . $target . '/' . $fileName, public_path('customers/' . $customer . '/mobile/images/s/') . $target . '/' . $fileName, FTP_BINARY); } else { ftp_put($conn, $ftpdir . '/' . 'images/l/' . $target . '/' . $fileName, $destinationPath . '/l/' . $target . '/' . $fileName, FTP_BINARY); ftp_put($conn, $ftpdir . '/' . 'images/s/' . $target . '/' . $fileName, $destinationPath . '/s/' . $target . '/' . $fileName, FTP_BINARY); ftp_put($conn, $ftpdir . '/' . 'mobile/images/l/' . $target . '/' . $fileName, public_path('customers/' . $customer . '/mobile/images/l/') . $target . '/' . $fileName, FTP_BINARY); ftp_put($conn, $ftpdir . '/' . 'mobile/images/s/' . $target . '/' . $fileName, public_path('customers/' . $customer . '/mobile/images/s/') . $target . '/' . $fileName, FTP_BINARY); } $data[$i]['name'] = $fileName; $data[$i]['url'] = asset('customers/' . $customer . '/images/l/' . $target . '/' . $fileName); $data[$i]['s_url'] = '/images/s/' . $target . '/' . $fileName; $i++; } } } $zip->close(); if ($conn) { if (trim($ftp) == '1') { $weburl = Customer::where('id', $cus_id)->pluck('weburl'); $suf_url = str_replace('http://c', '', $weburl); $cus_name = strtolower(Customer::where('id', $cus_id)->pluck('name')); if (trim($ftp) == '1') { $ftp_pcdomain = "http://" . $cus_name . $suf_url; } ftp_put($conn, $customer . '/img.zip', public_path('customers/' . $customer . '/img.zip'), FTP_BINARY); ftp_put($conn, $customer . '/img_unzip.php', public_path('packages/img_unzip.php'), FTP_ASCII); @file_get_contents("{$ftp_pcdomain}/img_unzip.php"); @unlink(public_path('customers/' . $customer . '/img.zip')); } } } //===扣除空间容量=== $Capacity = new CapacityController(); $Capacity->change_capa($filesize, 'use'); //===end=== @ftp_close($conn); return Response::json(['err' => 0, 'msg' => '保存成功', 'data' => $data]); } } else { return Response::json(['err' => 1001, 'msg' => '保存失败']); } }