Hello,
I’m currently creating a website where my members can create a photo album and upload their photos. I’ve tested my php script and the script is taking too long to upload an image. Is there a way to speed up the upload process?
Hello,
I’m currently creating a website where my members can create a photo album and upload their photos. I’ve tested my php script and the script is taking too long to upload an image. Is there a way to speed up the upload process?
Could you please post the code you currently have.
<?php
// no files uploaded
if (count(array_unique($_FILES['image']['error'])) == 1 and $_FILES['image']['error'][0] == 4)
{
Error("No files were uploaded!");
}
// uploaded files
else {
for ($i=0; $i<$max_uploadnr; $i++)
{
$thumbwidth = 125;
$thumbheight = 125;
// file not uploaded
if ($_FILES['image']['error'][$i] == 4) {/* do nothing here */}
// file exceeds filesize
elseif ($_FILES['image']['error'][$i] == 1 or $_FILES['image']['error'][$i] == 2)
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: the file exceeds the filesize limit!<p>");
}
// files partially uploaded
elseif ($_FILES['image']['error'][$i] == 3)
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: the file was partially uploaded!<p>");
}
// temporary folder missing
elseif ($_FILES['image']['error'][$i] == 6)
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: the temporary folder is missing!<p>");
}
// the file is not an HTTP upload
elseif (!is_uploaded_file($_FILES['image']['tmp_name'][$i]))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: the file was not an HTTP upload!<p>");
}
// the file is not an image
elseif (!getimagesize($_FILES['image']['tmp_name'][$i]))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: the file is not an image!<p>");
}
// uploaded image not allowed
elseif (!in_array($_FILES['image']['type'][$i],array_keys($upload_types)))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: this image type is not allowed!<p>");
}
// everything seems ok, save the image
else {
// make a unique filename, check it is not already taken
$now = time();
while(file_exists($uploadfilename1[$i] = $upload_dir.$now))
{
$now++;
}
$uploadfilename2[$i] = $upload_dir."thumb_".$now;
$filename = $now;
$filetype = $_FILES['image']['type'][$i];
$imageinfo[$i] = getimagesize($_FILES['image']['tmp_name'][$i]);
// unable to move the image
if (!move_uploaded_file($_FILES['image']['tmp_name'][$i],$uploadfilename1[$i]))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to move the image!<p>");
}
else {
// create thumbnails
$sourceimage = '';
switch ($imageinfo[$i][2])
{
case 1: // 1 is GIF
$sourceimage = imagecreatefromgif($uploadfilename1[$i]);
break;
case 2: // 2 is JPG
$sourceimage = imagecreatefromjpeg($uploadfilename1[$i]);
break;
case 3: // 3 is PNG
$sourceimage = imagecreatefrompng($uploadfilename1[$i]);
break;
}
if (empty($sourceimage))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to copy image!<p>");
}
else {
// obtain the ratio of the original image so we don't distort
$sourceratio[$i] = $imageinfo[$i][0] / $imageinfo[$i][1];
// Resize our thumbnail to match the proportions of the original
if ($thumbwidth/$thumbheight > $sourceratio[$i]) {$thumbwidth = $thumbheight * $sourceratio[$i];}
else {$thumbheight = $thumbwidth / $sourceratio[$i];}
// create the image object for the thumbnail
$destImageThumbnail[$i] = ImageCreateTrueColor($thumbwidth, $thumbheight);
// Resample the source image into the thumbnail
if (!imagecopyresampled($destImageThumbnail[$i], $sourceimage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imageinfo[$i][0], $imageinfo[$i][1]))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to resample this image!<p>");
}
elseif (!imagejpeg($destImageThumbnail[$i], $uploadfilename2[$i]))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to create thumbnail for this image!<p>");
}
else {
// Let's clean up after ourselves and free up the memory use by images
imagedestroy($destImageThumbnail[$i]);
imagedestroy($sourceimage);
// update database
if (!@mysql_query("UPDATE Folders SET imgcount=imgcount+1,uploads=uploads+1,lastupload=CURDATE() WHERE autoid='{$_POST['folder']}'"))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to store image in database (3)!<p>");
}
elseif (!@mysql_query("UPDATE Albums SET imgcount=imgcount+1,uploads=uploads+1,lastupload=CURDATE() WHERE userid='{$_SESSION['userid']}'"))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to store image in database (4)!<p>");
}
elseif (!@mysql_query("INSERT INTO Images (folderid,filename,filetype,filesize,fileheight,filewidth,uploaddate) VALUES ('{$_POST['folder']}','$filename','$filetype','{$_FILES['image']['size'][$i]}','{$imageinfo[$i][1]}','{$imageinfo[$i][0]}',CURDATE())"))
{
Error("<b>".$_FILES['image']['name'][$i]."</b>: unable to store image in database (2)!<p>");
}
else {
$width = 640;
$height = 480;
// obtain the ratio of the original image so we don't distort
$ratio = $imageinfo[$i][0] / $imageinfo[$i][1];
// Resize our thumbnail to match the proportions of the original
if ($width/$height > $ratio) {$width = $height * $ratio;} else {$height = $width / $ratio;}
echo "<p>".$_FILES['image']['name'][$i];
?>
<img src="display.image.php?userid=<?php echo $_SESSION['userid']; ?>&filename=<?php echo $filename; ?>&filetype=<?php echo $filetype; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" border="2" style="border-color:#ffffff" />
Size <?php echo $imageinfo[$i][0]; ?>x<?php echo $imageinfo[$i][1]; ?>, <?php echo Filesizeformat($_FILES['image']['size'][$i]); ?>
<?php
}
}
}
}
}
}
}
?>