|
-
March 22nd, 2009, 02:19 PM
#1
Uploading file to mysql using php
Hi,
I'm beginner in php and i'm trying to upload files to mysql using php using the following code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
echo "<br>File $fileName uploaded<br>";
}
?>
</body>
</html>
but i got the following errors:
Warning: include(library/config.php) [function.include]: failed to open stream: No such file or directory in F:\wamp\www\PHP_Test1\upload.php on line 39
Warning: include() [function.include]: Failed opening 'library/config.php' for inclusion (include_path='.;C:\php5\pear') in F:\wamp\www\PHP_Test1\upload.php on line 39
Warning: include(library/opendb.php) [function.include]: failed to open stream: No such file or directory in F:\wamp\www\PHP_Test1\upload.php on line 40
Warning: include() [function.include]: Failed opening 'library/opendb.php' for inclusion (include_path='.;C:\php5\pear') in F:\wamp\www\PHP_Test1\upload.php on line 40
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in F:\wamp\www\PHP_Test1\upload.php on line 45
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in F:\wamp\www\PHP_Test1\upload.php on line 45
Error, query failed
well i don't know how to get these files "(library/config.php)" "(library/opendb.php"
Last edited by PeejAvery; March 22nd, 2009 at 03:15 PM.
Reason: Added code tags.
-
March 22nd, 2009, 02:26 PM
#2
Re: Uploading file to mysql using php
i tried the following code to access the db:
PHP Code:
session_cache_limiter('nocache'); session_start(); $host = "localhost"; $dbusername = "root"; $dbpassword = ""; $dbname = "uploaded_itemsss"; $link_id=mysql_pconnect($host,$dbusername,$dbpassword); mysql_select_db($dbname,$link_id);
i got no error but there's nothing uploaded to the db...
http://www.theukwebdesigncompany.com...hp?article=678
Last edited by PeejAvery; March 22nd, 2009 at 03:15 PM.
Reason: Added PHP tags.
-
March 22nd, 2009, 03:17 PM
#3
Re: Uploading file to mysql using php
All those errors are saying is that you don't have the proper includes files in the right directory. Where your PHP file is, you need to have a folder named library. Then, in that folder, you need to have those included PHP files.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 22nd, 2009, 04:04 PM
#4
Re: Uploading file to mysql using php
well i removed the include files and there's no errors right now but there's nothing uploaded...here's the whole code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>Demo to upload file using PHP</title>
<script language="javascript">
function FnChk()
{
if(frm1.Nm.value=="")
{
alert("Enter your Name");
frm1.Nm.focus();
return false;
}
if(frm1.Address.value=="")
{
alert("Enter your Address");
frm1.Address.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name=frm1 method=post action="Demo.php?Mode=Save" enctype="multipart/form-data" onSubmit="return FnChk()">
<table>
<tr><td>Name :</td><td><input type=text name=Nm></td></tr>
<tr><td>Address :</td><td><textarea name=Address cols=20 rows=10></textarea></td></tr>
<tr><td colspan=2 align=center><input type=file name=Fle></td></tr>
<tr><td colspan=2 align=center><input type=submit name=Submit value=Submit></td></tr>
</table>
</form>
<?
session_cache_limiter('nocache');
session_start();
$host = "localhost";
$dbusername = "root";
$dbpassword = "password";
$dbname = "uploaded_itemsss";
$link_id=mysql_pconnect($host,$dbusername,$dbpassword);
mysql_select_db($dbname,$link_id);
if($_REQUEST['Mode']=="Save")
{
$Nm=$_REQUEST['Nm'];
$Addr=$_REQUEST['Address'];
if ($Nm !="" && $Addr!="" && $HTTP_POST_FILES['Fle']['name'] != "")
{
if(!(is_dir("Image_upload_Folder")))
{
mkdir("Image_upload_Folder");
}
copy($HTTP_POST_FILES['Fle']['tmp_name'],"Image_upload_Folder/".$HTTP_POST_FILES['Fle']['name']);
$Pict=$HTTP_POST_FILES['Fle']['name'];
$Sql="insert into customer([name],[address],[file_name]) values('".$Nm."','".$Addr."','".$Pict."')";
mysql_query($Sql);
}
}
?>
</body>
</html>
Last edited by PeejAvery; March 22nd, 2009 at 04:48 PM.
Reason: Added code tags.
-
March 22nd, 2009, 04:50 PM
#5
Re: Uploading file to mysql using php
Please start using code tags when posting code. Thanks.
PHP.net's documentation on file uploads is all you need to handle an upload. Then, from there, you can easily import it into a MySQL database by a standard INSERT command.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 22nd, 2009, 05:36 PM
#6
Re: Uploading file to mysql using php
ok this link is wonderful!!!...thanks a lot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|