Jump to content

send multiple file with php and html5 form


arturg
 Share

Recommended Posts

i have problem with send muliple files, for one file it works, for multiple not, what should I do with this code ?

<?php
 
   if($_POST){
    unset($_POST["send"]);
    //print_r($_POST);
    unset($_POST["pliki"]);
    
    $p = new Page();
    $p->template = 'page';
    $p->parent = '1043';
    $p->user_id = $user['id'];
    
    foreach($_POST as $v=>$pr){
        if($pr!=''){
            $p->{$v} = $pr;
        }
    }
    $p->save();
    
    
    $pp = $pages->get($p->id);
    $pp->setOutputFormatting(false);
    if(isset($_FILES['pliki'])){
        $errors= array();
        $file_name = $_FILES['pliki']['name'];
        $file_size =$_FILES['pliki']['size'];
        $file_tmp =$_FILES['pliki']['tmp_name'];
        $file_type=$_FILES['pliki']['type'];
        $file_ext=strtolower(end(explode('.',$_FILES['pliki']['name'])));
 
        //$name = strtolower($sanitizer->name($file_name)); 
        //'../../site/assets/files/'.$p->id.'/'.$name)
        
        $expensions= array("jpeg","jpg","png","docx","doc","pdf");
 
        if(in_array($file_ext,$expensions)=== true){
            move_uploaded_file($file_tmp,"../../site/assets/files/".$pp->id."/".strtolower($sanitizer->name($file_name)));
            $pp->pliki->add($file_name);
        }
    }
    $pp->save();
    
    
}
 
?>
My input looks like this:
<input type="file" id="pliki" name="pliki[]" multiple>
Edited by netcarver
Corrected bounds of your code block
Link to comment
Share on other sites

  • 2 weeks later...

Hi Arturg,

  I don't know if you were able to get your code working or not. LostKobrakai's suggestion is very solid of course. I just thought this link to good old StackOverflow might also help you see why the code you posted will only pick up one file.

http://stackoverflow.com/questions/5444827/how-do-you-loop-through-files-array

Search for the answer given by Decent Dabbler . I think his or her answer would be the easiest to adapt to your code to get it working. Basically the problem with the code you posted is that it isn't iterating through the $_FILES array. As you said, it's working for one file, so wrapping it in a foreach loop, as done in the example should fix things up for you.

Throwing out advice like this in a forum is new to me. I've never participated in a forum before. I hope what I've posted is of some additional help. It keeps me out of trouble. 

Have a good one...

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...