Kingfox
Kingfox

Reputation: 129

jQuery submit form and php

I'm making a upload form and have chosen to do this with jQuery. The file gets uploaded but not into the desired folder, so im not parsing the data correct from the upload form to the process.

upload.php

<script>
$(document).ready(function()
{

var settings = {
    url: "upload_process.php",
    method: "POST",
    allowedTypes:"jpg,jpeg,png",
    fileName: "myfile",
    galleryName: "<?php echo $gallery->folder; ?>",
    multiple: true,
    onSuccess:function(files,data,xhr)
    {
        $("#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>

upload_process.php

$galleryName = $_POST["galleryName"];
$output_dir = "media/images/".$galleryName."/";

if(isset($_FILES["myfile"])) {
    $ret = array();
    $error = $_FILES["myfile"]["error"];
    {
        /* Single File */
        if(!is_array($_FILES["myfile"]['name'])) {
            $fileName = $_FILES["myfile"]["name"];
            move_uploaded_file($_FILES["myfile"]["tmp_name"], $output_dir . $_FILES["myfile"]["name"]);
            $ret[$fileName] = $output_dir.$fileName;
        /* Multiple files */    
        } 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 );
            }
        }
    }
    echo json_encode($ret);
}

The file is uploaded to media/images/ and can't see why the $galleryName is not set?

Upvotes: 0

Views: 48

Answers (1)

Marcin Gałczyński
Marcin Gałczyński

Reputation: 590

The parameter passing to the script does not seem to be right. You did not specify the exact jQuery plugin that is being used, so the below example might not work, but if so, it should at least give You a good hint about what to look for in the plugin documentation

Please remove the line

galleryName: "<?php echo $gallery->folder; ?>",

And replace with lines

enctype: "multipart/form-data", // Upload Form enctype.
formData: { galleryName: "<?php echo $gallery->folder; ?>" },

Upvotes: 1

Related Questions