lintu Posted August 21, 2007 Share Posted August 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/ Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 I don't think you can do that with PHP without uploading it to the server. You should be able to do it with javascript, but it might require an activeX control. Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-330307 Share on other sites More sharing options...
Fadion Posted August 22, 2007 Share Posted August 22, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-330520 Share on other sites More sharing options...
lintu Posted August 24, 2007 Author Share Posted August 24, 2007 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-333256 Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-333258 Share on other sites More sharing options...
Barand Posted August 24, 2007 Share Posted August 24, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-333366 Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 Whoa that's cool, I didn't know that worked. Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-333372 Share on other sites More sharing options...
lintu Posted August 26, 2007 Author Share Posted August 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-334351 Share on other sites More sharing options...
chronister Posted August 26, 2007 Share Posted August 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-334361 Share on other sites More sharing options...
lintu Posted August 26, 2007 Author Share Posted August 26, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-334375 Share on other sites More sharing options...
trq Posted August 26, 2007 Share Posted August 26, 2007 <?php foreach($_POST['option2'] as $item_id) { echo "Checked:"; echo $item_id; echo $_POST['url'][$item_id]; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-334377 Share on other sites More sharing options...
chronister Posted August 26, 2007 Share Posted August 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-334380 Share on other sites More sharing options...
lintu Posted August 28, 2007 Author Share Posted August 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66046-solved-how-to-read-a-html-file-on-a-local-drive-and-display-it-on-the-browser/#findComment-335951 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.