php仿微信图片上传
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" name="fileField" id="fileField" />
<input type="submit" name="submit" id="submit" value="提交" />
</form>
<?php
if($_POST["submit"])
{
$file_gai = $_FILES["fileField"];$filename_gai=$file_gai["tmp_name"];
$destination = basename($file_gai["name"]);
if(!move_uploaded_file ($filename_gai, $destination))
{
echo "<font color='red'>移动文件出错!</a>";
}else{
echo ("<script>location.href='?file=".urlencode($destination)."'</script>");
}
}if($_GET["file"])
{
echo "<img src='".urldecode($_GET["file"])."'>";
}
?>
Ⅱ php怎么通过api接口上传图片
require_once "../common_mysql.php";
require_once MESSAGE_PATH . 'zh/zh_calendar_message.php';
require_once "function_common/user_function.php";
require_once "function_common/public_function.php";
global $DB;
$sql_time = microtime ( true );
//$uid = $self_userid;
//保存图片
$json_result ['status'] = 0;
$path = 'upfile';
$json_result ['status'] = 0;
$json_result ['successmsg'] = '上传失败';
if (isset ( $_FILES ['imageZip'] )) {
$upfile = 'upfile/' . $_FILES ['imageZip'] ['name'];
if (! @file_exists ( $path )) {
@mkdir ( $path );
}
$result = @move_uploaded_file ( $_FILES ['imageZip'] ['tmp_name'], $upfile );
if (! $result) {
$json_result ['status'] = 0;
$json_result ['successmsg'] = '上传失败';
$json_result ['datas'] = array ('savePath' => $upfile );
exit ( json_encode ( $json_result ) );
}
}
$json_result ['status'] = 1;
$json_result ['datas'] = array ('savePath' => $upfile );
Ⅲ php上传图片到服务器的前端和php代码
<?
require_once('../classfile/guid.class.php');
if(!isset($_FILES['imgFile'])){
echojson_encode(array("success"=>false,'msg'=>"NotgetImgfile"));
return;
}
$upfile=$_FILES['imgFile'];
$name=$upfile["name"];//上传文件的文件名
$type=$upfile["type"];//上传文件的类型
$size=$upfile["size"];//上传文件的大小
$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径
$error_cod=$upfile["error"];
if($error_cod>0){
echojson_encode(array("success"=>false,'msg'=>$error_cod));
}
$ext_file_name="";
switch($type){
case'image/pjpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case'image/jpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case'image/gif':
$okType=true;
$ext_file_name =".gif";
break;
case'image/png':
$okType=true;
$ext_file_name =".png";
break;
}
if(!$okType){
echojson_encode(array("success"=>false,'msg'=>"Notimage"));
return;
}
$web_root="D:".DIRECTORY_SEPARATOR."Easy2PHP5".DIRECTORY_SEPARATOR."webSiteJfz".DIRECTORY_SEPARATOR;
$photo_tmp_path=$web_root."img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp";
$temp_file_name=creat_guid(0).$ext_file_name;
$photo_tmp_file_name=$photo_tmp_path.DIRECTORY_SEPARATOR.$temp_file_name;
$photo_tmp_file_scr="img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR.$temp_file_name;
move_uploaded_file($tmp_name,$photo_tmp_file_name);
echojson_encode(array("success"=>true,'msg'=>"ok","file_name"=>$photo_tmp_file_name,"file_scr"=>$photo_tmp_file_scr));
//echojson_encode(array("success"=>false,'msg'=>json_encode($_FILES['imgFile'])));
return;
?>
guid.class.php//生成唯一的图片文件名
<?
functioncreat_guid($long){
$uuid="";
if(function_exists('com_create_guid')){
$uuid=com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optionalforphp4.2.0anp.
$charid=strtoupper(md5(uniqid(rand(),true)));
$hyphen=chr(45);//"-"
$uuid=chr(123)//"{"
.substr($charid,0,8).$hyphen
.substr($charid,8,4).$hyphen
.substr($charid,12,4).$hyphen
.substr($charid,16,4).$hyphen
.substr($charid,20,12)
.chr(125);//"}"
//return$uuid;
}
if(!isset($long)||$long==0){
returnsubstr($uuid,1,strlen($uuid)-2);
}else{
return$uuid;
}
}
Ⅳ PHP 微信上传永久素材
http请求方式: POST/FORM
http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
调用示例(使用curl命令,用FORM表单方式上传一个多媒体文件):
curl -F media=@test.jpg "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上传图片并且显示的问题,
处理页面的代码:
$file = $_FILES['image']; //得到上传文件的数组,包括文件大小,类型等数组元素
$url = "imgs/".$file['name']; //上传后的图片地址
if(move_uploaded_file($file['tmp_name'],$url)){ //将图片上传到指定路径
//将$url存入数据库,(如果路径是固定的,也可以只把文件名存入数据库,取数据的时候在代码里加上路径就好了)
……
}else{
echo "上传失败";
}
要显示图片就只要从数据库里获取指定图片的路径,然后显示就可以了
Ⅵ php中上传图片的问题
Ⅶ 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;
}