Jump to content

[SOLVED] How to read a HTML file on a local drive and display it on the browser?


Recommended Posts

Hello,

 

I am new to PHP and need to write a script for my site that will read a HTML file from a user's local drive and display the content on the browser. Basically there will be a HTML form where the user can browse to the file and select it, then hit send. In the next screen I would like to read the file content and display it.

 

Can someone help me out with the code please. Thanks in advance.

 

Lintu

http://www.jigjak.com - A Social Bookmarking and Blogging site

If there will be a form where the user will browse for the file and submit it, ull need to upload the file to your server and then display it.

 

A simple code for uploading:

<form name="form1" enctype="multipart/form-data" method="post" action="">
  <input type="file" name="file" />
  <input type="submit" name="Submit" value="Submit" />
</form>

<?php
if($_FILES['file']['name'] != ""){
$newPath = $_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $newPath)){
		echo "The file was uploaded succesfully";
	} else{
		echo "An error occured while uploading the file";
}
}
?>

 

A simple code for reading the html file and displaying as such:

$html = file('file.html');
foreach($html as $line){
      echo $line;
}

Thank you very much for your reply. I was able to upload the html file and display the content on the browser within a form. Now, after the user has made his selection, I would like to loop through the list and look at the all the checked boxed and add the content for that row into a database, how would I do that? Here is a sample of the html code:

 

<form name="checkboxtest" action="target.html" method="post">
<DL><p>
   <DT><input type="checkbox" name="option2" value=".$row['item_id'] checked><A HREF="http://www.x.com"> Developing Leaders</A>
   <DT><input type="checkbox" name="option2" value=".$row['item_id']><A HREF="http://www.y.com">Goal planning</A>
   <DT><input type="checkbox" name="option2" value=".$row['item_id'] checked><A HREF="http://z.com">Ten Tips on Career Advancement</A>
</DL><p>
<input type="submit" value="Upload my bookmarks">
</form>

 

Thanks again!!!  :)

The only way to loop through (aside from looping through everything in the post array) would be to name them with incrementing numbers. The way you have them named right now, only one will be passed to the php, they need to have different names. I would suggest option1, option2, option3, etc. Then use a loop like:

$i=0;
while (isset($_POST['option' $i])
{
   \\put info in dbase
   $i++;
}

Just add "[]" to the checkbox names

 

<DL><p>
   <DT><input type="checkbox" name="option2[]" value=".$row['item_id'] checked><A HREF="http://www.x.com"> Developing Leaders</A>
   <DT><input type="checkbox" name="option2[]" value=".$row['item_id']><A HREF="http://www.y.com">Goal planning</A>
   <DT><input type="checkbox" name="option2[]" value=".$row['item_id'] checked><A HREF="http://z.com">Ten Tips on Career Advancement</A>
</DL><p>

 

The c/box values are now posted in array. Only checked values are posted.

 

<?php

foreach ($_POST['option2'] as $item_id)
{
     // process the item_id
}

I followed Barand example below and have something like this on my form:

 

for ($i=0; $i< $count; $i++) {
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.x.com"> Developing Leaders</A>
  <input type="checkbox" name="option2[]" value="[$i]"><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.y.com">Goal planning</A>
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://z.com">Ten Tips on Career Advancement</A>
}

 

During processing:

 

<?php 
foreach($_POST['option2'] as $item_id ) 
{ 
echo "Checked:";
echo $item_id;	
echo $_POST['url[$item_id]'];
echo "<br>"; 
} 

 

But as the output I get:

Checked:0

Checked:2

 

The value for the url as not displaying, what am I doing wrong here? Thanks.

for ($i=0; $i< $count; $i++) {
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.x.com"> Developing Leaders</A>
  <input type="checkbox" name="option2[]" value="[$i]"><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.y.com">Goal planning</A>
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://z.com">Ten Tips on Career Advancement</A>
}

 

The value for each of these are going to be the same as they are set to $i, and $i does not change until the next loop starts

Hi Chronister ,

 

I checked the HTML source and the value of $i is changing properly, please see below -

 

<input type="checkbox" name="option2[]" value="0" checked><input name="url[0]" type="hidden" id="url[0]" value="http://www.microsoft.com"><a href="http://www.microsoft.com">ESPN Sports</a>
<br>
<input type="checkbox" name="option2[]" value="1" ><input name="url[1]" type="hidden" id="url[1]" value="http://www.yahoo"><a href="http://www.yahoo">YAHOO.COM</a><br>

<input type="checkbox" name="option2[]" value="2" checked><input name="url[2]" type="hidden" id="url[2]" value="http://www.google.com"><a href="http://www.google.com">Google Online</a><br>

<input name="submit" type="submit" value="Import" />
</form>

huh, guess I am just missing something as it looks to me like the $i "should" not change untill it loops through the next time.

 

<?php
$count=3; // I added this to "test" this code
for ($i=0; $i< $count; $i++) {
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.x.com"> Developing Leaders</A>
  <input type="checkbox" name="option2[]" value="[$i]"><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://www.y.com">Goal planning</A>
   <input type="checkbox" name="option2[]" value="[$i]" checked><input name="url[$i]" type="hidden" id="url[$i]" value="$links[$i]"><A HREF="http://z.com">Ten Tips on Career Advancement</A>
}
?>

 

And either the whole code is not there or I am really missing something because the code as it's posted errors out immediately because your not echoing the inputs and your not closing the php tag to make it html code. Oh well, I am not really adding anything helpful to this post and don't want to "criticize" your code, so good luck

 

 

Nate

 

Thank you everyone for your help...I was able to complete this task.

 

BTW, this site has been very helpful to me while I was building the site, so I have submitted a recommendation for it. So, come by and vote for it.

 

http://www.jigjak.com/modules/weblinks/singlelink.php?lid=189

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.