1

Below is a code I have tinkered with and changed to suit my website needs. It grabs the images and then uploads them individually, making a thumbnail image and then recoridng the data into a database.

The trouble I have is when I upload 5/6 at a time it sometimes doesn't upload anything nor specify whats wrong. I added (error_reporting(~0); ini_set('display_errors', 1);) to check the errors but still nothing shone a light as to what was going on.

Any help would be much appreciated.

PS:- php.ini is set to:-

upload_max_filesize = 60M  
post_max_size = 60M
max_execution_time = 90
max_file_uploads = 30

PHP

$j = 0;     
$target_path = "../../images/";     
$newimagenumber=$currentimages;
$imgname = 0;
$ImgFileName = 0;
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
  $target_path = "../../images/";     
  $imgname = 0;
  $ImgFileName = 0;
  $newimagenumber=$newimagenumber+1;
  $imgname=$_POST['FileName'];
  $validextensions = array("jpeg", "jpg", "png", "JPG");      
  $ext = explode('.', basename($_FILES['file']['name'][$i]));  
  $file_extension = end($ext); 
  $ImgFileName="$imgname-$newimagenumber";
  $ImgFileNameExt="$imgname-$newimagenumber.jpg";
  $target_path = $target_path . $ImgFileName . ".jpg";     
  $j = $j + 1;  
  if (($_FILES["file"]["size"][$i] < 5000000) && in_array($file_extension, $validextensions)) {
    if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
      $InsertImgFile = mysql_query("INSERT INTO image_data (image_url,image_name,image_customerid,FileName) VALUES ('$ImgFileNameExt','$ImgFileNameExt','$customer_id','$imgname')") or die(mysql_error()); 
      include('CreateNails.php');
      // If file moved to uploads folder.
      echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/>';
    } else {     //  If File Was Not Moved.
      echo $j. ').<span id="error">please try again!.</span><br/>';
    }
  } else {     //   If File Size And File Type Was Incorrect.
    echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/>';
  }
}
}
Burki
  • 1,188
  • 19
  • 28
Earunder
  • 177
  • 1
  • 11
  • let me ask, you are uploading 6 files and 5 uploaded and 6th not? – Shehary Jul 16 '15 at 08:49
  • 1
    try to set php.ini - set_time_limit(0); – wishab Jul 16 '15 at 08:50
  • can you clarify a bit? are you getting this problem randomly, or only with some special files? is the file size a candidate for the trouble, or the time? – Burki Jul 16 '15 at 08:57
  • Hi thanks for the response. When it fails it just reloads the page from the very start and doesn't show any errors or upload any images nor added any data to the database. It doesn't fail all the time though which is what's puzzling me. When I upload 5 it works fine. When I upload more than 5 it works sometimes and others it just reloads the page to the start. – Earunder Jul 16 '15 at 09:11
  • All the files are Jpg files and are all 4mb or under. So the size part shouldn't really cause an error. – Earunder Jul 16 '15 at 09:12
  • The script finishes within the set 90 second time frame. Around 50 seconds. – Earunder Jul 16 '15 at 09:13
  • What is your memory_limit setting in php.ini? – Ondřej Šotek Jul 16 '15 at 09:58

1 Answers1

1

If the problem is that you are unable to upload the 6th file out of total 6 files then the problem is here with your counter

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

Your number of file must be less / equal to the counter or you will miss the last file, suppose $_FILES['file'] are 6 and your counter is also 6 but you put condition < so it will ignore the 6th file, so your conidtion must be <=

for ($i = 0; $i <= count($_FILES['file']['name']); $i++) {
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • PHP arrays are zero-based. – Burki Jul 16 '15 at 08:56
  • Hi thanks for the response. When it fails it just reloads the page from the very start and doesn't show any errors or upload any images nor added any data to the database. It doesn't fail all the time though which is what's puzzling me. – Earunder Jul 16 '15 at 09:08
  • @AshleyDye your are defining variable twice before the counter and again after the counter e.g this `$imgname = 0;` you can check my answer here, might be helpfull http://stackoverflow.com/questions/31425743/how-to-upload-store-multiple-images-in-mysql-database-in-php – Shehary Jul 16 '15 at 09:13