This code reads a.txt file

<?php 

$file_path = 'a.txt';
$file_handle = fopen($file_path, 'r');
$text_data = fread($file_handle, filesize($file_path));
print $text_data;
fclose($file_handle);

?>

I want to upload this text file using the code given below and want to read the uploaded file when I hit submit button. I know there is a problem in this code that's why it is not working. pls help me to do that.

<html>
<body>

<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Dani AI

Generated

Nice that got it working. For anyone else reading later: was right to point at processing the upload first, but the example had two common issues — the error check should test for success (UPLOAD_ERR_OK, which is 0) instead of > 0, and using filesize() on an uploaded tmp file is brittle. A simpler, safer pattern is to read the temporary upload directly, validate it, then output it escaped (to avoid XSS).

if ($_SERVER['REQUEST_METHOD'] === 'POST'
    && isset($_FILES['file'])
    && $_FILES['file']['error'] === UPLOAD_ERR_OK) {

    $tmp = $_FILES['file']['tmp_name'];

    if (is_uploaded_file($tmp)) {
        $text = file_get_contents($tmp);
        echo nl2br(htmlspecialchars($text, ENT_QUOTES, 'UTF-8'));
    }
}

Notes and quick checks:

  • Confirm the form uses method="post" and enctype="multipart/form-data" (already present in the thread).
  • Use is_uploaded_file() before trusting tmp_name. file_get_contents() avoids relying on filesize().
  • Always escape output with htmlspecialchars() (and nl2br() if preserving line breaks). Raw echo of user files can cause XSS.
  • When saving uploads: validate MIME/type with finfo_file(), enforce size limits, generate a safe unique filename, and store files outside the webroot or deny execution.
  • If uploads fail, inspect $_FILES['file']['error'] (compare against PHP's UPLOADERR* constants) and verify php.ini settings (file_uploads, upload_max_filesize, post_max_size).

This addresses the original read-vs-upload gap while keeping processing safe and reliable.

Recommended Answers

All 2 Replies

you need to process the file first and then read... something like this, but you need to check if the form has been submitted..

    ## define target directory for you the uploaded file
    $file_directory = 'someDirectory';

    if(isset($_POST['submit']) && ($_FILES["file"]["error"] > 0)){

    ## move the uploaded file to your specified location, if there is no problem on submit
     move_uploaded_file($_FILES["file"]["tmp_name"], $file_directory .'/'. $_FILES["file"]["name"]);

     }
     else{

     echo 'No file uploaded at this time';

     }

     ## to read the file
     ## double check and make sure the file exist in file_directory, before opening it

     if (file_exists($file_directory .'/' . $_FILES["file"]["name"])) {
       ## put your text file reader function Here..
  }

Thanks veedeoo. Problem is solved. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.