no image from mysql table, just binary data.help?
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
i have a forum im making . and im trying to make the picture thats stored in the user_table to show up..but all i get is binary data ...
the code for viewing the entries is here:
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username = mb_users.user";
$r= mysql_query($query); //Execute the query.
while($row = mysql_fetch_array ($r)) {
print " <br><br><table border=0 style=\"background-color:black; border:1px solid white; width:400px\">
<tr>
<td align='left'>";
?>
<?php
print '
<img src=" " >
';
?>
<?php
print "
</td>
<td>
Title: {$row['title']} <br>
Posted by: <b> {$row['username']} </b> <br><Br>
Message: {$row['entry']}
</td>
</tr>
</table>";
}
i have 2 tables, one called :
mb_users
http://nuhorizononline.web.aplus.net/mb_user.JPG
and
mb_entries:
http://nuhorizononline.web.aplus.net/mb_entries.JPG
any help?
Thanks,
-Mike[/url]
the code for viewing the entries is here:
PHP Code: [ Select ]
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username = mb_users.user";
$r= mysql_query($query); //Execute the query.
while($row = mysql_fetch_array ($r)) {
print " <br><br><table border=0 style=\"background-color:black; border:1px solid white; width:400px\">
<tr>
<td align='left'>";
?>
<?php
print '
<img src=" " >
';
?>
<?php
print "
</td>
<td>
Title: {$row['title']} <br>
Posted by: <b> {$row['username']} </b> <br><Br>
Message: {$row['entry']}
</td>
</tr>
</table>";
}
- $query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username = mb_users.user";
- $r= mysql_query($query); //Execute the query.
- while($row = mysql_fetch_array ($r)) {
- print " <br><br><table border=0 style=\"background-color:black; border:1px solid white; width:400px\">
- <tr>
- <td align='left'>";
- ?>
- <?php
- print '
- <img src=" " >
- ';
- ?>
- <?php
- print "
- </td>
- <td>
- Title: {$row['title']} <br>
- Posted by: <b> {$row['username']} </b> <br><Br>
- Message: {$row['entry']}
- </td>
- </tr>
- </table>";
- }
i have 2 tables, one called :
mb_users
http://nuhorizononline.web.aplus.net/mb_user.JPG
and
mb_entries:
http://nuhorizononline.web.aplus.net/mb_entries.JPG
any help?
Thanks,
-Mike[/url]
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
September 2nd, 2006, 2:05 pm
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
I understand what you're trying to do, but I don't see how you can expect the code you posted to do anything even remotely similar. Before I get into your actual problem though, there are a couple other issues you need to look into.
Whitespace is not always a good thing. You don't need multiple lines of nothing but white space and actual spaces breaking your code all up. The idea is to use it (in moderation) to group common functionality together. If you're using some code editor that outputs stuff that looks like this, go get PSPad (http://www.pspad.com)
You shouldn't be using double quotes on your HTML string. In that entire thing, you're only outputting 4 variables, all of which reside in a associative array. A better way to write it would be something like:
Since you're only writing a very few variables into a relatively large HTML block, and even better way would be to escape out of PHP entirely (which I show below).
There's hardly ever a reason to use mysql_fetch_array(). 9 times out of 10, including this one, what you really need is mysql_fetch_assoc(). The difference is that mysql_fetch_array() creates an array that's twice as large as the data because it creates keys by column name as well as by number. This doesn't have much effect on a small site, but can make a difference when something running on your code gets popular.
Never, never, never use single character variable names unless it's the $i in a for() (for() being something that you should probably be using a foreach() in place of anyway). The standard for a database result handler is "$result". Using single character variable names does not make your code look more "professional" in any way, shape or form and it's not that hard to think of a real name and press a few extra keys.
Here's your code, cleaned up:
I notice you have nothing in the image src here. This would explain why you're seeing nothing at all. However, whenever you're pulling binary data out of a database, you need to store the mime type and the file size and you have to have a separate script whose sole purpose is to pull the image out of the database and send it to the page. Something like:
To display the image on the page, you just call the above script as though it were an image:
HTH
Whitespace is not always a good thing. You don't need multiple lines of nothing but white space and actual spaces breaking your code all up. The idea is to use it (in moderation) to group common functionality together. If you're using some code editor that outputs stuff that looks like this, go get PSPad (http://www.pspad.com)
You shouldn't be using double quotes on your HTML string. In that entire thing, you're only outputting 4 variables, all of which reside in a associative array. A better way to write it would be something like:
Code: [ Select ]
'This is my string with a '.$data['value'].' embedded';
Since you're only writing a very few variables into a relatively large HTML block, and even better way would be to escape out of PHP entirely (which I show below).
There's hardly ever a reason to use mysql_fetch_array(). 9 times out of 10, including this one, what you really need is mysql_fetch_assoc(). The difference is that mysql_fetch_array() creates an array that's twice as large as the data because it creates keys by column name as well as by number. This doesn't have much effect on a small site, but can make a difference when something running on your code gets popular.
Never, never, never use single character variable names unless it's the $i in a for() (for() being something that you should probably be using a foreach() in place of anyway). The standard for a database result handler is "$result". Using single character variable names does not make your code look more "professional" in any way, shape or form and it's not that hard to think of a real name and press a few extra keys.
Here's your code, cleaned up:
PHP Code: [ Select ]
<?php
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username=mb_users.user";
$result = mysql_query($query);
while( $row = mysql_fetch_assoc($result) ){
?><br><br>
<table border=0 style="background-color:black; border:1px solid white; width:400px">
<tr>
<td align='left'><img src=" " ></td>
<td>Title: <?php echo $row['title']; ?><br>
Posted by: <b><?php echo $row['username']; ?></b>
<br><Br>
Message: <?php echo $row['entry']; ?>
</td>
</tr>
</table>
<?php
}
?>
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username=mb_users.user";
$result = mysql_query($query);
while( $row = mysql_fetch_assoc($result) ){
?><br><br>
<table border=0 style="background-color:black; border:1px solid white; width:400px">
<tr>
<td align='left'><img src=" " ></td>
<td>Title: <?php echo $row['title']; ?><br>
Posted by: <b><?php echo $row['username']; ?></b>
<br><Br>
Message: <?php echo $row['entry']; ?>
</td>
</tr>
</table>
<?php
}
?>
- <?php
- $query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username=mb_users.user";
- $result = mysql_query($query);
- while( $row = mysql_fetch_assoc($result) ){
- ?><br><br>
- <table border=0 style="background-color:black; border:1px solid white; width:400px">
- <tr>
- <td align='left'><img src=" " ></td>
- <td>Title: <?php echo $row['title']; ?><br>
- Posted by: <b><?php echo $row['username']; ?></b>
- <br><Br>
- Message: <?php echo $row['entry']; ?>
- </td>
- </tr>
- </table>
- <?php
- }
- ?>
I notice you have nothing in the image src here. This would explain why you're seeing nothing at all. However, whenever you're pulling binary data out of a database, you need to store the mime type and the file size and you have to have a separate script whose sole purpose is to pull the image out of the database and send it to the page. Something like:
PHP Code: [ Select ]
<?php
$sql = "SELECT * FROM wherever_my_file_data_resides WHERE something='whatever'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['file_binary'];
$type = $file_data['file_type'];
$size = $file_data['file_size'];
$name = $file_data['file_name'];
header( "Content-type: $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
$sql = "SELECT * FROM wherever_my_file_data_resides WHERE something='whatever'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['file_binary'];
$type = $file_data['file_type'];
$size = $file_data['file_size'];
$name = $file_data['file_name'];
header( "Content-type: $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
- <?php
- $sql = "SELECT * FROM wherever_my_file_data_resides WHERE something='whatever'";
- $file_data = mysql_fetch_assoc(mysql_query($sql));
- $data = $file_data['file_binary'];
- $type = $file_data['file_type'];
- $size = $file_data['file_size'];
- $name = $file_data['file_name'];
- header( "Content-type: $type");
- header( "Content-length: $size" );
- header( "Content-Disposition: attachment; filename=$name" );
- echo $data;
- ?>
To display the image on the page, you just call the above script as though it were an image:
Code: [ Select ]
<img src="image.php?image_id=123">
HTH
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
thanks! that will help..
my upload function was this..
if ($_REQUEST[completed] == 1) {
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
..it worked...but now since you said i have to get the size and type.. i dont know how to incorporate that into the upload function...
i've seen other upload functions but they dont work lol for me at at least...
could u guide me to a good upload technique?
my upload function was this..
if ($_REQUEST[completed] == 1) {
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
..it worked...but now since you said i have to get the size and type.. i dont know how to incorporate that into the upload function...
i've seen other upload functions but they dont work lol for me at at least...
could u guide me to a good upload technique?
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
hey the upload works now ...
i just have a question about my image..
here's the view_entry.php page i have..
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username = mb_users.user";
$result = mysql_query($query);
// if the guestbook is empty show a message
if(mysql_num_rows($result) == 0)
{
?>
<p><br><br> <font color="white">Message Board is empty. </font> </p>
<?php
}
else
{
while( $row = mysql_fetch_assoc($result) ){
?><br><br>
<table border=0 style="background-color:black; border:1px solid whit e; width:400px">
<tr>
<td align='left'> <img src="view_picture.php?id={$row['user_id']}"> </td>
<td>Title: <?php echo $row['title']; ?><br>
Posted by: <b><?php echo $row['username']; ?></b>
<br><Br>
Message: <?php echo $row['entry']; ?>
</td>
</tr>
</table>
<?php
}
}
and here's my view picture :
import_request_variables("gP");
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
;
$id = $_GET['id'];
$query = "SELECT img_title, imgdata, image_type, image_size " .
"FROM mb_users WHERE user_id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($fileName, $content, $fileType, $fileSize) = mysql_fetch_array($result);
header("Content-length: $fileSize");
header("Content-type: $fileType");
header("Content-Disposition: attachment; filename=$fileName");
echo $content;
exit;
}
in my messageboad, i just have a red x in my picture box..i dont think its getting the image...
could u help me out with that?
my message board is at http://www.nuhorizononline.com/messageB ... eBoard.php
thanks.
i just have a question about my image..
here's the view_entry.php page i have..
$query = "SELECT * FROM mb_entries, mb_users WHERE mb_entries.username = mb_users.user";
$result = mysql_query($query);
// if the guestbook is empty show a message
if(mysql_num_rows($result) == 0)
{
?>
<p><br><br> <font color="white">Message Board is empty. </font> </p>
<?php
}
else
{
while( $row = mysql_fetch_assoc($result) ){
?><br><br>
<table border=0 style="background-color:black; border:1px solid whit e; width:400px">
<tr>
<td align='left'> <img src="view_picture.php?id={$row['user_id']}"> </td>
<td>Title: <?php echo $row['title']; ?><br>
Posted by: <b><?php echo $row['username']; ?></b>
<br><Br>
Message: <?php echo $row['entry']; ?>
</td>
</tr>
</table>
<?php
}
}
and here's my view picture :
import_request_variables("gP");
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
;
$id = $_GET['id'];
$query = "SELECT img_title, imgdata, image_type, image_size " .
"FROM mb_users WHERE user_id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($fileName, $content, $fileType, $fileSize) = mysql_fetch_array($result);
header("Content-length: $fileSize");
header("Content-type: $fileType");
header("Content-Disposition: attachment; filename=$fileName");
echo $content;
exit;
}
in my messageboad, i just have a red x in my picture box..i dont think its getting the image...
could u help me out with that?
my message board is at http://www.nuhorizononline.com/messageB ... eBoard.php
thanks.
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
Never use user submitted values directly in the query string like that. At the very least, you should be assigning the value to the variable using addslashes(). Since the id is a number, you'd do better to use intval(). You can look both of these up here (a good idea would be to bookmark this page): http://www.php.net/quickref.php
As for dealing with your file uploads: http://us2.php.net/features.file-upload
The $_FILES array contains the mime type of the uploaded file. There's a basic tutorial for doing all of this here: http://codewalkers.com/tutorials/35/1.html
As for dealing with your file uploads: http://us2.php.net/features.file-upload
The $_FILES array contains the mime type of the uploaded file. There's a basic tutorial for doing all of this here: http://codewalkers.com/tutorials/35/1.html
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
thanks..the upload works fine ..
here's a quick question..
regarding my image in the user table..
i can grab the user_id, but it wont display the image ..
look at this to see what im talking about. http://www.nuhorizononline.com/viewPicture.php
the user_table has:
user_id, first_name, last_name...,img_title, image_size, image_type, imgdata
here's a quick question..
regarding my image in the user table..
i can grab the user_id, but it wont display the image ..
look at this to see what im talking about. http://www.nuhorizononline.com/viewPicture.php
the user_table has:
user_id, first_name, last_name...,img_title, image_size, image_type, imgdata
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
changing it to image/jpeg doesnt do anything to it...
check it out again..
http://www.nuhorizononline.com/viewPicture.php
my code for download2.php is:
if ($dbc = @mysql_connect ('localhost', 'database', 'password')) {
if (!@mysql_select_db ('db)) {
die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
}
}
else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$sql = "SELECT * FROM mb_users WHERE user_id = {$_GET['id']}";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$size = $file_data['image_size'];
$name = $file_data['img_name'];
header( "Content-type: image/jpeg");
header( "Content-length: $size" );
echo $data;
print "
{$row['user']}
{$row['pass']}
";
it doesn't seem to read any of the fields on the download2.php page... on viewPicture.php it prints out the username and first name, etc. though.
check it out again..
http://www.nuhorizononline.com/viewPicture.php
my code for download2.php is:
PHP Code: [ Select ]
if ($dbc = @mysql_connect ('localhost', 'database', 'password')) {
if (!@mysql_select_db ('db)) {
die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
}
}
else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$sql = "SELECT * FROM mb_users WHERE user_id = {$_GET['id']}";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$size = $file_data['image_size'];
$name = $file_data['img_name'];
header( "Content-type: image/jpeg");
header( "Content-length: $size" );
echo $data;
print "
{$row['user']}
{$row['pass']}
";
- if ($dbc = @mysql_connect ('localhost', 'database', 'password')) {
- if (!@mysql_select_db ('db)) {
- die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
- }
- }
- else {
- die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
- }
- $sql = "SELECT * FROM mb_users WHERE user_id = {$_GET['id']}";
- $file_data = mysql_fetch_assoc(mysql_query($sql));
- $data = $file_data['imgdata'];
- $size = $file_data['image_size'];
- $name = $file_data['img_name'];
- header( "Content-type: image/jpeg");
- header( "Content-length: $size" );
- echo $data;
- print "
- {$row['user']}
- {$row['pass']}
- ";
it doesn't seem to read any of the fields on the download2.php page... on viewPicture.php it prints out the username and first name, etc. though.
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
Try this
Code: [ Select ]
<?php
if( $dbc = @mysql_connect('localhost','database','password') ){
if( !@mysql_select_db('db) ){
die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
}
} else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$user_id = intval($_GET['user_id']);
$sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$type = $file_data['file_type'];
$size = $file_data['image_size'];
$name = $file_data['file_name'];
header( "Content-type: image/jpeg"); // $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
if( $dbc = @mysql_connect('localhost','database','password') ){
if( !@mysql_select_db('db) ){
die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
}
} else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$user_id = intval($_GET['user_id']);
$sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$type = $file_data['file_type'];
$size = $file_data['image_size'];
$name = $file_data['file_name'];
header( "Content-type: image/jpeg"); // $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
- <?php
- if( $dbc = @mysql_connect('localhost','database','password') ){
- if( !@mysql_select_db('db) ){
- die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
- }
- } else {
- die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
- }
- $user_id = intval($_GET['user_id']);
- $sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
- $file_data = mysql_fetch_assoc(mysql_query($sql));
- $data = $file_data['imgdata'];
- $type = $file_data['file_type'];
- $size = $file_data['image_size'];
- $name = $file_data['file_name'];
- header( "Content-type: image/jpeg"); // $type");
- header( "Content-length: $size" );
- header( "Content-Disposition: attachment; filename=$name" );
- echo $data;
- ?>
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
- this213
- Guru


- Joined: Mar 01, 2004
- Posts: 1242
- Loc: ./
- Status: Offline
You still have something not quite right, post the current contents of this script:
http://www.nuhorizononline.com/download2.php
Be sure you're changing your database username and password for the sake of security.
Make sure you're storing the file's binary, size, type and name in the database and are able to call them back out. You might also post your upload script so I can see how you're doing things there too.
http://www.nuhorizononline.com/download2.php
Be sure you're changing your database username and password for the sake of security.
Make sure you're storing the file's binary, size, type and name in the database and are able to call them back out. You might also post your upload script so I can see how you're doing things there too.
- nuhorizon
- Student


- Joined: Mar 16, 2006
- Posts: 68
- Status: Offline
this whole thing is so i can be able to grab the image from the user table and display it on the message board on this site im making... upload form can be looked at http://www.nuhorizononline.com/messageB ... ter_mb.php (it's not fully validated yet, but it works and i got the image to upload as well)
here's the php script for the upload :
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
import_request_variables("gP");
if (isset ($_POST['submit'])) {
//connect to MySQL server and select databse
if ($dbc = @mysql_connect ('localhost', 'myusername', 'mypassword')) { //connect to MySQL
if (!@mysql_select_db ('mydatabase')) { //connect to myblog database
die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
}
}
else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
//CHECK FOR UNIQUE USERNAME
$username =($_POST['username']);
$sql = "SELECT * FROM mb_users WHERE user = '$username'";
$row = mysql_fetch_assoc(mysql_query($sql));
if( $row['user'] == $username) {
print '<p style="color:white; border:1px solid white; width:400px"> <font color="orange"> ERROR: </font> Username is already taken. Please pick another one. </p>';
}
else {
//THIS IS WHERE THE UPLOAD STARTS
if(isset($_POST['submit']) && $_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);
//DEFINE the query.
$query = "INSERT INTO mb_users (user_id, date_entered, first_name, last_name, user, pass, user_email, city, province, country, age, sex, img_title, imgdata, image_type, image_size)
VALUES (0, NOW(), '{$_POST['first_name']}', '{$_POST['last_name']}', '{$_POST['username']}', '{$_POST['password']}', '{$_POST['email']}', '{$_POST['city']}', '{$_POST['prov']}', '{$_POST['country']}', '{$_POST['age']}', '{$_POST['sex']}','$fileName', '$content', '$fileType', '$fileSize')";
//EMAIL CONFIRMATION TO NEW USER
$to= $email;
$subject = "Confirmation Message -DO NOT REPLY-";
$body= "Thanks $first_name! You're now registered which means you can post messages, buy merchandise, and also win cool prizes! \n
Your account information is here: \n
Username: $username \n
password: $password \n
e-mail: $email ";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: NuHorizon Message Board <webmaster@nuhorizononline.com>' . "\r\n";
mail($to, $subject,$body, $headers);
//CHECK TO SEE IF THE QUERY HAS BEEN SUBMITTED OKAY.
if (@mysql_query ($query)) {
//REDIRECT USER TO WELCOME PAGE
header('Location:confirm_registration.php');
exit();
}
else {
// if the file is not less than the maximum allowed, print an error
echo
'<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.'</div>
<div>File '.$_FILES['imagefile']['name'].' is '.$_FILES['imagefile']['size'].' bytes</div>
<hr />';
}
}
} //END of unique username.
mysql_close(); //Close the connection.
} //END OF ELSE
} //END OF UPLOAD
?>
my user table is shown here:
http://nuhorizononline.web.aplus.net/userstable.JPG
my download2.php script is here:
<?php
if( $dbc = @mysql_connect('localhost','myusername','mypassword') ){
if( !@mysql_select_db('mydatabase') ){
die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
}
} else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$user_id = intval($_GET['user_id']);
$sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$type = $file_data['image_type'];
$size = $file_data['image_size'];
$name = $file_data['image_title'];
header( "Content-type: image/jpeg"); // $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
here's the php script for the upload :
PHP Code: [ Select ]
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
import_request_variables("gP");
if (isset ($_POST['submit'])) {
//connect to MySQL server and select databse
if ($dbc = @mysql_connect ('localhost', 'myusername', 'mypassword')) { //connect to MySQL
if (!@mysql_select_db ('mydatabase')) { //connect to myblog database
die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
}
}
else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
//CHECK FOR UNIQUE USERNAME
$username =($_POST['username']);
$sql = "SELECT * FROM mb_users WHERE user = '$username'";
$row = mysql_fetch_assoc(mysql_query($sql));
if( $row['user'] == $username) {
print '<p style="color:white; border:1px solid white; width:400px"> <font color="orange"> ERROR: </font> Username is already taken. Please pick another one. </p>';
}
else {
//THIS IS WHERE THE UPLOAD STARTS
if(isset($_POST['submit']) && $_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);
//DEFINE the query.
$query = "INSERT INTO mb_users (user_id, date_entered, first_name, last_name, user, pass, user_email, city, province, country, age, sex, img_title, imgdata, image_type, image_size)
VALUES (0, NOW(), '{$_POST['first_name']}', '{$_POST['last_name']}', '{$_POST['username']}', '{$_POST['password']}', '{$_POST['email']}', '{$_POST['city']}', '{$_POST['prov']}', '{$_POST['country']}', '{$_POST['age']}', '{$_POST['sex']}','$fileName', '$content', '$fileType', '$fileSize')";
//EMAIL CONFIRMATION TO NEW USER
$to= $email;
$subject = "Confirmation Message -DO NOT REPLY-";
$body= "Thanks $first_name! You're now registered which means you can post messages, buy merchandise, and also win cool prizes! \n
Your account information is here: \n
Username: $username \n
password: $password \n
e-mail: $email ";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: NuHorizon Message Board <webmaster@nuhorizononline.com>' . "\r\n";
mail($to, $subject,$body, $headers);
//CHECK TO SEE IF THE QUERY HAS BEEN SUBMITTED OKAY.
if (@mysql_query ($query)) {
//REDIRECT USER TO WELCOME PAGE
header('Location:confirm_registration.php');
exit();
}
else {
// if the file is not less than the maximum allowed, print an error
echo
'<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.'</div>
<div>File '.$_FILES['imagefile']['name'].' is '.$_FILES['imagefile']['size'].' bytes</div>
<hr />';
}
}
} //END of unique username.
mysql_close(); //Close the connection.
} //END OF ELSE
} //END OF UPLOAD
?>
- <?php
- ini_set('display_errors', 1);
- error_reporting(E_ALL & ~E_NOTICE);
- import_request_variables("gP");
- if (isset ($_POST['submit'])) {
- //connect to MySQL server and select databse
- if ($dbc = @mysql_connect ('localhost', 'myusername', 'mypassword')) { //connect to MySQL
- if (!@mysql_select_db ('mydatabase')) { //connect to myblog database
- die ('<p> Could not select the database because: <b>' . mysql_error() . '</b> </p>');
- }
- }
- else {
- die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
- }
- //CHECK FOR UNIQUE USERNAME
- $username =($_POST['username']);
- $sql = "SELECT * FROM mb_users WHERE user = '$username'";
- $row = mysql_fetch_assoc(mysql_query($sql));
- if( $row['user'] == $username) {
- print '<p style="color:white; border:1px solid white; width:400px"> <font color="orange"> ERROR: </font> Username is already taken. Please pick another one. </p>';
- }
- else {
- //THIS IS WHERE THE UPLOAD STARTS
- if(isset($_POST['submit']) && $_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);
- //DEFINE the query.
- $query = "INSERT INTO mb_users (user_id, date_entered, first_name, last_name, user, pass, user_email, city, province, country, age, sex, img_title, imgdata, image_type, image_size)
- VALUES (0, NOW(), '{$_POST['first_name']}', '{$_POST['last_name']}', '{$_POST['username']}', '{$_POST['password']}', '{$_POST['email']}', '{$_POST['city']}', '{$_POST['prov']}', '{$_POST['country']}', '{$_POST['age']}', '{$_POST['sex']}','$fileName', '$content', '$fileType', '$fileSize')";
- //EMAIL CONFIRMATION TO NEW USER
- $to= $email;
- $subject = "Confirmation Message -DO NOT REPLY-";
- $body= "Thanks $first_name! You're now registered which means you can post messages, buy merchandise, and also win cool prizes! \n
- Your account information is here: \n
- Username: $username \n
- password: $password \n
- e-mail: $email ";
- // To send HTML mail, the Content-type header must be set
- $headers = 'MIME-Version: 1.0' . "\r\n";
- $headers .= 'From: NuHorizon Message Board <webmaster@nuhorizononline.com>' . "\r\n";
- mail($to, $subject,$body, $headers);
- //CHECK TO SEE IF THE QUERY HAS BEEN SUBMITTED OKAY.
- if (@mysql_query ($query)) {
- //REDIRECT USER TO WELCOME PAGE
- header('Location:confirm_registration.php');
- exit();
- }
- else {
- // if the file is not less than the maximum allowed, print an error
- echo
- '<div>File exceeds the Maximum File limit</div>
- <div>Maximum File limit is '.$maxsize.'</div>
- <div>File '.$_FILES['imagefile']['name'].' is '.$_FILES['imagefile']['size'].' bytes</div>
- <hr />';
- }
- }
- } //END of unique username.
- mysql_close(); //Close the connection.
- } //END OF ELSE
- } //END OF UPLOAD
- ?>
my user table is shown here:
http://nuhorizononline.web.aplus.net/userstable.JPG
my download2.php script is here:
PHP Code: [ Select ]
<?php
if( $dbc = @mysql_connect('localhost','myusername','mypassword') ){
if( !@mysql_select_db('mydatabase') ){
die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
}
} else {
die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
}
$user_id = intval($_GET['user_id']);
$sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
$file_data = mysql_fetch_assoc(mysql_query($sql));
$data = $file_data['imgdata'];
$type = $file_data['image_type'];
$size = $file_data['image_size'];
$name = $file_data['image_title'];
header( "Content-type: image/jpeg"); // $type");
header( "Content-length: $size" );
header( "Content-Disposition: attachment; filename=$name" );
echo $data;
?>
- <?php
- if( $dbc = @mysql_connect('localhost','myusername','mypassword') ){
- if( !@mysql_select_db('mydatabase') ){
- die ('<p> Could not select the database because: <b>'.mysql_error().'</b> </p>');
- }
- } else {
- die ('<p> Could not connect to MySQL because: <b>' . mysql_error() . '</b> </p>');
- }
- $user_id = intval($_GET['user_id']);
- $sql = "SELECT * FROM mb_users WHERE user_id='$user_id'";
- $file_data = mysql_fetch_assoc(mysql_query($sql));
- $data = $file_data['imgdata'];
- $type = $file_data['image_type'];
- $size = $file_data['image_size'];
- $name = $file_data['image_title'];
- header( "Content-type: image/jpeg"); // $type");
- header( "Content-length: $size" );
- header( "Content-Disposition: attachment; filename=$name" );
- echo $data;
- ?>
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
September 6th, 2006, 2:06 pm
1, 2
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 21 posts
- Users browsing this forum: No registered users and 105 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
