Last active
December 17, 2015 12:59
-
-
Save dongilbert/5613482 to your computer and use it in GitHub Desktop.
Don't override your variables. Can't you spot the error?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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
My guess would be
$val
. But, then why would you also loop$_FILES['form']
within itself?<head spins/>