Jump to content

Need help uploading images to DB and then displaying them.


thrgk

Recommended Posts

Hello everyone!

 

I need some help enabling people to upload images to my website, and then display them. I know how to display them, and I know how to have them upload the directory and the name of the photo, but I am not sure how to let them actually upload to my server (Since uploading photos to DB is bad).

 

I will post my code below, what I need help with I guess is letting them upload the photo to my server in "images" folder and only letting them upload a size limit specifically to gif, png, and jpeg photos.

 

I know there are tutorials but I cannot seem to get it to work.

 

Thanks for the help

 

    <DOCTYPE HTML>
    <html>
    <head>
    <div id="header">
        <?php include ('header.php') ?>
    </div>
    <h1> Photo Album </h1>
    
        <div id="home"> This is the "Photo Album's" Home Page, where Albums by different users will be showed. 
         </div>
<?php
// create the connection  (host, username, pw, dbname)
// and give feedback if that fails
$con = mysqli_connect('localhost', 'tmh233sp14', 'password', 'info230_SP14_tmh233sp14');
 
// check connection
if (mysqli_connect_errno()) {
   printf('Connect failed: %s\n', mysqli_connect_error());
   exit();
}
// construct the query 
$query1 = "SELECT albumID, title FROM Album ORDER BY title";
 
if ($result1 = mysqli_query($con, $query1)) {
 
    /* fetch associative array */
    while ($row = mysqli_fetch_row($result1)) {
       print "<div id='albumname'>('$row[0], $row[1]')</div>";
 
    }
    
}
// construct the query 
$query = "SELECT urlID FROM Photos";
if ($result = mysqli_query($con, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
$urlID = $row[0]; // this contains "/images/cat.jpg"
print "<img src='$baseURL/images/$urlID' />";
 
}
 
}
 
// close the connection
mysqli_close($con);
?>
 
                <form action="photos.php" method="POST">
        Upload Photos <input type="file" name="urlID" required/>
        Photo Name <input type="text" name="photoID" required/>
 
        <input type="submit" id="submit" value="Upload" />
            </form>
  <?php
$con=mysqli_connect("localhost","tmh233sp14","password","info230_SP14_tmh233sp14");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
if (isset($_POST['urlID']) && !empty($_POST['urlID']) && isset($_POST['photoID']) && !empty($_POST['photoID'])) {
 
 $sql="INSERT INTO Photos (urlID, photoID)
 VALUES
 ('$_POST[urlID]','$_POST[photoID]')";
 
 if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
  }
 echo "1 record added";
}
 
mysqli_close($con);
?>
    
    <div id="footer">
        <?php include ('footer.php') ?>
    </div>
        
    </body>
        </html>
 
Link to comment
Share on other sites

Also my tables are as follows:

 

Photos(photoID(int 150), type(varchar 10), size (varchar 20), name (varchar 150), dateadded (date), urlID, (varchar 150) caption(varchar 150))
 

User(userID(varchar 150)), fName, (varchar 150)) lName(varchar 150)), password(varchar 150))
 

Album(albumID(Int 150), title(varchar 150), datecreated(date), datemod(date), size(Int 150)(# of photos))
 

photoInAlbum(photoID(int 150), albumID(int 150))

Link to comment
Share on other sites

There are few problems with your form, you have not added the  enctype="multipart/form-data"  attribute to your form tag. And you access the uploaded file(s) from the $_FILES superglobal not the $_POST superglobal.

 

Have a read of the following on how PHP handles file uploads

http://php.net/manual/en/features.file-upload.post-method.php

 

 

You should use blob, not varchar, or do something like my solution at: http://forums.phpfreaks.com/topic/286668-phpmyadmin-blob-insert-error/

 

The OP is storing the filename in the database, not the actual file.

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.