Home  >  Article  >  Backend Development  >  PHP avatar upload

PHP avatar upload

WBOY
WBOYOriginal
2016-08-08 09:31:531084browse

Hee hee hee, since Christmas, I have been lazy. Because of being too busy these days, I feel that I haven’t come out for a long time, eh ...

I have been struggling for life. As a front-end development engineer, I am becoming more and more confused. I don’t know who I am now.

Will html5 and css3 be the front end?

Do you know PHP as a front-end?

Will smarty be the front end?

Do you know JavaScript as a front-end?

Do you know jQuery is the front-end?

Does Ajax work as a front-end?

是 Will various frameworks are the front end?

                                                                                                                                                                                          are now increasingly felt that I am not a front-end person anymore. I have learned a lot and used it in various ways. Is this the fate of the front-end?

Website front-end:

                 For a website, the front-end usually refers to the front-end part of the website including the presentation layer and structural layer of the website. Therefore, front-end technology is generally divided into front-end design and front-end development. Front-end design can generally be understood as the visual design of the website, and front-end development is the front-end code implementation of the website, including basic HTML, CSS and JavaScript/ajax. Now the latest advanced version is HTML5, CSS3, and SVG, etc.

I recently came into contact with some PHP stuff and it feels pretty good, so I’d better share it with you!

php avatar upload:

1, html

<span><</span><span>html</span><span>></span>  
<span><</span><span>head</span><span>></span>  
<span><</span><span>title</span><span>></span>图片上传<span></</span><span>title</span><span>></span>  
<span><</span><span>style </span><span>type</span><span>="text/css"</span><span>></span><span> 
body  
    </span><span>{</span><span>font-size</span><span>:</span><span> 14px</span><span>;</span><span>}</span>  
<span></</span><span>style</span><span>></span>  
<span></</span><span>head</span><span>></span>  
  
<span><</span><span>body</span><span>></span>  
<span><</span><span>form </span><span>enctype</span><span>="multipart/form-data"</span><span> method</span><span>="post"</span><span> name</span><span>="upform"</span><span>></span><span>  
  上传文件:  
  </span><span><</span><span>input </span><span>name</span><span>="upfile"</span><span> type</span><span>="file"</span><span>></span>  
  <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="上传"</span><span>><</span><span>br</span><span>></span>  
   
<span></</span><span>form</span><span>></span> 


2. php upload avatar

<?<span>php
 </span><span>//</span><span>上传文件类型列表  </span>
<span>$uptypes</span>=<span>array</span><span>(  
    </span>'image/jpg',  
    'image/jpeg',  
    'image/png',  
    'image/pjpeg',  
    'image/gif',  
    'image/bmp',  
    'image/x-png'<span>  
);

</span><span>$max_file_size</span>=2000000;     <span>//</span><span>上传文件大小限制, 单位BYTE  </span>
<span>$destination_folder</span>="uploadimg/"; <span>//</span><span>上传文件路径  </span>
<span>$cun</span>="../../images/uploadimg/"<span>;
</span><span>$imgpreview</span>=1;      <span>//</span><span>是否生成预览图(1为生成,其他为不生成);  </span>
<span>$imgpreviewsize</span>=1/2;    <span>//</span><span>缩略图比例  </span>

?>

Simply define the format and other parameters for image upload,

3. Specific judgment method

<?<span>php  
</span><span>if</span> (<span>$_SERVER</span>['REQUEST_METHOD'] == 'POST'<span>)  
 {  
     </span><span>if</span> (!<span>is_uploaded_file</span>(<span>$_FILES</span>["upfile"<span>][tmp_name]))  
     </span><span>//</span><span>是否存在文件  </span>
<span>     {  
         </span><span>echo</span> "图片不存在!"<span>;  
         </span><span>exit</span><span>;  
     }  
  
    </span><span>$file</span> = <span>$_FILES</span>["upfile"<span>];  
    </span><span>if</span>(<span>$max_file_size</span> < <span>$file</span>["size"<span>])  
    </span><span>//</span><span>检查文件大小  </span>
<span>    {  
        </span><span>echo</span> "文件太大!"<span>;  
        </span><span>exit</span><span>;  
    }  
  
    </span><span>if</span>(!<span>in_array</span>(<span>$file</span>["type"], <span>$uptypes</span><span>))  
    </span><span>//</span><span>检查文件类型  </span>
<span>    {  
        </span><span>echo</span> "文件类型不符!".<span>$file</span>["type"<span>];  
        </span><span>exit</span><span>;  
    }  
  
    </span><span>if</span>(!<span>file_exists</span>(<span>$destination_folder</span><span>))  
    {  
        </span><span>mkdir</span>(<span>$destination_folder</span><span>);  
    }  
  
    </span><span>$filename</span>=<span>$file</span>["tmp_name"<span>];  
    </span><span>$image_size</span> = <span>getimagesize</span>(<span>$filename</span><span>);  
    </span><span>$pinfo</span>=<span>pathinfo</span>(<span>$file</span>["name"<span>]);  
    </span><span>$ftype</span>=<span>$pinfo</span>['extension'<span>];  
    </span><span>$destination</span> = <span>$destination_folder</span>.<span>time</span>().".".<span>$ftype</span><span>;  
    </span><span>if</span> (<span>file_exists</span>(<span>$destination</span>) && <span>$overwrite</span> != <span>true</span><span>)  
    {  
        </span><span>echo</span> "同名文件已经存在了"<span>;  
        </span><span>exit</span><span>;  
    }  
  
    </span><span>if</span>(!<span>move_uploaded_file</span> (<span>$filename</span>, <span>$destination</span><span>))  
    {  
        </span><span>echo</span> "移动文件出错"<span>;  
        </span><span>exit</span><span>;  
    }  
  
    </span><span>if</span>(<span>$imgpreview</span>==1<span>)  
    {  
    </span><span>echo</span> "<br>图片预览:<br>"<span>;  
    </span><span>echo</span> "<img src=\"".<span>$destination</span>."\" width=".(<span>$image_size</span>[0]*<span>$imgpreviewsize</span>)." height=".(<span>$image_size</span>[1]*<span>$imgpreviewsize</span><span>);  
    </span><span>echo</span> " alt=\"图片预览:\r文件名:".<span>$destination</span>."\r上传时间:\">"<span>;  
    }  
  }
</span>?>  

Anyway, it’s just a blind cat encountering a dead mouse, and the result is such a willful effect, hehe, the storage path of the generated image is a key, just pay attention to it!

The above introduces PHP avatar uploading, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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