Maison php教程 PHP源码 更改xhEditor 上传文件upload.php

更改xhEditor 上传文件upload.php

May 25, 2016 pm 04:58 PM

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

PHP代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

<?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;

}

?>

Copier après la connexion
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)