php+html+js+ajax实现文件上传

php+html+js+ajax实现文件上传

php+html+js+ajax实现文件上传_第1张图片

目录

一、表单单文件上传

1、上传页面

2、接受文件上传php

二、表单多文件上传

1、上传页面

2、接受文件上传php 

三、表单异步xhr文件上传

1、上传页面

2、接受文件上传php  

四、表单异步ajax文件上传 

1、上传页面

2、接受文件上传php   


一、表单单文件上传
1、上传页面




    文件上传


文件上传

2、接受文件上传php
 500000) {
        echo "对不起,文件太大.";
        $uploadOk = 0;
    }
    // 允许上传的文件格式
    $allowedExtensions = array("jpg", "jpeg", "png", "gif");
    if (!in_array($imageFileType, $allowedExtensions)) {
        echo "对不起,仅允许上传 JPG, JPEG, PNG 和 GIF 文件.";
        $uploadOk = 0;
    }
    // 检查上传状态
    if ($uploadOk == 0) {
        echo "对不起,文件上传失败.";
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
            echo "文件上传成功.";
        } else {
            echo "对不起,文件上传失败.";
        }
    }
}
?>

 

二、表单多文件上传
1、上传页面




    文件上传示例


2、接受文件上传php 
";
                } else {
                    echo "文件上传失败: " . $file_name . "
"; } } else { echo "文件上传错误: " . $file_name . "
"; } } } else { echo "没有选择要上传的文件"; } } ?>
三、表单异步xhr文件上传
1、上传页面




    文件上传


文件上传

2、接受文件上传php  
 $tmp_name) {
        $targetFile = $targetDir . $_FILES['files']['name'][$key];
        $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION)); // 上传文件的扩展名
        // 检查文件是否已存在
        if (file_exists($targetFile)) {
            echo "对不起,文件已存在.";
            $uploadOk = 0;
        }
        // 允许上传的文件格式
        $allowedExtensions = array("jpg", "jpeg", "png", "gif");
        if (!in_array($imageFileType, $allowedExtensions)) {
            echo "对不起,仅允许上传 JPG, JPEG, PNG 和 GIF 文件.";
            $uploadOk = 0;
        }
        // 检查上传状态
        if ($uploadOk == 0) {
            echo "对不起,文件上传失败.";
        } else {
            if (move_uploaded_file($tmp_name, $targetFile)) {
                echo "文件上传成功.";
            } else {
                echo "对不起,文件上传失败.";
            }
        }
    }
}
?>
四、表单异步ajax文件上传 
1、上传页面


    

    文件上传示例
    


2、接受文件上传php   

你可能感兴趣的:(php,html,js,php,开发语言,数据库)