Hi all,
Here is my code for uploading a text file to a mysql db. However, it does not work. All I get is a blank page.
Please, could you help me.
Thank you.
Kevin.
CODE.....
<?php
include('db_fns.php');
$conn = db_connect();
// file on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// original filename
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// mime type
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// error checking
if ($userfile_error)
{
echo 'Problem: '
switch ($userfile_error)
{
case 1: echo 'File exceeded upload max filesize'; break;
case 2: echo 'File exceeded max filesize'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// does the file have the right mime type?
if ($userfile_type != 'text/plain')
{
echo 'Problem: File is not plain text';
exit;
}
if ( (isset($HTTP_POST_FILES['userfile']['name']) &&
(dirname($HTTP_POST_FILES['userfile']['type']) == 'text')
&& is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])))
{
$fp = fopen($HTTP_POST_FILES['userfile']['tmp_name'], 'r');
$story_text = addslashes(fread($fp, filesize($HTTP_POST_FILES['userfile']['tmp_name'])));
fclose($fp);
}
else
echo 'Error: Please return and select a file';
$query = "insert into update (id, story) values
(NULL, '$userfile')";
$result = mysql_query($query);
if ($result)
echo 'Congratulations. File has been uploaded.';
else
echo 'Failed. Please try later.';
?>
Here is my HTML code.....
<form action="file_upload.php" method="post" enctype="multipart/form-data" name="form1">
<p>
<input name="userfile" type="file" maxlength="100">
</p><input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
I am very grateful for any help.