Jump to content

Uploading Multiple Photos To Database


han003

Recommended Posts

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();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/273794-uploading-multiple-photos-to-database/
Share on other sites

I'm not going to read through all of your code. You need to learn how to TEST your code. If there is a problem, narrow down the section of code where the error occurs and, if necessary, use appropriate data to expose the root cause. In this case, you are having a problem with multiple uploads and large uploads. So, parse down the code where the problem occurs and try single uploads of varying sizes and then try multiple uploads incrementally.

 

However, I am guessing this is the problem with the single large uploads

if($size < 1000000)
{
   uploadImages($tmp, $file_name, $extension, $album_id);
}
else
{

}

 

 

What was the purpose there? Else what??? That size check is for just less than 1MB. So, any file over that size would apparently not be uploaded

 

 

Also, why do you use a for loop to iterate over an array???

However, I am guessing this is the problem with the single large uploads

if($size < 1000000)
{
uploadImages($tmp, $file_name, $extension, $album_id);
}
else
{

}

 

 

What was the purpose there? Else what??? That size check is for just less than 1MB. So, any file over that size would apparently not be uploaded

 

 

If I dont do the size check I and a photo is larger than 1mb the whole thing gets fucked up, thats why its there

 

Also, why do you use a for loop to iterate over an array???

?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.