• curl文件上传类


    <form action="index.php" method="post" enctype='multipart/form-data' >
         <input type="file" name="path" />
         <input type="submit">
    </form>
    <?php include 'imageUpload.php'; $tool=new imageUpload(); if(isset($_FILES["path"])&&$_FILES["path"]["error"]==UPLOAD_ERR_OK ){ $file=$_FILES["path"]["tmp_name"]; $ext=$tool->getExt($_FILES["path"]["name"]); $svaePath="/www/znm/images/"; $result=$tool->uploadFile($file,$ext,$svaePath); $result= json_decode($result,1); if($result["code"]==1){ echo "上传成功,上传后的图片名称为{$result["name"]}"; }else{ echo "{$result["error"]}"; } }else{ echo "上传失败"; } ?> 客户端上传类 <?php /** * Description of imageUpload * 图片上传类 * @author Xiang dongdong<xiangdong198719@gmail.com> */ class imageUpload { /** * 默认图片服务器 * @var type */ private $url = "http://smallgame.com/acceptFile.php"; /** * 图片上传图片服务器 返回图片名称 * @param type $file 要上传的文件 * @param type $ext 文件扩展名 * @param type $savePath 保存路径 * @return string 返回新的文件名 失败返回false */ public function uploadFile($file, $ext, $savePath) { $fields['path'] = '@' . $file; $fields['savePath'] = $savePath; $fields['ext'] = $ext; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_POST, 1); //使返回结果为字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); $result=curl_exec($ch); //var_dump($result);exit; if ($error = curl_error($ch)) { die($error); } curl_close($ch); return $result; } /** * 获取文件扩展名 * @param type $path * @return type */ public function getExt($path) { $info = pathinfo($path); $ext = $info["extension"]; return $ext; } /** * 判断文件是否是可上传文件 * @param type $filename * @return boolean */ function isAllowUpload($filename) { $file = fopen($filename, "rb"); $bin = fread($file, 2); //只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']); $fileType = ''; switch ($typeCode) { case 7790: $fileType = 'exe'; break; case 7784: $fileType = 'midi'; break; case 8297: $fileType = 'rar'; break; case 8075: $fileType = 'zip'; break; case 255216: $fileType = 'jpg'; break; case 7173: $fileType = 'gif'; break; case 6677: $fileType = 'bmp'; break; case 13780: $fileType = 'png'; break; default: $fileType = 'unknown: ' . $typeCode; } //Fix if ($strInfo['chars1'] == '-1' AND $strInfo['chars2'] == '-40'){ $fileType='jpg'; } if ($strInfo['chars1'] == '-119' AND $strInfo['chars2'] == '80'){ $fileType='png'; } if(in_array($fileType, $allowUpload)){ return true; }else{ return false; } } } 服务端用户接受和移动图片 <?php include 'imageUploadServer.php'; $server = new imageUploadServer(); if ($_FILES["path"]["error"] == UPLOAD_ERR_OK) { $svaePath = $_POST["savePath"]; $ext = $_POST["ext"]; $result = $server->moveFile($_FILES["path"]["tmp_name"], $ext, $svaePath); echo json_encode($result); } else { $result["code"] = 4; $result["error"] = "上传服务器过程中出错!"; echo json_encode($result); } 服务端图片处理类 <?php /** * Description of imageUploadServer * 图片服务器服务端 * @author Xiang dongdong<xiangdong198719@gmail.com> */ class imageUploadServer { /** * 允许上传的文件类型 * @var type */ private $allowUpload=array("jpg","gif","png"); /** * 文件移动 * @param type $file 上传的文件 * @param type $ext 文件扩展名 * @param type $svaePath 文件保存目录 * @return int */ public function moveFile($file,$ext, $svaePath) { if($this->isAllowUpload($file)){ $name = md5($this->getRoundNumber() . time()) . "." . $ext; //move_uploaded_file($_FILES["path"]["tmp_name"], "{$svaePath}{$name}"); if(move_uploaded_file($_FILES["path"]["tmp_name"], "{$svaePath}{$name}")){ $result["code"]=1; $result["name"]=$name; }else{ $result["code"]=2; $result["error"]="移动文件失败!"; } }else{ $result["code"]=3; $result["error"]="上传的文件类型非法"; } return $result; } /** * 生成6位随机字符串 * @return string */ private function getRoundNumber() { $randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); $rand = substr($randStr, 0, 6); return $rand; } /** * 判断文件是否是可上传文件 * @param type $filename * @return boolean */ function isAllowUpload($filename) { $file = fopen($filename, "rb"); $bin = fread($file, 2); //只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']); $fileType = ''; switch ($typeCode) { case 7790: $fileType = 'exe'; break; case 7784: $fileType = 'midi'; break; case 8297: $fileType = 'rar'; break; case 8075: $fileType = 'zip'; break; case 255216: $fileType = 'jpg'; break; case 7173: $fileType = 'gif'; break; case 6677: $fileType = 'bmp'; break; case 13780: $fileType = 'png'; break; default: $fileType = 'unknown: ' . $typeCode; } //Fix if ($strInfo['chars1'] == '-1' AND $strInfo['chars2'] == '-40') { $fileType = 'jpg'; } if ($strInfo['chars1'] == '-119' AND $strInfo['chars2'] == '80') { $fileType = 'png'; } if (in_array($fileType, $this->allowUpload)) { return true; } else { return false; } } /** * 获取文件扩展名 * @param type $path * @return type */ public function getExt($path) { $info = pathinfo($path); $ext = $info["extension"]; return $ext; } }
  • 相关阅读:
    10个很有用的高级Git命令
    25套用于Web UI设计的免费PSD网页元素模板
    101个MySQL 的调节和优化的提示
    10款最新且超实用的开发框架
    30 个有用的 HTML5 和 CSS3 表单设计
    cetos7最小化安装设置网络启动和更新yum源
    百度地图api开发:根据坐标获得地理描述地址
    防止sql注入的函数addslashes()
    jquery使用ajax提交form表单
    Git常用命令
  • 原文地址:https://www.cnblogs.com/biggerCoder/p/5069449.html
Copyright © 2020-2023  润新知