Error while process the file upload in PHP - Hack The Tech - Latest News related to Computer and Technology

logo

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, June 10, 2021

demo-image

Error while process the file upload in PHP

I have the following form in HTML

<form action="" method="post">       
       <fieldset>
            <div>
                 <label for="data">Upload your file or <br> 
                                just click the submit button if you want to read the default file</label><br>
            <div><!--empty div--></div>
            </div>
            <div>
                 <input type="file" name="data" id="data" value="">
                 <div><!--empty div--></div>
            </div>
            <br>
                  <input type="submit" name="submit" value="Submit File">                                
       </fieldset>                                                                
                    
</form>

I want to check whether the file is uploaded and in .txt or not. However, after I chose the file and clicked submit, my website display an error that no file was uploaded. Here my PHP code:

if(!isset($_FILES['data']) || $_FILES['data']['error'] == UPLOAD_ERR_NO_FILE){
        echo "<div class=\"highlight\">
                    <p>Please fix the following errors:</p>
                        <ul>
                            <li>No file was uploaded. Error code 0</li>
                            <li>We will display the data we have in the server instead!</li>
                        </ul>                                        
                </div>
                <br>";
    }
    else{
        if(is_uploaded_file($_FILES['data']['tmp_name'])){
            if($_FILES['data']['type'] != "plain/text"){
                echo "<div class=\"highlight\">
                            <p>Please fix the following errors:</p>
                            <ul>
                                <li>The data file must be in comma separated.</li>
                                <li>Error 2</li>
                            </ul>                                        
                        </div>
                        <br>";
            } 
        }

    }

What is wrong with my code and how to fix it?



source https://stackoverflow.com/questions/67910194/error-while-process-the-file-upload-in-php

No comments:

Post a Comment