0

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?

4
  • $gallery->folder has a valid value when you run this code? Commented Aug 29, 2017 at 21:11
  • Yes, I have printed the value above the provided code in here, so there is an actual value in the object $gallery->folder. Commented Aug 29, 2017 at 21:13
  • 2
    what is uploadFile()? some jquery plugin? may be "galleryName" isn't a recognized value by that function and not sent to the server. My guess, if it is a plugin, probably you have to use another key name to add custom data. Commented Aug 29, 2017 at 21:25
  • You was right, it wasnt parsed in the javascript file.. sorry I totally missed that call, was to focused on the form. Commented Aug 29, 2017 at 21:59

1 Answer 1

1

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; ?>" },
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! this did the trick :) if anyone was wondering i use: hayageek.com/jquery-multiple-file-upload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.