-1

I'm a beginner in jQuery. I really don't know how to do this but have the basic idea.

I'm using this jQuery uploader by Hayageek and I'm trying to move uploaded files to its permanent directory after form submits.

So, this is how it goes. The user first selects the file and the upload to TEMP directory starts and just stores an json array. But then the user accidentally adds a file that he never meant to and wants to remove that file (removing the array object). After correcting his mistake he then submits the form and the files get stored in a permanent directory.

This is what I have:

upload.php:

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

    $error =$_FILES["myfile"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if(!is_array($_FILES["myfile"]["name"])) //single file
    {
        $fileName = $_FILES["myfile"]["name"];
$ret[]= $fileName;
    }
    else  //Multiple files, file[]
    {
      $fileCount = count($_FILES["myfile"]["name"]);
      for($i=0; $i < $fileCount; $i++)
      {
        $fileName = $_FILES["myfile"]["name"][$i];
        $ret[]= $fileName;
      }

    }
    echo json_encode($ret);
 }

Delete file:

//This is the part where you unset the array object of a file.

index.php:

    <script>
    $(document).ready(function()
    {
    var settings = {
        url: "upload.php",
        dragDrop:true,
        fileName: "myfile",
        allowedTypes:"jpg,png,gif,doc,pdf,zip", 
        returnType:"json",
         onSuccess:function(files,data,xhr)
        {
           // alert((data));
        },
        showDelete:true,
        deleteCallback: function(data,pd)
        {
        for(var i=0;i<data.length;i++)
        {
            $.post("delete.php",{op:"delete",name:data[i]},
            function(resp, textStatus, jqXHR)
            {
                //Show Message  
                $("#status").append("<div>File Deleted</div>");      
            });
         }      
        pd.statusbar.hide(); //You choice to hide/not.

    }
    }
    var uploadObj = $("#mulitplefileuploader").uploadFile(settings);


    });
    </script>
    <form action="upload.php" name="upload_form" enctype="multipart/form-data" method="POST">
        <div>
         <label>Message</label>
         <textarea name="description" style="min-height:200px;" value="<?php echo $mess; ?>"></textarea>
         <div id="mulitplefileuploader">Upload</div>
         <div id="status"></div>
         <input type="submit" name="submit" value="Create Project">
        </div>
   </form>

<?php if(isset($_POST['submit'])){

    $tmp_file=$_FILES['myfile']['tmp_name'];
    $dir="upload/";
    $file_name=$_FILES['myfile']['name'];

    if(move_uploaded_file($tmp_file,$dir . $file_name)){
        echo "success";
    }else{echo "failure";}
}

 ?>
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
  • 1
    this answer maybe help http://stackoverflow.com/a/19296001/3615630 – Mohammad Alabed Sep 08 '15 at 13:24
  • 1
    question is unclear. you have 2 different files arrays `$_FILES["myfile"]` and `$_FILES['file_upload']` and no submit button to match the conditional statement `if(isset($_POST['submit']))` – Funk Forty Niner Sep 08 '15 at 13:26
  • 1
    and also there are many plugin can help you, just google it then use them, why you try to rebuild every thing again if somebody did it before http://www.codexworld.com/upload-multiple-images-using-jquery-ajax-php/ – Mohammad Alabed Sep 08 '15 at 13:26
  • @Fred-ii- Thanks for letting me know that. I had a long form so I decided to make it only with one field as an example. Also, please don't down vote my question:(. – Ikhlak S. Sep 08 '15 at 16:04

1 Answers1

0

You can refer some of plugins available online.

https://blueimp.github.io/jQuery-File-Upload/basic-plus.html this is one of them...

Meeshal
  • 397
  • 1
  • 6
  • 10