thrgk Posted March 13, 2014 Share Posted March 13, 2014 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)) { $baseURL = "http://info230.cs.cornell.edu/users/tmh233sp14/www/P3"; $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> Quote Link to comment https://forums.phpfreaks.com/topic/286946-need-help-uploading-images-to-db-and-then-displaying-them/ Share on other sites More sharing options...
thrgk Posted March 13, 2014 Author Share Posted March 13, 2014 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)) Quote Link to comment https://forums.phpfreaks.com/topic/286946-need-help-uploading-images-to-db-and-then-displaying-them/#findComment-1472476 Share on other sites More sharing options...
Q695 Posted March 13, 2014 Share Posted March 13, 2014 You should use blob, not varchar, or do something like my solution at: http://forums.phpfreaks.com/topic/286668-phpmyadmin-blob-insert-error/ Quote Link to comment https://forums.phpfreaks.com/topic/286946-need-help-uploading-images-to-db-and-then-displaying-them/#findComment-1472486 Share on other sites More sharing options...
Ch0cu3r Posted March 13, 2014 Share Posted March 13, 2014 (edited) 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 March 13, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286946-need-help-uploading-images-to-db-and-then-displaying-them/#findComment-1472492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.