Home > php教程 > PHP源码 > body text

更改xhEditor 上传文件upload.php

PHP中文网
Release: 2016-05-25 16:58:46
Original
1434 people have browsed it

更改xhEditor  上传文件upload.php
把上传完的图片路径改为 http 路径

PHP代码

<?php
/*!
 
 * 注2:本程序特别针对HTML5上传,加入了特殊处理
 */
header(&#39;Content-Type: text/html; charset=UTF-8&#39;);
//------定义http路径
$url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER["REQUEST_URI"]; 
$dir_url_http=dirname($url).&#39;/&#39;;
//-------------------------------------
$inputName=&#39;filedata&#39;;//表单文件域name
$attachDir=&#39;upload&#39;;//上传文件保存路径,结尾不要带/
$dirType=1;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
$maxAttachSize=2097152;//最大上传大小,默认是2M
$upExt=&#39;txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid&#39;;//上传扩展名
$msgType=2;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET[&#39;immediate&#39;])?$_GET[&#39;immediate&#39;]:0;//立即上传模式,仅为演示用
ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);//时区
 
$err = "";
$msg = "&#39;&#39;";
$tempPath=$attachDir.&#39;/&#39;.date("YmdHis").mt_rand(10000,99999).&#39;.tmp&#39;;
$localName=&#39;&#39;;
 
if(isset($_SERVER[&#39;HTTP_CONTENT_DISPOSITION&#39;])&&preg_match(&#39;/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i&#39;,$_SERVER[&#39;HTTP_CONTENT_DISPOSITION&#39;],$info)){//HTML5上传
    file_put_contents($tempPath,file_get_contents("php://input"));
    $localName=urldecode($info[2]);
}
else{//标准表单式上传
    $upfile=@$_FILES[$inputName];
    if(!isset($upfile))$err=&#39;文件域的name错误&#39;;
    elseif(!empty($upfile[&#39;error&#39;])){
        switch($upfile[&#39;error&#39;])
        {
            case &#39;1&#39;:
                $err = &#39;文件大小超过了php.ini定义的upload_max_filesize值&#39;;
                break;
            case &#39;2&#39;:
                $err = &#39;文件大小超过了HTML定义的MAX_FILE_SIZE值&#39;;
                break;
            case &#39;3&#39;:
                $err = &#39;文件上传不完全&#39;;
                break;
            case &#39;4&#39;:
                $err = &#39;无文件上传&#39;;
                break;
            case &#39;6&#39;:
                $err = &#39;缺少临时文件夹&#39;;
                break;
            case &#39;7&#39;:
                $err = &#39;写文件失败&#39;;
                break;
            case &#39;8&#39;:
                $err = &#39;上传被其它扩展中断&#39;;
                break;
            case &#39;999&#39;:
            default:
                $err = &#39;无有效错误代码&#39;;
        }
    }
    elseif(empty($upfile[&#39;tmp_name&#39;]) || $upfile[&#39;tmp_name&#39;] == &#39;none&#39;)$err = &#39;无文件上传&#39;;
    else{
        move_uploaded_file($upfile[&#39;tmp_name&#39;],$tempPath);
        $localName=$upfile[&#39;name&#39;];
    }
}
 
if($err==&#39;&#39;){
    $fileInfo=pathinfo($localName);
    $extension=$fileInfo[&#39;extension&#39;];
    if(preg_match(&#39;/^(&#39;.str_replace(&#39;,&#39;,&#39;|&#39;,$upExt).&#39;)$/i&#39;,$extension))
    {
        $bytes=filesize($tempPath);
        if($bytes > $maxAttachSize)$err=&#39;请不要上传大小超过&#39;.formatBytes($maxAttachSize).&#39;的文件&#39;;
        else
        {
            switch($dirType)
            {
                case 1: $attachSubDir = &#39;day_&#39;.date(&#39;ymd&#39;); break;
                case 2: $attachSubDir = &#39;month_&#39;.date(&#39;ym&#39;); break;
                case 3: $attachSubDir = &#39;ext_&#39;.$extension; break;
            }
            $attachDir = $attachDir.&#39;/&#39;.$attachSubDir;
            if(!is_dir($attachDir))
            {
                @mkdir($attachDir, 0777);
                @fclose(fopen($attachDir.&#39;/index.htm&#39;, &#39;w&#39;));
            }
            PHP_VERSION < &#39;4.2.0&#39; && mt_srand((double)microtime() * 1000000);
            $newFilename=date("YmdHis").mt_rand(1000,9999).&#39;.&#39;.$extension;
            $targetPath = $attachDir.&#39;/&#39;.$newFilename;
             
            rename($tempPath,$targetPath);
            @chmod($targetPath,0755);
            $targetPath=jsonString($targetPath);
            if($immediate==&#39;1&#39;)$targetPath=&#39;!&#39;.$targetPath;
            if($msgType==1)$msg="&#39;$targetPath&#39;";
             
            else $msg="{&#39;url&#39;:&#39;".$dir_url_http.$targetPath."&#39;,&#39;localname&#39;:&#39;".jsonString($localName)."&#39;,&#39;id&#39;:&#39;1&#39;}";//id参数固定不变,仅供演示,实际项目中可以是数据库ID
        }
    }
    else $err=&#39;上传文件扩展名必需为:&#39;.$upExt;
 
    @unlink($tempPath);
}
 
echo "{&#39;err&#39;:&#39;".jsonString($err)."&#39;,&#39;msg&#39;:".$msg."}";
 
 
function jsonString($str)
{
    return preg_replace("/([\\\\\/&#39;])/",&#39;\\\$1&#39;,$str);
}
function formatBytes($bytes) {
    if($bytes >= 1073741824) {
        $bytes = round($bytes / 1073741824 * 100) / 100 . &#39;GB&#39;;
    } elseif($bytes >= 1048576) {
        $bytes = round($bytes / 1048576 * 100) / 100 . &#39;MB&#39;;
    } elseif($bytes >= 1024) {
        $bytes = round($bytes / 1024 * 100) / 100 . &#39;KB&#39;;
    } else {
        $bytes = $bytes . &#39;Bytes&#39;;
    }
    return $bytes;
}
?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!