Hello,

I have no such information or you can say less information about the ajax can anyone help me out the thing is that i want to pass dir id in the get url but that get id will be selected by the user though from a drop down i mean the user will select the dir name i mean dir name then he will upload the image on same section so how do i pass the dir name.

<div id="mulitplefileuploader">Upload</div>

                    <div id="status"></div>
                    <select name="dirname">
                        <option name="1">Sports Images</option>
                        <option name="2">Bike Images</option>
                        <option name="3">Nature Images</option>
                        <option name="4">Sunset Images</option>
                        <option name="5">3d Images</option>
                    </select>
                    <?php $dir = $_REQUEST["dirname"]; ?>
                    <script>

                    $(document).ready(function()
                    {

                    var settings = {
                        url: "upload.php?uid=<?php echo $_SESSION["uid"]."&dirname=".$dir;?>",
                        method: "POST",
                        allowedTypes:"jpg,png,gif",
                        fileName: "myfile",
                        multiple: true,
                        onSuccess:function(files,data,xhr,dirname)
                        {
                            $("#status").html("<font color='green'>Upload is success</font>");

                        },
                        onError: function(files,status,errMsg)
                        {       
                            $("#status").html("<font color='red'>Upload is Failed</font>");
                        }
                    }
                    $("#mulitplefileuploader").uploadFile(settings);

                    });
                    </script>
Member Avatar for diafol

Ajax file upload quite tricky to do. But where are your file fields? Server-side code?

user_upload.php

<!-- MyModel2 -->
        <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                 <h4 class="modal-title" id="gridSystemModalLabel" style="text-align:center;">Existing Gallery</h4>
              </div>
              <div class="modal-body" style="padding:15px 0;">
              <div id="basic-upload">
                    <div id="mulitplefileuploader">Upload</div>

                    <div id="status"></div>

                    <select class="dates">
                        <option value="my Dir new" data-value="my Dir new">my Dir new</option>
                        <option value="my Dir new 1" data-value="my Dir new 1">my Dir new 1</option>
                        <option value="My Dir" data-value="My Dir">My Dir</option>
                    </select>

                    <div class="numValue"><?php echo $dir = ''; ?></div>
                    <script>
                                                            $('select.dates').change(function() {
   $('.numValue').val($(this.options[this.selectedIndex]).attr('data-value'));
});
                    $(document).ready(function()
                    {

                    var settings = {
                        url: "upload.php?uid=<?php echo $_SESSION["uid"]."&dir=".$dir;?>",
                        method: "POST",
                        allowedTypes:"jpg,png,gif",
                        fileName: "myfile",
                        dirname: "dirname",
                        multiple: true,
                        onSuccess:function(files,data,xhr,dirname)
                        {
                            $("#status").html("<font color='green'>Upload is success</font>");

                        },
                        onError: function(files,status,errMsg)
                        {       
                            $("#status").html("<font color='red'>Upload is Failed</font>");
                        }
                    }
                    $("#mulitplefileuploader").uploadFile(settings);

                    });
                    </script>
            </div>
                <div class="modal-footer">
                <button type="button" style="float:left;margin:0;" class="sm-button sm-button-size-large sm-button-skin-submit yui3-u-1-2" data-dismiss="modal">Close</button>

              </form>
              </div> </div></div></div>
              </div>

            </div>
          </div>

upload.php

<?php
session_start();
require_once("includes/connection.php");
$uid = $_GET["uid"];
$dir_name = $_GET["dir"];
$query = mysqli_query($connection, "SELECT * FROM usersdata WHERE uid='$uid'");
while($row = mysqli_fetch_assoc($query)) {
    $dir_id   = $row["dir_id"];
}

//If directory doesnot exists create it.
$output_dir = "usersdata/".$dir_name."/";


if(isset($_FILES["myfile"]))
{
    $ret = array();

    $error =$_FILES["myfile"]["error"];
   {

        if(!is_array($_FILES["myfile"]['name'])) //single file
        {
            $fileName = $_FILES["myfile"]["name"];
            move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
             //echo "<br> Error: ".$_FILES["myfile"]["error"];
                $query = mysqli_query($connection, "INSERT INTO usersfolder (image_name, dir_id, uid) VALUES ('$fileName', '$dir_id', '$uid')");
                 $ret[$fileName]= $output_dir.$fileName;
        }
        else
        {
                $fileCount = count($_FILES["myfile"]['name']);
              for($i=0; $i < $fileCount; $i++)
              {
                $fileName = $_FILES["myfile"]["name"][$i];
                 $ret[$fileName]= $output_dir.$fileName;
                move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName );
                $query = mysqli_query($connection, "INSERT INTO usersfolder (image_name, dir_id, uid) VALUES ('$fileName', '$dir_id', '$uid')");
              }

        }
    }
    echo json_encode($ret);
}

?>

only these both files are here no any other files are used

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.