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/>'; } } } 
7
  • let me ask, you are uploading 6 files and 5 uploaded and 6th not? Commented Jul 16, 2015 at 8:49
  • 1
    try to set php.ini - set_time_limit(0); Commented Jul 16, 2015 at 8: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? Commented Jul 16, 2015 at 8: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. Commented Jul 16, 2015 at 9:11
  • All the files are Jpg files and are all 4mb or under. So the size part shouldn't really cause an error. Commented Jul 16, 2015 at 9:12

1 Answer 1

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++) { 
Sign up to request clarification or add additional context in comments.

3 Comments

PHP arrays are zero-based.
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.
@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 stackoverflow.com/questions/31425743/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.