0
votes

I am struggling to understand how to retrieve additional form data when uploading files with Blueimp jQuery File Upload. I am tying to pass a value to be appended to the file name when it is uploaded...

I have an hidden input in my #fileupload form...

<input type="hidden" name="referrer" id="referrer" value="123">

I have added this to my main.js...

$('#fileupload').bind('fileuploadsubmit', function (e, data) {
    data.formData = $('form').serializeArray();
});

and this to index.php in the php folder...

$smeg = $_POST['referrer'];

What I now can't work out is how to use the variable $smeg in UploadHander.php

I ideally I would like to add the variable to the file name using the below as an example

protected function trim_file_name($file_path, $name, $size, $type, $error,$index, $content_range) {
   return $name. ' - '.$smeg;
}

but whenever I try and use $smeg I get the error $smeg is an undefined variable.

Am I passing the additional form data correctly and if so I do I retrieve it to use if?

1
$smeg is not defined in a scope of trim_file_name - u_mulder
@u_mulder so do I use $smeg = $_POST['referrer'] in trim_file_name? That is the bit I don't understand - tatty27
Or pass it as argument. - u_mulder
sorted, thank you, if you add it as an answer I will accept it - tatty27

1 Answers

1
votes

In your function scope $smeg is not defined. So you either can use $_POST['referrer'] inside function:

protected function trim_file_name($file_path, $name, $size, $type, $error,$index, $content_range) {
   return $name . ' - ' . $_POST['referrer'];
}

Or add another argument to function signature:

protected function trim_file_name($file_path, $name, $smeg, $size, $type, $error,$index, $content_range) {
   return $name . ' - ' . $smeg;
}

Call to function will be something like:

trim_file_name($path, $name, $_POST['referrer'], /* more arguments */);

Second approach is preferrable because it eliminates dependency from $_POST array in your code.

By the way, if you don't use all these arguments in your function - there's no need to define them in a signature and simplify function to something like:

protected function trim_file_name($name, $smeg)
{
    return $name . '-' . $smeg;
}