Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Last active December 17, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongilbert/5613482 to your computer and use it in GitHub Desktop.
Save dongilbert/5613482 to your computer and use it in GitHub Desktop.
Don't override your variables. Can't you spot the error?
<?php
class Foo
{
public function save($key = null, $urlVar = null)
{
if (!empty($_FILES['form']))
{
foreach($_FILES['form']['name'] as $field => $val)
{
// Make sure we're just dealing with the `pdf` field.
if ($field !== 'pdf')
{
continue;
}
$file = new stdClass;
foreach ($_FILES['form'] as $key => $value)
{
$file->$key = $value[$field];
}
// <snip>
}
}
parent::save($key, $urlVar);
}
}
@betweenbrain
Copy link

My guess would be $val. But, then why would you also loop $_FILES['form'] within itself? <head spins/>

@dongilbert
Copy link
Author

I'm looping $_FILES['form'] twice because of the way PHP $_FILES gets populated. The first time I'm just grabbing the files name and putting it into a $field var and then looping again to get the actual values for that specific uploaded file. But that's not the real issue. Looking at it now, I just realized I messed up the code while simplifying it for the gist. I've updated it to reflect a better scenario.

The problem with the code is it overwrites the $key variable and then passes that to the parent::save($key, $urlVar) method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment