Jump to content

Multiple image upload problems!


johnnywishwash

Recommended Posts

I have been using this script to upload images to a server.  I have been trying to edit it so i can perform multiple uploads at once.  When i use the script though it decides not to work because it thinks the file extension is not correct (but it is).  Any thoughts on how to fix this problem or how to get my multiple upload working?

 

Here is the code

 

<?php

$idir = 'images/';  // Path To Images Directory

$twidth = "320";  // Maximum Width For Thumbnail Images

$theight = "240";  // Maximum Height For Thumbnail Images

$max_no_img = 4 ;

$rand = rand(0,10000000);

if (!isset($_GET['subpage'])) {

  // Image Upload Form Below   

 

 

echo '<form method="post" action="imageUpload.php?subpage=upload" enctype="multipart/form-data">';

 

for($i=1; $i<=$max_no_img; $i++){

echo '<input type= "file" id = "file" name= "imagefile[]" class="form">';

echo '<br /><br />';

}

echo '<input type="reset" value="Clear" class="form">';

echo '<br /><br />';

echo '<input name="submit" type="submit" value="Submit" class="form">';

echo '<br /><br />';

echo '</form>';

 

}

if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {  // Uploading/Resizing Script

 

$url = $_FILES['imagefile']['name'];  // Set $url To Equal The Filename For Later Use 

  if ($_FILES['imagefile']['type'] == 'image/jpg' || $_FILES['imagefile']['type'] == 'image/jpeg' || $_FILES['imagefile']['type'] == 'image/pjpeg') {

    $file_ext = strrchr($_FILES['imagefile']['name'], '.');  // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php

    $copy = copy($_FILES['imagefile']['tmp_name'], $idir . $_FILES['imagefile']['name'] );  // Move Image From Temporary Location To Permanent Location

    if ($copy) {  // If The Script Was Able To Copy The Image To It's Permanent Location

      echo '<h1>Image Upload Successful!</h1>';  // Was Able To Successfully Upload Image

      $simg = imagecreatefromjpeg($idir . $url);  // Make A New Temporary Image To Create The Thumbanil From

      $currwidth = imagesx($simg);  // Current Image Width

      $currheight = imagesy($simg);  // Current Image Height

      if ($currheight > $currwidth) {  // If Height Is Greater Than Width

        $zoom = $twidth / $currheight;  // Length Ratio For Width

        $newheight = $theight;  // Height Is Equal To Max Height

        $newwidth = $currwidth * $zoom;  // Creates The New Width

      } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)

        $zoom = $twidth / $currwidth;  // Length Ratio For Height

        $newwidth = $twidth;  // Width Is Equal To Max Width

        $newheight = $currheight * $zoom;  // Creates The New Height

      }

      $dimg = imagecreate($newwidth, $newheight);  // Make New Image For Thumbnail

      imagetruecolortopalette($simg, false, 256);  // Create New Color Pallete

      $palsize = ImageColorsTotal($simg);

      for ($i = 0; $i < $palsize; $i++) {  // Counting Colors In The Image

      $colors = ImageColorsForIndex($simg, $i);  // Number Of Colors Used

      ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);  // Tell The Server What Colors This Image Will Use

      }

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);  // Copy Resized Image To The New Image (So We Can Save It)

      imagejpeg($dimg, $idir.$rand . $url);  // Saving The Image

      imagedestroy($simg);  // Destroying The Temporary Image

      imagedestroy($dimg); // Destroying The Other Temporary Image

      echo '<h1>Image Resized!</h1><form><input type=button value="Refresh" onClick="revertRefresh()"></form>';  // Resize successful

    } else {

      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';  // Error Message If Upload Failed

    }

  } else {

    print '<font color="#FF0000">ERROR: Wrong filetype it has to be a .jpg or .jpeg. Yours is ';  // Error Message If Filetype Is Wrong

    print $file_ext;  // Show The Invalid File's Extention

    print '.</font>';

  }

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/192202-multiple-image-upload-problems/
Share on other sites

step 1. create a function that saves one image, use the code you have embedded into the page and make a function out of it.

step 2. loop over your image data and call that function each time passing along the data it needs to create your image.

 

 

****

 

Looking at what you wrote, I'm not too sure you even have one working, as the data passed for your fields "imagefile" should be a numerically indexed array. ie:

 

for($i = 0; $i < count($_FILES['imagefile']); $i++) {
    $file_name = $_FILES['imagefile'][$i]['name'];
}

// Not: 
$file_name = $_FILES['imagefile']['name'];

 

 

  • 2 months later...

hey, don't know if you've found your answer or not...it's been a couple months, but I think I see an error that could've kept you from getting your code to work... :

 

if ($_FILES['imagefile']['type'] == 'image/jpg' || $_FILES['imagefile']['type'] == 'image/jpeg' || $_FILES['imagefile']['type'] == 'image/pjpeg') {

 

See the very last bit of code where it says 'image/pjpeg'?

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.