编程语言
首页 > 编程语言> > php – 将excel文件上传到服务器

php – 将excel文件上传到服务器

作者:互联网

我无法将msoffice文件上传到服务器上的文件夹,我可以上传.pdf和图片文件,但不能上传.docx,.xlsx甚至.txt,我的所有语法看起来都是正确的.这是表格

<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="fileSelect">Navigate and choose:</label>
    <input type="file" name="file" id="fileSelect"><br><br>
    <input class="button" type="submit" name="action" value="Upload to Shared Folder">
</form>

这是upload_file.php

    if(isset($_FILES["file"]["error"])){
if($_FILES["file"]["error"] > 0){
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else{
    $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/msword", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.ms-excel", "pdf" => "application/pdf", "txt" => "application/txt");
    $filename = $_FILES["file"]["name"];
    $filetype = $_FILES["file"]["type"];
    $filesize = $_FILES["file"]["size"];

    // siati faaopoopo
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");

    // siati fua - 10MB
    $maxsize = 200000 * 60;
    if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");

    // siati MYME 
    if(in_array($filetype, $allowed)){
        // Check whether file exists before uploading it
        if(file_exists("general/" . $_FILES["file"]["name"])){
            echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
        } else{
            move_uploaded_file($_FILES["file"]["tmp_name"], "general/" . $_FILES["file"]["name"]);
            echo "The file was uploaded successfully.</br></br>";
        } 
    } 
    else{
        echo "Error: There was a problem uploading the file - please try again."; 
    }
}
} else{
echo "Error: Invalid parameters - something is very very very wrong with this upload.";
}

我必须遗漏一些东西,但还没弄清楚是什么.我已经尝试在stackexchange和google上查找application / msword和东西,但这一切似乎都是正确的.我得到的错误是:

    Error: There was a problem uploading the file - please try again.

请协助

解决方法:

您可能检查错误的mimetype,尝试转储文件无法上传的所有mimetypes.

以下是ms office mimetypes的一个小概述:office_mime_types

如果您想让文件检查更好更安全,可以像这样检查.

$allowed = array(
    "xls" => array( "application/vnd.ms-excel" ),
    "xlsx" => array(
        "application/vnd.ms-excel",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    )
);

// replace this
if ( in_array( $filetype, $allowed ) ) {}

// with this
// this checks also if mimetype of xlsx is just a mimetype of xlsx and nothing else
if ( isset( $allowed[$ext] ) && in_array( $filetype, $allowed[$ext] ) ) {}

标签:php,excel,upload,ms-office
来源: https://codeday.me/bug/20190829/1763120.html