Home > OS >  Txt File Upload With PHP
Txt File Upload With PHP

Time:04-16

For some reason the following code keeps returning the "Unknown Error Type" found in the script. I have used similar code for both image and video file types and it worked fine, not sure what is wrong with this one. On the image and video scripts I did not receive the unknown error once. I have also made sure the folder that I am uploading to has the proper permissions.

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 >
            <form method="post" action="">
                <div id="div_options">
                    <h1> Your Documents</h1>
                        <ol><?php echo $thelist; ?></ol>
                </div>
                <br>
            </form>
        </div>
        <div >
            <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"];
    }
}
?>

CodePudding user response:

What you have done wrong here is you've added a wrong mime file type change document/text to text/plain

Mime file types:

        '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',

Source here

  •  Tags:  
  • php
  • Related