Storing Multiple Images using PHP

Asked By 30 points N/A Posted on -
qa-featured

 

I wondering if you anyone can help me about, how I can store multiple images in database using php?

SHARE
Best Answer by Sharath Reddy
Answered By 0 points N/A #106196

Storing Multiple Images using PHP

qa-featured

Hy,

You can manage to show or display your pictures in your website. If you want, you can use the web photo gallery to display the pictures one by one automatically. There have no need to use mysql server for create a simple web photo gallery.

At first open all pictures in Adobe Photoshop CS3 version.  Then select File and then select Export and then export it as a web gallery. Then you will get a new window, here you can manage or customize your web gallery’s design, color and others style. After complete the web gallery set up select ok and then you will get an auto script for your web gallery.

 

Now use it in your website for create a web gallery with your own pictures.

 

Thanks

Best Answer
Best Answer
Answered By 562920 points N/A #106197

Storing Multiple Images using PHP

qa-featured

Here is an example of a script written in PHP that will insert an image into the database. Name the file as “image.php”. This file will do the actual uploading process.

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "username"; // change to your database password
$db_password = "password"; // change to your database password
$database = "database"; // provide your database name
$db_table = "table"; // Your Table Name where you want to Store Your Image.
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
$uploadDir = 'images/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
                $filePath = addslashes($filePath);
}
$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";
mysql_query($query) or die('Error, query failed');
}
?>

After creating the script above, construct this next script and save it as “image.htm”. This is the page where you can select the file to upload and will then access the “image.php” file to begin the upload.

<form name="Image" enctype="multipart/form-data" action="image.php" method="POST">
<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
<INPUT type="submit" class="button" name="Submit" value="  Submit  ">
&nbsp;&nbsp;<INPUT type="reset" class="button" value="Cancel">
</form>

Finally, to view your uploaded image file, create the following script and save it as “profile.php”.

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "username"; // change to your database password
$db_password = "password"; // change to your database password
$database = "database"; // provide your database name
$db_table = "table"; // Your Table Name where you want to Store Your Image.
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
$query = "SELECT * FROM $db_table WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($result))
                {
echo "<img border="0" src="".$row['Image']."" width="102" alt="Your Name" height="91">";
}
?>

Login/Register to Answer

Related Questions