|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Uploading and parsing a file into a table
I am having trouble writing a script that use a form to take in a file to parse it into a selected table. My script is bellow. When i run the script by hitting the submit button nothing happens and the page refreshes and give no error messages. Please note that I am trying to acomplish two things. One to get the file, Two to parse the file into the selected table.
<?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; if($tmpName) { print "File Name: $File_name<p>\n"; print "File Size: $File_size<p>\n"; if(move_uploaded_file ($_FILES['File']['tmp_name'], $_FILES['File']['name']) or die ('Could not upload')) { $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } # first get a mysql connection as per the FAQ //function that connects to the dba so that I can insert data into the selected table hookUpDb2(); $query = "INSERT INTO 2004Scores (fileName, content ) ". "VALUES ('$fileName','$content')"; mysql_query($query) or die('Error, query failed'); include 'library/closedb.php'; echo "<br>File $fileName uploaded<br>"; } print "Your file uploaded successfully!<p>\n"; } else { print "Was unable to upload file!<p>\n"; } unlink($tmpName);//deletes file } ?> <form method="post" action="<?php echo($PHP_SELF) ?>" > <table class="formBg" align="center" width="42%" border="1" cellspacing="0" cellpadding="2"> <tr valign="top"> <td width="43%">Choose Table here</td> <td width="57%"> <select class="dropdownMedium" name="chooseTable"> <option selected value="0">--- Choose table here ---</option> <?php getTableName(); ?> </select> <input class="medium" type="hidden" name="TableName" value="<?php print(unFormatData($row[1])); ?>"> </td> </tr> <tr valign="top"> <td width="43%">Choose File here</td> <td width="57%"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input type="file" name="File" size=20> <!--input name="userfile" type="file" id="userfile"> --> </td> </tr> <tr valign="top"> <td width="43%"> </td> <td width="57%"> <input name="upload" type="submit" class="box" id="upload" value=" Upload "> <input type="reset" name="reset" value="Reset"> </td> </tr> </table> </form> |
|
#2
|
|||
|
|||
|
To upload files, you must have 'enctype="multipart/form-data"' in your opening form tag. It's probably not processing the form, so you first if() is failing -- it'd help if you added and else clause to tell you if that if fails.
Also, please wrap your codes in php tags when posting ( [ PHP ] [ /PHP ] without the spaces between PHP and the brackets). It color-codes and allows indention, making it much easier to read. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Uploading and parsing a file into a table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|