php 文件上传

uploadForm.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="upload_file.php" method="post" enctype="multipart/form-data">
            <label for="fileName">fileName:</label>
            <input type="file" id="fileName" name="file" /><br />
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

upload_file.php

<?php
    //判断文件的类型,用户只能上传gif和jpeg的文件
    if((($_FILES['file']['type'] == 'image/gif')||($_FILES['file']['type'] == 'image/jpeg')||($_FILES['file']['type'] == 'image/pjpeg'))&&($_FILES['file']['size']<2000000)){
        if($_FILES['file']['error'] > 0){
            echo "Error:" . $FILES['file']['error']. '<br/>';
        }else{
            echo 'Upload: ' . $_FILES['file']['name'].'<br />';
            echo 'Type: ' . $_FILES['file']['type'] . '<br />';
            echo 'Size: ' . ($_FILES['file']['size']/1024).'Kb <br/>';
            echo 'Stored in: ' . $_FILES['file']['tmp_name'] . '<br />';
            
            //判断是否存在
            if(file_exists('upload/' . $_FILES['file']['name'])){
                echo $_FILES['file']['name']. ' is exist';
            }else{
                move_uploaded_file($_FILES['file']['tmp_name'] , 'upload/'.$_FILES['file']['name']);
                echo 'successed';
            }
        }
    }else{
        echo 'fileType error or fileSize error';
    }
?>
原文地址:https://www.cnblogs.com/xudy/p/6063801.html