I am still working on my image uploading script as part of my blogging site, and it seems I have a problem when I try to upload several large files. When I try to upload 6 or more jpegs, each of which is at least 1 mb, first of all the page takes forever to process (literally... 15m or so) and then the images simply don't get uploaded. Is there a server time-out or maximum upload size that is to blame for this? And also, is there a way to optimize my script so that it doesn't take so long to load? (We have a slow connection where I live but still...)
To see this for yourself, go to www.efamz.com, login as 'Bablon' with password 'bablon' and try uploading several large pictures.
Here is the script for uploading, sorry it's long:
<?
session_start();
$user = $_SESSION["user"];
//Content type
header('Content-type: image/jpeg');
$date = date("n / j / y");
$title = $_REQUEST["title"];
$numberArray = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty");
$captionsArray = array($_POST["captionOne"], $_POST["captionTwo"], $_POST["captionThree"], $_POST["captionFour"], $_POST["captionFive"]);
//desired maximum dimensions
$height = 600;
$width = 600;
//size of new files
$storage = 0;
//database file list
$db = $title . ";;";
//file upload boolean
$file = false;
for($i=0;$i<20;$i++)
{
if( $_FILES[$numberArray[$i]]['size'] != 0)
{
//desired directory
$directory = "";
//desired filename
$fileName = $user . "_" . $title . "_" . $i;
//check if file is a picture
if (exif_imagetype($_FILES[$numberArray[$i]]["tmp_name"]) != FALSE)
{
//find original file dimensions
list($width_orig, $height_orig) = getimagesize($_FILES[$numberArray[$i]]["tmp_name"]);
if($width_orig > $width || $height_orig > $height)
{
//find new dimensions
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
}
else
{
$width = $width_orig;
$height = $height_orig;
}
if (exif_imagetype($_FILES[$numberArray[$i]]["tmp_name"]) == IMAGETYPE_JPEG)
{
//find unique file address
$fileLocation = $directory . $fileName . ".jpg";
//resample (shrink image)
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($_FILES[$numberArray[$i]]["tmp_name"]);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//write new image to file
$newImage = fopen($fileLocation, "w");
fclose($newImage);
if($_FILES[$numberArray[$i]]['size'] < 50000)
imagejpeg($image_p, $fileLocation, 100);
else if($_FILES[$numberArray[$i]]['size'] < 100000)
imagejpeg($image_p, $fileLocation, 80);
else
imagejpeg($image_p, $fileLocation, 60);
//increment storage space usage
settype($storage, "float");
$storage += $_FILES[$numberArray[$i]]["size"] / 10000000;
//write file address to db
$db .= $fileName . ".jpg" . "---";
$text = "caption" . $numberArray[$i];
$db .= $_POST[$text] . ";";
$file = true;
}
else
{
if (exif_imagetype($_FILES[$numberArray[$i]]["tmp_name"]) == IMAGETYPE_GIF)
{
//find unique file address
$fileLocation = $directory . $fileName . ".gif";
//resample (shrink image)
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromgif($_FILES[$numberArray[$i]]["tmp_name"]);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//write new image to file
$newImage = fopen($fileLocation, "w");
fclose($newImage);
if($_FILES[$numberArray[$i]]['size'] < 50000)
imagegif($image_p, $fileLocation, 100);
else if($_FILES[$numberArray[$i]]['size'] < 100000)
imagegif($image_p, $fileLocation, 80);
else
imagegif($image_p, $fileLocation, 60);
//increment storage space usage
settype($storage, "float");
$storage += $_FILES[$numberArray[$i]]["size"] / 1000000;
//write file address to db
$db .= $fileName . ".gif" . "---";
$text = "caption" . $numberArray[$i];
$db .= $_POST[$text] . ";";
$file = true;
}
else
{
echo 'The file is a picture, but not a jpeg or a gif.';
}
}
}
else
{
echo 'The file is not a picture.';
}
}
}
// Connecting, selecting database
$link = mysql_connect('mysql6.servage.net', 'efamz', 'Kikuyu2003')
or die('Could not connect: ' . mysql_error());
mysql_select_db('efamz') or die('Could not select database');
// Performing SQL query
$query = "SELECT * FROM main WHERE user = '$user'";
$result = mysql_query($query, $link) or die('Query failed: ' . mysql_error());
//get db data
while ($row = mysql_fetch_object($result))
{
$password = $row->password;
$email = $row->email;
$location = $row->location;
$about = $row->about;
$interests = $row->interests;
$ourFamily = $row->ourFamily;
$network = $row->network;
$messages = $row->messages;
$blogs = $row->blog;
$albums = $row->albums;
$oldStorage = $row->storage;
$signupDate = $row->signupDate;
}
settype($oldStorage, "float");
settype($storage, "float");
$totalStorage = $oldStorage + $storage;
settype($totalStorage, "float");
if($albums != "")
{
if($file == true)
{
$newAlbums = $db . ";;;" . $albums;
}
else
{
$newAlbums = $albums;
}
}
else
{
if($file == true)
{
$newAlbums = substr($db, 0, strlen($db)-1);
}
else
{
$newAlbums = "";
}
}
if($totalStorage > 10)
{
$newAlbums = $albums;
$totalStorage = $oldStorage;
}
//delete current version of user in db
$query = "DELETE FROM main WHERE user = '$user'";
$result = mysql_query($query, $link) or die('Query failed: ' . mysql_error());
$query2 = "INSERT INTO main VALUES ('$user', '$password', '$email', '$location', '$about', '$interests', '$ourFamily', '$network', '$messages', '$wholeBlog', '$newAlbums', '$totalStorage', '$signupDate')";
$result = mysql_query($query2, $link) or die('Query failed: ' . mysql_error());
// Closing connection
mysql_close($link);
?>
<html>
<head>
<title>New Album</title>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.efamz.com/edit.php">
<link rel="StyleSheet" type="text/css" href ="css.css">
</head>
<body>
</body>
</html>