Home > Backend Development > PHP Tutorial > PHP avatar upload, php avatar upload_PHP tutorial

PHP avatar upload, php avatar upload_PHP tutorial

WBOY
Release: 2016-07-13 10:10:32
Original
1414 people have browsed it

PHP avatar upload, php avatar upload

Hee hee, I have been lazy since Christmas. I have been too busy these days and it feels like I haven’t been there for a long time. Come out and bubble, 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?

Is JavaScript the front-end?

Do you think jQuery is the front-end?

Does Ajax work as a front-end?

Are various frameworks front-end?

In short, now I feel more and more that I am no longer a front-end person. I have learned a lot and used it in various ways. Is this the fate of the front-end?

Website front-end:

The front-end for a website 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> 
Copy after login


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>

?>
Copy after login

Simply define the format and other parameters for image upload,

3. Specific judgment methods

<?<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>?>  
Copy after login

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/935348.htmlTechArticlePHP avatar upload, php avatar upload Hehe, I have been lazy since Christmas, and these days also because It's because I'm too busy, and I feel like I haven't come out for a long time, eh... because...
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template