php:自定义文件上传

时间:2018-04-15 13:33:07

标签: javascript php file-upload

按照教程我得到一个基本的php文件上传表格,我是php的新手,请问如何获得五个文件上传表单同时发送五个文件,每个文件都有自己的进度条,但只需一个按钮即可开始全部上传,还可以如何将输出文件重命名为特定名称,并且只能上传指定的MIME类型。 我无法以模块方式使用此代码,以节省代码,无需复制和粘贴所有内容。

非常感谢

HTML

<!DOCTYPE html>
<html>
<head>
<script>
function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php");
    ajax.send(formdata);
}
function progressHandler(event){
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
}
</script>
</head>
<body>
<h2>UPGRADE ADAPTIVE-SETS</h2>
<form id="upload_form" enctype="multipart/form-data" method="post">
  <input type="file" name="file1" id="file1"><br>
  <input type="button" value="Upload File" onclick="uploadFile()">
  <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  <h3 id="status"></h3>
  <p id="loaded_n_total"></p>
</form>
</body>
</html>

PHP

<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if(move_uploaded_file($fileTmpLoc, "incoming/$fileName")){
    echo "$fileName upload is complete";
} else {
    echo "move_uploaded_file function failed";
}
?>

1 个答案:

答案 0 :(得分:0)

<?php
if(isset($_FILES['file']))
{
    $error = $_FILES['file']['error'];
    if($error != 0)
    {
        echo 'Upload error.';
    }
    else
    {
        $size = $_FILES['file']['size'];
        if($size > (1024*1024*3))
        {
            echo 'file < 3MB ';
        }
        else
        {
            $tip = $_FILES['file']['type'];
            $name = $_FILES['file']['name'];
            $extension = explode('.', $name);
            $extension = $extension[count($extension)-1];
            if($extension == 'jpg' || $extension == 'png' || $extension == 'gif')
            {
                if(!file_exists('files'))
                    mkdir('files');
                $file = $_FILES['file']['tmp_name'];
                copy($file, 'files/' . $_FILES['file']['name']);
                echo 'Uploaded ';
            }
            else
            {
                echo 'Just =>JPG,PNG.';
            }
        }
    }
}?>

嗨,例如,此代码中的示例