-1

i really need help as this has been giving me problem all day. i only need to upload images from 4 input fields. yet only image from the last input field gets uploaded CODE(function to upload image):

function loadImage($file){

        $valid_types = array('jpg','jpeg','png');//allowed img extensions

        $ext = strtolower(end(explode('.', $file['name'])));

        $size = $file['size'];          

        if(in_array($ext, $valid_types)){

                if($size < (2097152)){

                        $image_path = substr(md5(time()), 0, 10).'.'.$ext;

                        if(move_uploaded_file($file['tmp_name'] , "../props/".$image_path)){
                                return $image_path;
                        }else{
                            echo "error";
                        }

                }else{

                    echo "Image  must be less than 2mb";
                }

        }else{
            //echo "image has an invalid file format";
        }

}

code to call upload function

$img1 = isset($_FILES['img1'])  ? loadImage($_FILES['img1']) : NULL ;
$img2 = isset($_FILES['img2'])  ? loadImage($_FILES['img2']) : NULL ;
$img3 = isset($_FILES['img3'])  ? loadImage($_FILES['img3']) : NULL ;
$img4 = isset($_FILES['img4'])  ? loadImage($_FILES['img4']) : NULL ;

jquery ajax code:

        formdata.append('img1', img1);
        formdata.append('img2', img2);
        formdata.append('img3', img3);
        formdata.append('img4', img4);

$.ajax({
            url     : 'core/upload.php',
            type    : 'POST',
            data    : formdata,
            contentType : false,
            processData : false,
            success : function (ep){
                alert(ep);

                //$('#say').html(ep);
            }
        });
Dapo Michaels
  • 380
  • 2
  • 4
  • 16
  • It's recommended to send them in a `for()` loop. See reference http://stackoverflow.com/questions/19295746/how-to-upload-multiple-files-using-php-jquery-and-ajax here – node_modules Sep 08 '16 at 16:44
  • 1
    done any basic debugging, like `var_dump($_FILES)` to see what arrived? And note that you're simply ASSUMING the uploads never fail. There's a `['error']` parameter in $_FILES for a reason. – Marc B Sep 08 '16 at 16:44
  • when i alert the $image_path it gives me paths for 4 of them but only 1 gets uploaded to the folder. – Dapo Michaels Sep 08 '16 at 16:45
  • i tried using both for and foreach array i still got the same result – Dapo Michaels Sep 08 '16 at 16:45
  • 1
    Are those `$image_path`s actually different? `time()` returns time in seconds. If all `$image_path`s are created in the same second, all will have the same filename. – gre_gor Sep 08 '16 at 17:08
  • o really?how can i solve that please – Dapo Michaels Sep 08 '16 at 17:41

1 Answers1

0

it works now,.painfully not through jquery but just through normal processing of form directly with php. this is how my loop looks like

for($i=0; $i<count($_FILES['img']['name']); $i++){
        $target_path = "../props/";

        $ext =  strtolower(end(explode('.', $_FILES['img']['name'][$i])));

        $target_path = $target_path . md5(uniqid()) . "." . $ext; 

        if(move_uploaded_file($_FILES['img']['tmp_name'][$i], $target_path)) {
            echo "The file has been uploaded successfully <br />";
        } else{
            echo "There was an error uploading the file, please try again! <br />";
        }
    }

but can anyone help out with how to still send it through jquery along with other text input and checkbox fields? the problem is coming from how i send it from jquery to php.

Dapo Michaels
  • 380
  • 2
  • 4
  • 16