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>