We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,397 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

read file simple problem

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>
2
Contributors
2
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
3
Views
Question
Answered
rubai
Light Poster
28 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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..
  }
veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

Thanks veedeoo. Problem is solved. :-)

rubai
Light Poster
28 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by veedeoo

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0777 seconds using 2.71MB