0

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?

4
  • $smeg is not defined in a scope of trim_file_name Commented Mar 25, 2017 at 19:11
  • @u_mulder so do I use $smeg = $_POST['referrer'] in trim_file_name? That is the bit I don't understand Commented Mar 25, 2017 at 19:13
  • Or pass it as argument. Commented Mar 25, 2017 at 19:14
  • sorted, thank you, if you add it as an answer I will accept it Commented Mar 25, 2017 at 19:18

1 Answer 1

1

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

Comments

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.