0

Hi

i am using array for uploading images but i am unable to insert it into database as well as in the folder. can anyone please help me to resolve it.

            $name = $_GET['name'];
            $gititle = $_POST['gititle'];


            $number_of_file_fields = 0;
            $number_of_uploaded_files = 0;
            $number_of_moved_files = 0;
            $uploaded_files = $_POST[$_FILES['images']['name'][$i]];
            echo $uploaded_files;
            $upload_directory = '../images/gallery/'.$name.'/'.$gititle.'/';
            echo $upload_directory;
    //set upload directory
    /**
     * we get a $_FILES['images'] array ,
     * we procee this array while iterating with simple for loop 
     * you can check this array by print_r($_FILES['images']); 
     */
    for ($i = 0; $i < count($_FILES['images']['name']); $i++) 
    {
        $number_of_file_fields++;

        if ($_FILES['images']['name'][$i] != '') 

            { //check if file field empty or not

            $number_of_uploaded_files++;

            $uploaded_files[$i] = $_FILES['images']['name'][$i];
            echo $uploaded_files[$i];
            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {

            echo $sql=mysql_query("INSERT INTO ginnergallery (ginname,gifilename,giname)VALUES('".$_POST["gititle"]."', '".$uploaded_files[$i]."', '".$_POST["name"]."')");

            $number_of_moved_files++;

            }
            }

        }

Javascript Code

<script type="text/javascript">
/* function add_file_field(){
var container=document.getElementById('file_container');
var file_field=document.createElement('input');
file_field.name='images[]';
file_field.type='file';
container.appendChild(file_field);
var br_field=document.createElement('br');
container.appendChild(br_field);
}*/
$(document).ready(function() { 
var FileInputsHolder    = $('#AddFileInputBox');
var MaxFileInputs       = 30; //Maximum number of file input boxs
// adding and removing file input box
var i = $("#AddFileInputBox div").size() + 1;
$("#AddMoreFileBox").click(function () {
        event.returnValue = false;
        if(i < MaxFileInputs)
        {
            $('<span><input type="file" id="fileInputBox" size="20" name="upl[]" class="addedInput" value=""/><a href="#" class="removeclass small2"><img src="images/close_icon.gif" border="0" /></a></span>').appendTo(FileInputsHolder);
            i++;
        }
        return false;
});
$("body").on("click",".removeclass", function(e){
        event.returnValue = false;
        if( i > 1 ) {
                $(this).parents('span').remove();i--;
        }

}); 
});
</script>

Form

 <form class="form-horizontal form-separate" enctype="multipart/form-data" method="post" role="form">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h6 class="panel-title"><i class="icon-page-break"></i> Add New Gallery</h6>
        </div>
        <div class="panel-body">
          <div class="form-group">
            <label class="col-md-2 control-label">Title:</label>
            <div class="col-md-10">
              <input name="gititle" type="text" required class="form-control" value="<?php echo $gres['gititle']; ?>" id="gititle">
              <input name="name" type="hidden" class="form-control" id="name" value="<?php echo $name; ?>">
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-2 control-label">Image:<br><span class="small"><a href="#" id="AddMoreFileBox">Add More Files</a></span></label>
                <div class="col-md-10">
                    <div id="AddFileInputBox"><input id="fileInputBox" style="margin-bottom: 5px;" type="file"  name="images[]"/></div>
                </div>
          </div>
          <div class="form-actions text-right">
            <input name="Reset" type="reset" class="btn btn-danger" value="Cancel">
            <input name="Submit" type="submit" class="btn btn-primary" value="Submit">
          </div>
        </div>
      </div>
    </form>

Edited by rpv_sen: update

2
Contributors
1
Reply
21
Views
3 Years
Discussion Span
Last Post by Rahman_1
0

What do you ask ?
you want to create a new folder when uploading a file? then the name of the image file are entered into the database .

Using mkdir(), If you want to make folder according to the title in form the input and the images in a folder

mkdir('uploads/'.$name);
 if (move_uploaded_file($tmp_name,  'uploads/'.$name.'/'.$gititle))
    die("success");
 else
 die("error");      

If you want to Add More Files, try to use this
<span class="small"><a href="#" id="AddMoreFileBox">Add More Files</a></span>

Replace with this

<span class="add_more"><a href="#" id="AddMoreFileBox">Add More Files</a></span>  

Call JS

    <script type='text/javascript'>
     $(document).ready(function(){
        $('.add_more').click(function(e){
            e.preventDefault();
            $(this).before("<p><input name='image[]' type='file'/></p>");
        });
    });
    </script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.