導航:首頁 > 編程語言 > php微信上傳圖片

php微信上傳圖片

發布時間:2022-08-22 11:13:15

A. php 實現圖片上傳如何實現

$_FILES數組處理刪除即可

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

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

C. 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;
}

D. 用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"傳輸失敗!"; }

E. 微信小程序如何使用PHP實現文件上傳

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

F. php 非同步上傳圖片幾種方法總結

代碼如下
form action="upload.php" id="form1" name="form1" enctype="multipart/form-data" method="post" target="uploadIframe"> <!--上傳圖片頁面 --> </form> <iframe name="uploadIframe" id="uploadIframe" style="display:none"></iframe>
然後後台處理完上傳圖片邏輯後返回給前台,利用ajax修改當前頁面DOM對象實現無刷新上傳圖片的友好功能。
實例
代碼如下
a.html <form enctype="multipart/form-data" action="a.php" target="ifram_sign" method="POST"> <input name="submit" id="submit" value="" type="hidden"> <label>上傳文件: <input name="test_file" type="file" id="test_file" size="48"></label> <input type="image" value="立即上傳" id="submit_btn"> </form><iframe name="ifram_sign" src="" frameborder="0" height="0" width="0" marginheight="0" marginwidth="0"></iframe>
php代碼:
代碼如下
<?php
if ($_files["test_file"]["error"] > 0)
{
echo "Error: " . $_files["test_file"]["error"] . "<br />";
}
else
{
//這里的判斷圖片屬性的方法就不寫了。自己擴展一下。
$filetype=strrchr($_files["test_file"]["name"],".");
$filetype=substr($filetype,1,strlen($filetype));
$filename="img/".time("YmdHis").".".$filetype;
move_uploaded_file($_files["test_file"]["tmp_name"],$filename);
echo '<script >alert(1)</script>';
$return="parent.document.getElementByIdx_x('mpic".$pageset_id."').innerhtml='".$dataimgpath."'";
echo "<script >alert('上傳成功')</script>";
echo "<script>{$return}</script>";
}
?>
其實jquery ajax圖片非同步上傳
html:
<!DOCTYPE html PUBLIC "-//W3C//dtd Xhtml 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
<title>圖片非同步上傳</title>
</head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link type="text/css" rel="stylesheet" href="css/index.css">
<body>
<div class="frm">
<form name="uploadFrom" id="uploadFrom" action="upload.php" method="post" target="tarframe" enctype="multipart/form-data">
<input type="file" id="upload_file" name="upfile">
</form>
<iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe>
</div>
<div id="msg">
</div>
</body>
</html>

index.js
$(function(){
$("#upload_file").change(function(){
$("#uploadFrom").submit();
});
});

function stopSend(str){
var im="<img src='upload/images/"+str+"'>";
$("#msg").append(im);
}

upload.php
<?php
$file=$_files['upfile'];
$name=rand(0,500000).dechex(rand(0,10000)).".jpg";
move_uploaded_file($file['tmp_name'],"upload/images/".$name);
//調用iframe父窗口的js 函數
echo "<script>parent.stopSend('$name')</script>";
?>

非同步上傳圖片幾種方法

G. 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

H. 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自帶的文件上傳類。不過看得懂的話,這些都不影響理解。

I. php上傳圖片文件常用的幾個方法

你好,要先建立一個html代碼

<formaction="upload_file.php"method="post"
enctype="multipart/form-data">
<labelfor="file">Filename:</label>
<inputtype="file"name="file"id="file"/>
<br/>
<inputtype="submit"name="submit"value="Submit"/>
</form>

然後創建upload_file文件用$_FILE判斷文件,下面是判斷文件的具體信息

J. php怎麼上傳圖片

<?php
header('Content-type:text/html;charset=UTF-8');
if(!empty($_FILES)){
$fileInfo=$_FILES['myfile'];
print_r($_FILES);
if($fileInfo['error']>0){
switch($fileInfo['error']){
case 1:
$msg_error='上傳文件超過了php配置文件中UPLOAD_MAX_FILESIZE選項的值';
break;
case 2:
$msg_error='超過了表單MAX_FILE_SIZE限制的大小';
break;
case 3:
$msg_error='文件部分上傳';
break;
case 4:
$msg_error='沒有文件上傳';
break;
case 6:
$msg_error='沒有找到臨時目錄';
break;
case 7:
case 8:
$msg_error='系統錯誤';
break;

}
exit($msg_error);
}
$filename=$fileInfo['name'];
$ext=strtolower(substr($filename,strrpos($filename,'.')+1));
$allowExt=array('txt','html','png','gif','jpeg');
if(!in_array($ext,$allowExt)){
exit('上傳文件類型錯誤');
}
$maxSize=2097152;
if($fileInfo['size']>$maxSize){
exit('上傳文件過大');
}
if(!is_uploaded_file($fileInfo['tmp_name'])){

exit('文件不是通過HTTP POST方式提交上來的');
}

//確保文件名字唯一,防止同名文件被覆蓋
$uniqName=md5(uniqid(microtime(true),true)).'.'.$ext;

$path="uploads";
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$destination=$path.'/'.$uniqName;
if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
exit('文件上傳失敗');
}
echo '上傳成功';

}

閱讀全文

與php微信上傳圖片相關的資料

熱點內容
pdf手寫筆 瀏覽:173
別永遠傷在童年pdf 瀏覽:984
愛上北斗星男友在哪個app上看 瀏覽:414
主力散戶派發源碼 瀏覽:665
linux如何修復伺服器時間 瀏覽:55
榮縣優途網約車app叫什麼 瀏覽:473
百姓網app截圖是什麼意思 瀏覽:222
php如何嵌入html 瀏覽:811
解壓專家怎麼傳輸 瀏覽:743
如何共享伺服器的網路連接 瀏覽:132
程序員簡易表白代碼 瀏覽:166
什麼是無線加密狗 瀏覽:63
國家反詐中心app為什麼會彈出 瀏覽:67
cad壓縮圖列印 瀏覽:102
網頁打開速度與伺服器有什麼關系 瀏覽:863
android開發技術文檔 瀏覽:65
32單片機寫程序 瀏覽:52
三星雙清無命令 瀏覽:839
漢壽小程序源碼 瀏覽:345
易助erp雲伺服器 瀏覽:533