By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

How to upload file and submit button to other page (PHP)

P: 15
I am trying to make a site available for someone to *upload* certain type of text file (.txt) that can be parsed by written code. the upload file is available on the main site but I need this to result in different page on the right as someone clicks on SUBMIT with one's file attached. The uploaded file by clicking submit button should be PARSED on the right side of the page but when I click on the submit with the file now, it goes to the local host main page. I don't know what I have to do. This is what I currently have on the website, below picture!



the code is what I currently have for the THEME folder for Drupal page. Line 39-45 is what I wrote for the uploading file part.


Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. // $Id: page.tpl.php,v 1.28.2.1 2009/04/30 00:13:31 goba Exp $ 
  3. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language->language ?>" xml:lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>"> 
  5.  
  6. <head> 
  7.   <?php print $head ?> 
  8.   <title><?php print $head_title ?></title> 
  9.   <?php print $styles ?> 
  10.   <?php print $scripts ?> 
  11.   <script type="text/javascript"><?php /* Needed to avoid Flash of Unstyle Content in IE */ ?> </script> 
  12. </head> 
  13.  
  14. <body> 
  15.  
  16. <table border="0" cellpadding="0" cellspacing="0" id="header"> 
  17.   <tr> 
  18.     <td id="logo"> 
  19.       <?php if ($logo) { ?><a href="<?php print $front_page ?>" title="<?php print t('Home') ?>"><img src="<?php print $logo ?>" alt="<?php print t('Home') ?>" /></a><?php } ?> 
  20.       <?php if ($site_name) { ?><h1 class='site-name'><a href="<?php print $front_page ?>" title="<?php print t('Home') ?>"><?php print $site_name ?></a></h1><?php } ?> 
  21.       <?php if ($site_slogan) { ?><div class='site-slogan'><?php print $site_slogan ?></div><?php } ?> 
  22.     </td> 
  23.     <td id="menu"> 
  24.       <?php if (isset($secondary_links)) { ?><?php print theme('links', $secondary_links, array('class' => 'links', 'id' => 'subnavlist')) ?><?php } ?> 
  25.       <?php if (isset($primary_links)) { ?><?php print theme('links', $primary_links, array('class' => 'links', 'id' => 'navlist')) ?><?php } ?> 
  26.       <?php print $search_box ?> 
  27.     </td> 
  28.   </tr> 
  29.   <tr> 
  30.     <td colspan="2"><div><?php print $header ?></div></td> 
  31.   </tr> 
  32. </table> 
  33.  
  34. <table border="0" cellpadding="0" cellspacing="0" id="content"> 
  35.   <tr> 
  36.     <?php if ($left) { ?><td id="sidebar-left"> 
  37.       <?php print $left ?> 
  38.  
  39. <form action="test_0512.php" method="post" 
  40. enctype="multipart/form-data"> 
  41. <label for="file">Upload the file: </label> 
  42. <input type="file" name="file" id="file" />  
  43. <br /> 
  44. <input type="submit" name="submit" value="Submit" /> 
  45. </form> 
  46.  
  47.  
  48.     </td><?php } ?> 
  49.     <td valign="top"> 
  50.       <?php if ($mission) { ?><div id="mission"><?php print $mission ?></div><?php } ?> 
  51.       <div id="main"> 
  52.         <?php print $breadcrumb ?> 
  53.         <h1 class="title"><?php print $title ?></h1> 
  54.         <div class="tabs"><?php print $tabs ?></div> 
  55.         <?php if ($show_messages) { print $messages; } ?> 
  56.         <?php print $help ?> 
  57.         <?php print $content; ?> 
  58.         <?php print $feed_icons; ?> 
  59.       </div> 
  60.     </td> 
  61.     <?php if ($right) { ?><td id="sidebar-right"> 
  62.       <?php print $right ?> 
  63.     </td><?php } ?> 
  64.   </tr> 
  65. </table> 
  66.  
  67. <div id="footer"> 
  68.   <?php print $footer_message ?> 
  69.   <?php print $footer ?> 
  70. </div> 
  71. <?php print $closure ?> 
  72. </body> 
  73. </html> 
  74.  
I have test_0512.php as a page that website should be resulted as clicking on Submit button.. but i don't know it seems like its not working. I don't know how to do this..Please help me out :(

Expand|Select|Wrap|Line Numbers
  1. <html> 
  2.  
  3. <head> 
  4.  
  5. <title>TESTING PAGE</title> 
  6. </head> 
  7. <body> 
  8. <?php 
  9.  
  10.  
  11. $file = $_FILES['fileHandle']['name']; 
  12.  
  13. move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
  14.   "C:/upload/" . $_FILES["fileToUpload"]["name"]); 
  15.  
  16.  if ($_FILES["fileToUpload"]["error"] > 0) 
  17.     { 
  18.     echo "Apologies, an error has occurred."; 
  19.     echo "Error Code: " . $_FILES["fileToUpload"]["error"]; 
  20.     } 
  21.  else 
  22.     { 
  23.  
  24.     move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
  25.       "C:/upload/" . $_FILES["fileToUpload"]["name"]); 
  26.     } 
  27.  
  28.  
  29. //$file = fopen($fileName, "r") or exit ("Unable to open file!"); 
  30.  
  31. while(!feof($file)) 
  32.  
  33.     $line = fgets($file); // Read a line. 
  34.  
  35.  
  36. //escape if the line is empty 
  37.         if (trim($line) == "") 
  38.             continue; 
  39. //explode from the : 
  40.         $fields = explode(":", $line, 2); 
  41.  
  42.         echo "<b>$fields[0]</b>"; 
  43.  
  44. //checks if second part exists  
  45.         if (isset($fields[1])) 
  46.             echo " : $fields[1]"; 
  47.  
  48.         echo "<br/>"; 
  49.  
  50.  
  51.     fclose($file); 
  52. ?> 
  53.  
  54. </body> 
  55. </html> 
  56.  
  57.  
  58.  

Someone replied me that I should put below code but I could not get it..
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit'])) {  
  2.   //add your file processing code here  
  3.  
  4.  
  5. }else{ 
  6.   // add your upload form here 
  7.  
  8.  
  9.  
May 24 '10 #1
Share this Question
Share on Google+
2 Replies


dlite922
Expert 100+
P: 1,584
I'm not familiar with Drupal, but the code suggested means that if anything is submitted (in $_POST['submit']), then do this, else display the original page (whatever the page is before the user clicks submit)

I'm worried that if you don't understand that piece of code, how you will have the level of PHP programming skills to do what you need to do.

I would go back to some PHP tutorials and learn the language more if you want to do this yourself

Good luck,



Dan
May 25 '10 #2

P: 15
@dlite922
actually I understood that part I just did not know how to implement that...
I meant, I plugged this recommended code into the page where the replier mentioned me to do but that was messing me up with other codes..
May 25 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.