Hi I'm trying to upload multiple images to my database
Problems I'm encountering:
1. Wont upload more than 20, if i select 32 then only 20 uploaded and the rest is not
2. If upload take too long I get a 403 forbidden
3. Seems to crash when photo size is more than 1mb, my webhost says the file size limit is 128mb so I don't know
My HTML
<form enctype="multipart/form-data" method="post" action="upload.php">
<table>
<tr>
<td class="left"><span>Album Name</span></td><td><input id="albumName" name="name" type="text" autocomplete="off" maxlength="40" /><br /></td>
</tr>
<tr>
<td class="left"><span>Description</span></td><td><textarea name="description" cols="18" rows="2"></textarea></td>
</tr>
<tr>
<td class="left"><span>Choose File(s)</span></td><td><input type="file" name="files[]" multiple="multiple" /></td>
</tr>
<tr>
<td></td><td><input name="submit" type="submit" value="Upload"></td>
</tr>
</table>
</form>
My PHP
<?php
// INCLUDE RESIZE CLASS
include("resize-class.php");
// CHECK IF FIELDS ISSET
if (isset($_POST['submit'])
&& isset($_POST['name'])
&& isset($_POST['description'])) {
$name = ($_POST['name']);
$description = ($_POST['description']);
// CREATE NEW ALBUM IF FIELDS ARE NOT EMPTY
if (empty($name) ||
empty($description) ||
empty($_FILES['files'])) {
echo 'Fill out all fields';
}else{
if(!isset($_FILES['files'])){
echo 'Please select a minimum of one file to upload';
}else{
$album_id = createTable($name, $description);
addImages($album_id);
}
}
}
function addImages($album_id){
$count = count($_FILES['files']['name']);
for($i = 0; $i < $count; $i++){
$file_name = $_FILES['files']['name'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
$size = $_FILES['files']['size'][$i];
if($extension == 'jpg' || $extension == 'jpeg' || $extension == 'png' || $extension == 'gif'){
if($size < 1000000){
uploadImages($tmp, $file_name, $extension, $album_id);
}else{
}
}else{
}
}
}
function createTable($name, $description){
// CONNECT
include '../includes/database.php';
// GET DATE
$date = date('Y-m-d H:i:s');
// QUERY TO CREATE NEW ALBUM
$query = "INSERT INTO albums
(id, album_name, date_created, last_updated, thumb_id, album_description)
VALUES
(NULL, '$name', '$date', '$date', '1', '$description')";
// RUN QUERY
mysql_query($query) or die("Error in query: " . mysql_error());
// GET TABLE ID
$query = "SELECT id FROM `albums` ORDER BY id DESC LIMIT 1";
$result = mysql_query($query) or die("Error in query: " . mysql_error());
while($row = mysql_fetch_assoc($result)){
$album_id = $row['id'];
}
mysql_close();
return $album_id;
}
function uploadImages($tmp, $file_name, $extension, $album_id){
// CONNECT
include '../includes/database.php';
$directory = '../images/';
// GET IMAGE DIMENSIONS
list($width, $height, $type, $attr) = getimagesize($tmp);
// GET MAIN IMAGE DATA
$img_data = addslashes(file_get_contents($tmp));
move_uploaded_file($tmp, $directory . $file_name);
// *** 1) Initialise / load image
$resizeObj = new resize($directory . $file_name);
// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj -> resizeImage(240, 135, 'exact');
// *** 3) Save image
$resizeObj -> saveImage($directory . $file_name, 100);
// GET THUMB DATA
$thumb_data = addslashes(file_get_contents($directory . $file_name));
## -----------------------------------------------------------------
$query = "INSERT INTO photos
(id, album_id, photo, thumb, photo_name, photo_description, filetype, height, width)
VALUES
(NULL, $album_id ,'$img_data', '$thumb_data', '$file_name', 'Description', '$extension', '$height', '$width')";
mysql_query($query) or die("Error in query: " . mysql_error());
unlink($directory . $file_name);
// GET PHOTO ID
$query = "SELECT id FROM photos WHERE album_id=$album_id ORDER BY id ASC LIMIT 1";
$result = mysql_query($query) or die("Error in query: " . mysql_error());
while($row = mysql_fetch_assoc($result)){
$thumb_id = $row['id'];
}
$query = "UPDATE albums SET thumb_id =$thumb_id WHERE id=$album_id";
mysql_query($query) or die("Error in query: " . mysql_error());
mysql_close();
}
?>