Txt文件上传与PHP

  • 本文关键字:PHP 文件 Txt php
  • 更新时间 :
  • 英文 :


由于某种原因,下面的代码一直返回"未知错误类型"在脚本中找到。我对图像和视频文件类型都使用了类似的代码,它工作得很好,不知道这个有什么问题。在图像和视频脚本上,我没有收到一次未知错误。我还确保我要上传的文件夹具有适当的权限。

HTML:

<?php
include "documentsconfig.php";
if(!isset($_SESSION['uname'])){
    header('Location: http://ts558.cloud/index.php');
}
?>
<?php
  if ($handle = opendir('allDocs')) {
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
        $thelist .= '<li><a download href="allDocs/'.$file.'">'.$file.'</a></li>';
      }
    }
    closedir($handle);
  }
?>
<html>
    
    <head>
        <title>Cloud</title>
        <link href="documents.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div class="container">
            <form method="post" action="">
                <div id="div_options">
                    <h1> Your Documents</h1>
                        <ol><?php echo $thelist; ?></ol>
                </div>
                <br>
            </form>
        </div>
        <div class="container">
            <form action="docsUploadScript.php" method="post" enctype="multipart/form-data">
                <div id="div_upload">
                <h1>Upload a Document File</h1>
                        <input type="file" name="document" id="fileSelect">
                        <br>
                        <input type="submit" name="submit" value="Upload" id="uploadButton">
                        <p> -Or-</p>
                        <a href="../../home.php">
                            <input type="button" name="home" value="Back to Options" id="home"?
                        </a>
                        <br>
                </div>  
            </form>
        </div>
    </body>
</html>
PHP:

<?php
include "documentsconfig.php";
if(!isset($_SESSION['uname'])){
    header('Location: http://ts558.cloud/index.php');
}
?>
<?php
header( "Refresh: 5; url=documents.php");
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(isset($_FILES["document"]) && $_FILES["document"]["error"] == 0){
        $allowed = array("txt" => "document/txt");
        $filename = $_FILES["document"]["name"];
        $filetype = $_FILES["document"]["type"];
        $filesize = $_FILES["document"]["size"];
    
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("File type not supported. Redirecting back in 5 Seconds");
    
        if(in_array($filetype, $allowed)){
            if(file_exists("allDocs/" . $filename)){
                echo $filename . "is already on server. Redirecting back in 5 Seconds";
            } else{
                move_uploaded_file($_FILES["document"]["tmp_name"], "allDocs/" . $filename);
                echo "File upload was successful. Redirecting back in 5 Seconds";
            } 
        } else{
            echo "Uknown Error. Please try again. Redirecting back in 5 Seconds"; 
        }
    } else{
        echo "Error type:" . $_FILES["document"]["error"];
    }
}
?>

您在这里做错的是您添加了错误的mime文件类型将document/text更改为text/plain

Mime文件类型

        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',
        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',
        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',
        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',
        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',
        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',

源这里

最新更新