當前位置:首頁 » 文件管理 » php微信上傳圖片

php微信上傳圖片

發布時間: 2022-11-16 03:59:37

『壹』 各位大神,微信小程序用戶上傳的圖片怎麼用php存儲資料庫

通常是將圖片的地址存儲到資料庫,上傳之後,應該能獲得圖片的保存地址,然後把這個圖片地址以字元串形式存儲到資料庫里。

『貳』 PHP 微信上傳永久素材

http請求方式: POST/FORM
http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
調用示例(使用curl命令,用FORM表單方式上傳一個多媒體文件):
curl -F [email protected] "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"

你這種情況屬於使用curl命令的方式傳值,幫助文檔:http://linux.51yip.com/search/curl

正確情況下的返回JSON數據包結果如下:{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
錯誤情況下的返回JSON數據包示例如下(示例為無效媒體類型錯誤):{"errcode":40004,"errmsg":"invalid media type"}
另外全局返回碼說明:https://mp.weixin.qq.com/wiki/17/.html

『叄』 PHP 微信端上傳圖片,上傳logo和banner圖,哪位大神傳授一下經驗啊,有demo更好,可以加分

<divclass="header">
<span>頭像</span>
<divclass="head_r"style="position:relative">
<imgclass="portrait_line"id="show_portrait"src="{$agent['portrait']}">
<imgsrc="__IMAGES__/more.png">
<inputtype="file"name="portrait"id="portrait"data-server="{:U('Home/Upload/mobile_upload_portrait')}"style="width:100%;height:100%;position:absolute;left:0px;top:0px;opacity:0;">
</div>
</div>

最後一個input file弄成透明的,占據整個你要觸發上傳的位置。

重點在下面,用原生ajax上傳提交圖片,並把上傳後伺服器本地的地址傳回來,通過js付到表單里,並把圖片縮小預覽貼出來。

<script>
$(function(){
$("input#portrait").on("change",function(){
changepic('portrait','show_portrait');
});
});

varxhr;
varreturnimg="";
varreturninput="";

functionchangepic(id,img,input){
returnimg=img;
returninput=input;
varfileObj=document.getElementById(id).files[0];
varuploadServer=$("#"+id).attr("data-server");
varform=newFormData();
form.append("portrait",fileObj);
createXMLHttpRequest();
xhr.onreadystatechange=handleStateChange;
xhr.open("post",uploadServer,true);
xhr.send(form);
}

functioncreateXMLHttpRequest()
{
if(window.ActiveXObject)
{
xhr=newActiveXObject("Microsoft.XMLHTTP");
}
elseif(window.XMLHttpRequest)
{
xhr=newXMLHttpRequest();
}
}
functionhandleStateChange()
{
var$loading=layer.open({type:2,})
if(xhr.readyState==4)
{
if(xhr.status==200||xhr.status==0)
{
varresult=xhr.responseText;
varjson=eval("("+result+")");
if(json.result=='success'){
$.ajax({
type:'post',
url:'/index.php/Wap/Self/updatePortrait',
data:{
portrait:json.url,
},
success:function(){

},
error:function(){
alert('伺服器錯誤');
},
});
$("#"+returnimg).attr("src",json.url);
layer.close($loading);
}
else{
alert('上傳頭像失敗'+json.msg);
layer.close($loading);
}

}
}
}
</script>

然後是ajax上傳的介面

publicfunctionmobile_upload_portrait(){//手機端上傳頭像
if(IS_POST){
$upload=newUpload();
$upload->maxSize=3*1024*1024;//3M
$upload->exts=array('jpg','gif','png','jpeg');
$upload->rootPath='./';
$upload->savePath='/Uploads/';
$upload->autoSub=true;
$upload->subName=array('date','Ymd');
$upload->saveName='uniqid';
if(!is_dir($upload->savePath)){
mkdir($upload->savePath);
}
$info=$upload->uploadOne($_FILES['portrait']);
if(!$info){
$result=array('result'=>'fail','msg'=>'請上傳3M以下的圖片');
}else{
$result=array('result'=>'success','url'=>$info['savepath'].$info['savename']);
}
$this->ajaxReturn($result);
}
}

代碼是Thinkphp的項目截出來的,頁面上有模板的痕跡,最後一段php的介面,也用了tp自帶的文件上傳類。不過看得懂的話,這些都不影響理解。

『肆』 微擎系統 微信端上傳圖片失敗怎麼辦

如果是表單上傳,要加上enctype="multipart/form-data",我經常忘記!!

『伍』 微信小程序如何使用PHP實現文件上傳

調用小程序文件上傳api
伺服器接收到微信post過來的文件之後,php處理代碼和傳統文件上傳代碼一樣

『陸』 如何在微信公眾平台上傳圖片

1、首先進入微信公眾平台官網。登陸微信公眾賬號,輸入賬號和密碼,如果是自己電腦可以記住密碼。

『柒』 php微信上傳永久圖片素材求代碼

您好,這樣的:
//素材
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';

/**
* 上傳臨時素材,有效期為3天(認證後的訂閱號可用)
* 注意:上傳大文件時可能需要先調用 set_time_limit(0) 避免超時
* 注意:數組的鍵值任意,但文件名前必須加@,使用單引號以避免本地路徑斜杠被轉義
* 注意:臨時素材的media_id是可復用的!
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 類型:圖片:image 語音:voice 視頻:video 縮略圖:thumb
* @return boolean|array
*/
public function uploadMedia($data, $type){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上傳多媒體文件介面使用 self::UPLOAD_MEDIA_URL 前綴
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 獲取臨時素材(認證後的訂閱號可用)
* @param string $media_id 媒體文件id
* @param boolean $is_video 是否為視頻文件,默認為否
* @return raw data
*/
public function getMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上傳多媒體文件介面使用 self::UPLOAD_MEDIA_URL 前綴
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
}
return $result;
}
return false;
}

/**
* 上傳永久素材(認證後的訂閱號可用)
* 新增的永久素材也可以在公眾平台官網素材管理模塊中看到
* 注意:上傳大文件時可能需要先調用 set_time_limit(0) 避免超時
* 注意:數組的鍵值任意,但文件名前必須加@,使用單引號以避免本地路徑斜杠被轉義
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 類型:圖片:image 語音:voice 視頻:video 縮略圖:thumb
* @param boolean $is_video 是否為視頻文件,默認為否
* @param array $video_info 視頻信息數組,非視頻素材不需要提供 array('title'=>'視頻標題','introction'=>'描述')
* @return boolean|array
*/
public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
if (!$this->access_token && !$this->checkAuth()) return false;
//#TODO 暫不確定此介面是否需要讓視頻文件走http協議
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
//當上傳視頻文件時,附加視頻文件信息
if ($is_video) $data['description'] = self::json_encode($video_info);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 上傳永久圖文素材(認證後的訂閱號可用)
* 新增的永久素材也可以在公眾平台官網素材管理模塊中看到
* @param array $data 消息結構{"articles":[{...}]}
* @return boolean|array
*/
public function uploadForeverArticles($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 修改永久圖文素材(認證後的訂閱號可用)
* 永久素材也可以在公眾平台官網素材管理模塊中看到
* @param string $media_id 圖文素材id
* @param array $data 消息結構{"articles":[{...}]}
* @param int $index 更新的文章在圖文素材的位置,第一篇為0,僅多圖文使用
* @return boolean|array
*/
public function updateForeverArticles($media_id,$data,$index=0){
if (!$this->access_token && !$this->checkAuth()) return false;
if (!isset($data['media_id'])) $data['media_id'] = $media_id;
if (!isset($data['index'])) $data['index'] = $index;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 獲取永久素材(認證後的訂閱號可用)
* 返回圖文消息數組或二進制數據,失敗返回false
* @param string $media_id 媒體文件id
* @param boolean $is_video 是否為視頻文件,默認為否
* @return boolean|array|raw data
*/
public function getForeverMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
//#TODO 暫不確定此介面是否需要讓視頻文件走http協議
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return $result;
}
return false;
}

/**
* 刪除永久素材(認證後的訂閱號可用)
* @param string $media_id 媒體文件id
* @return boolean
*/
public function delForeverMedia($media_id){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return true;
}
return false;
}

/**
* 獲取永久素材列表(認證後的訂閱號可用)
* @param string $type 素材的類型,圖片(image)、視頻(video)、語音 (voice)、圖文(news)
* @param int $offset 全部素材的偏移位置,0表示從第一個素材
* @param int $count 返回素材的數量,取值在1到20之間
* @return boolean|array
* 返回數組格式:
* array(
* 'total_count'=>0, //該類型的素材的總數
* 'item_count'=>0, //本次調用獲取的素材的數量
* 'item'=>array() //素材列表數組,內容定義請參考官方文檔
* )
*/
public function getForeverList($type,$offset,$count){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array(
'type' => $type,
'offset' => $offset,
'count' => $count,
);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 獲取永久素材總數(認證後的訂閱號可用)
* @return boolean|array
* 返回數組格式:
* array(
* 'voice_count'=>0, //語音總數量
* 'video_count'=>0, //視頻總數量
* 'image_count'=>0, //圖片總數量
* 'news_count'=>0 //圖文總數量
* )
*/
public function getForeverCount(){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

『捌』 用php上傳圖片怎麼做

上傳圖片原理:首先判斷文件類型是否為圖片格式,若是則上傳文件,然後重命名文件(一般都是避免上傳文件重名,現在基本上都是以為時間來命名),接著把文件上傳到指定目錄,成功上傳後輸出上傳圖片的預覽。

1.首先我們開始判斷文件類型是否為圖片類型用到的函數

{
strrchr:查找字元串在另一個字元串中最後一次出現的位置,並返回從該位置到字元串結尾的所有字元。
substr: 取部份字元串。
$HTTP_POST_FILES['file']['name']:獲取當前上傳的文件全稱。
}
圖片類型就是「.」後面的字元(比如:一個文件名稱為XXX.JPG 那麼它的類型就是「.」後面的JPG)。 我們可以用PHP中的函數來截取上傳者文件名字的。我們來寫個獲取文件類型的函數

<?
function type()
{
return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
}
?>
2.若是則上傳文件,然後重命名文件用到的函數

{ strtolower:把字元串的字母全部轉換為小寫字母. in_array: 函數在數組中搜索給定的值。 implode:函數把數組元素組合為一個字元串 random:隨機生成的數 $_FILES['userfile']['name']:上傳文件名稱 $uploaddir:自己定義的變數。比如在同一個文件夾裡面,你想把上傳的文件放到這個文件夾的FILE文件夾下,你可以這樣定義$uploaddir="./file/";注意寫法 } 這邊會出現很多問題,第一先寫一個能上傳類型的數組。第二判斷文件合法性。第三給文件重名。*(這邊判斷文件大小就不寫了)先定義允許上傳文件的類型數組:$type=array("jpg","gif","bmp","jpeg","png");第二用一個IF。。else。。寫一個判斷文件合法性的控制流語句。if(!in_arry(strtolower(type()),$type))//如果不存在能上傳的類型 { $text=implode('.',$type); echo "您只能上傳以下類型文件: ",$text,"<br>"; } 下面就是給他們重新命名了,else { $filename=explode(".",$_FILES['userfile']['name']);//把上傳的文件名以「.」好為准做一個數組。 $time=date("m-d-H-i-s");//去當前上傳的時間 $filename[0]=$time;//取文件名t替換 name=implode(".",$filename); //上傳後的文件名 $uploadfile=$uploaddir.$name;//上傳後的文件名地址 } 3.最後把文件上傳到指定目錄,成功上傳後輸出上傳圖片的預覽用到的函數{ move_uploaded_file:執行上傳文件 } if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) { echo "<center>您的文件已經上傳完畢 上傳圖片預覽: </center><br><center><img src='$uploadfile'></center>"; echo"<br><center><a href='javascrīpt:history.go(-1)'>繼續上傳</a></center>"; } else { echo"傳輸失敗!"; }

『玖』 php開發微信jssdk介面 iphone手機當一次多圖上傳時,只有一張上傳成功怎麼回事,安

微信上傳圖片,只能遞歸方法上傳多張,所以你可能是JS代碼里只上傳了一次

『拾』 PHP微信開發上傳永久縮略圖(thumb)失敗

你要想永久,就放自己伺服器,
在微信上面調用自己伺服器的圖片就好了

熱點內容
萬魔斬腳本 發布:2024-10-06 08:23:18 瀏覽:876
華強北耳機連安卓手機下什麼軟體 發布:2024-10-06 08:21:53 瀏覽:251
兒科發現傳染病的應急演練腳本 發布:2024-10-06 08:21:48 瀏覽:598
玩王者榮耀的時候要買哪個配置 發布:2024-10-06 08:19:41 瀏覽:620
imovie怎麼清理緩存 發布:2024-10-06 08:14:30 瀏覽:17
python字典list 發布:2024-10-06 08:14:26 瀏覽:578
amhip訪問 發布:2024-10-06 08:03:54 瀏覽:437
joy加密 發布:2024-10-06 07:53:27 瀏覽:201
結構是由哪種腳本語言填寫的 發布:2024-10-06 07:24:27 瀏覽:744
客戶端反編譯教學 發布:2024-10-06 07:10:42 瀏覽:783