search
Homephp教程php手册PHP接收上传文件

PHP接收上传文件

Jun 13, 2016 am 09:45 AM
phpupload filestake over

前言

这星期一直再搞php,涉及到文件上传的部分有些遗忘,这里记录一下

HTML的form表单

用html的表单模拟一个文件上传的post请求,代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload</title>
</head>
<body>




</body>
</html>

注意:
要确保文件上传表单的属性是 enctype="multipart/form-data",否则文件上传不了

PHP

首先,需要解释一下PHP的全局变量$_FILES,此数组包含了所有上传的文件信息
$_FILE['userfile']['name'] : 客户端机器文件的原名称
$_FILE['userfile']['type'] : 文件的MIME类型
$_FILE['userfile']['size'] : 已上传的文件大小
$_FILE['userfile']['tmpname'] : 文件被上传后在服务器存储的临时文件名
$_FILE['userfile']['error'] : 和该文件上传的错误代码

思路

1、生成40位的随机字符串作为文件名 2、根据文件是图片还是语音转存到不同的文件位置 3、暂时不做文件大小和文件类型的校验
function processFile($files, $type) {
    $uploadName = null;
    foreach ($files as $name => $value) {
        $originalName = $value['name'];
        $arr = explode(".", $originalName);
        $postfix = $arr[count($arr) - 1];
        $tmpPath = $value['tmp_name'];
        $tmpType = $value['type'];
        $tmpSize = $value['size'];
    }
    
    $newname = EhlStaticFunction::generateRandomStr(40).".".$postfix;
    
    switch ($type) {
        case 1 : 
            // 处理声音文件
            $destination = VIDEOUPLOADDIR.$newname;
            break;
        case 2 :
            // 处理图像文件
            $destination = IMAGEUPLOADDIR.$newname;
            break;
    }
    
    move_uploaded_file($tmpPath, $destination);
}


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use