代码之家  ›  专栏  ›  技术社区  ›  Hacker

PHP中的foreach问题

  •  1
  • Hacker  · 技术社区  · 15 年前

    我有一个数组,比如 $_FILES 在AM打印中。

    Array ( [ANTI-HAV__TOTAL] => Array ( [name] => Array ( [0] => Firefox_wallpaper1.png [1] => Firefox_wallpaper2.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /tmp/phpr92AvZ [1] => /tmp/phpxmzia3 ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 353750 [1] => 353750 ) ) )
    

    但是当我试着用前臂的时候

    foreach($_FILES['ANTI-HAV__TOTAL']['name'] as $value1)
    

    它说

    Warning: Invalid argument supplied for foreach()  
    

    我如何解决这个问题?

    这是我的形式

    <?php
    $myresult .= '<div class=\'fieldForm\'>';
    $myresult .= '<form id=\'lab_form\' enctype=\'multipart/form-data\' method=\'POST\' action =\''.$this->url(array('controller'=>'lab', 'action' =>'uploadsave')).'\'>';
    $myresult .= '<fieldset>';
    $myresult .= '<legend>Lab Upload Form</legend>';
    foreach($labtests_break as $key => $value)
    {
    $nvalue = str_replace(' ','__',trim($value));
    $nvalue = str_replace('(','___',trim($nvalue));
    $nvalue = str_replace(')','____',trim($nvalue));
    if(!in_array($value,$gotImages2))
    {     
    $myresult .= '<div class=\'field50Pct\'>';
    /*$myresult .= '<input type=\'button\' id=\''.$nvalue.'Add\'  value=\'Add Another '.$value.' Image\' onclick=\'addElement(this.id)\' />';
    $myresult .= '<input type=\'button\' id=\''.$nvalue.'Del\'  value=\'Remove'.$value.' Image\' onclick=\'removeElement(this.id)\' disabled=\'disabled\/>';*/
    $myresult .= '</div>';
    $myresult .= '<div class=\'clear\'></div>';
    $myresult .= '<div class=\'clear\'></div>';
    $myresult .= '<div class=\'field50Pct\'>';
    $myresult .= '<div class=\'fieldItemLabel\'>';
    $myresult .= '<label for=\'\'>'.$value.'</label>';
    $myresult .= '</div>';
    $myresult .= '<div class=\'fieldItemValue\'>';
    $myresult .= '<div id=\''.$nvalue.'input1\'  class=\''.$nvalue.'clonedInput\'>';
    $myresult .= '<input type=\'file\' name=\''.$nvalue.'[]\'  id=\''.$nvalue.'1\' class="multi">';
    //$myresult .= '<input type=\'button\' style=\'width:50px;padding:2px;margin-left:10px;\' value=\'Clear\'  onclick=\'clearField(this)\'/>';
    $myresult .= '</div>';
    $myresult .= '</div>';
    $myresult .= '</div>';
    $myresult .= '<div class=\'clear\'></div>';
    }
    }
    $myresult .= '<input type=\'hidden\' name=\'visitId\'  value=\''.$visitId.'\' >';
    $myresult .= '<input type=\'hidden\' name=\'patientId\'  value=\''.$patientId.'\' >';
    $myresult .= '<input type=\'hidden\' name=\'tests\'  value=\''.$labtests.'\' >';
    $myresult .= '<input type=\'submit\' value=\'upload\'>';
    $myresult .= '</fieldset>';
    $myresult .= '</form>';
    $myresult .= '</div>';
    echo $myresult;
    ?>
    

    这是操作部分的控制器。

    public function uploadsaveAction()
    {
    $this->view->homeId = "current";
            $this->view->homeUrl = $this->view->url(array('controller' => 'lab', 'action' =>'index'));
            $this->view->labMenu = 'yes';
            $this->view->patientsId = "";
            $this->view->settingsId = "";
            $this->view->settingsURL = "";
    
             $request = $this->getRequest();
    if($request->isPost())
            {
                    $visitId        = $_POST['visitId'];
                    $patientId      = $_POST['patientId'];
                    $tests          = $_POST['tests'];
                    $tests_break    = explode(',',$tests);
                    foreach($tests_break as $key=>$value)
                    {
    
                            $nvalue = str_replace(' ','__',trim($value));
                            $nvalue = str_replace('(','___',trim($nvalue));
                            $nvalue = str_replace(')','____',trim($nvalue));
    
                            //for($i= 0; $i<=$limit ; $i++)
                            echo '<br/>';
                            print_r($_FILES);
                            echo '<br/>';
                            echo $nvalue;
                            foreach($_FILES[$nvalue]['name'] as $value1)
                            {
                            $key_found = array_search($value1, $_FILES[$nvalue]['name']); // $key = 2;
                            /*$check                = $nvalue.$i;*/
                            $filename       = $_FILES[$nvalue]['name'][$key];
                            $tmpname        = $_FILES[$nvalue]['tmp_name'][$key];
                            //echo $check;
                            //echo '<br/>';
                            if($filename != '')
                            {
    
                            $structure = '/var/www/html/hms-work/public/visit_images/'.$patientId.'/'.$visitId;
    
                            if(!is_dir($structure))
                            {
                            if (!mkdir($structure, 0777, true))
                             {
                                die('Failed to create folders...');
                             }
                            }
                            /** is_dir if ends here **/
                            if(is_dir($structure))
                            {
                                    if($_FILES[$nvalue]['error'][$key] == 0)
       if(file_exists($structure.'/'.$filename))
                                            {
    
                                            }
                                            else
                                            {
                                               if(move_uploaded_file($tmpname, "$structure/$filename"))
                                                    {
                                                     $imageUpload = new Application_Model_Imageupload();
                                                     $imageUploadMapper = new Application_Model_ImageuploadMapper();
    
                                                     $imageUpload->setVisitid($visitId)
                                                                    ->setType($value)
                                                                    ->setImagename($filename)
                                                                    ->setCreateddate(date('Y-m-d H:i:s'))
                                                                    ->setCreatedby($this->session->userId);
    
                                                    $imageUploadMapper->save($imageUpload);
                                                    $this->view->message = 'Succesfully uploaded the images';
    
                                                    }
                                            }
                                    }
                            }
    
                            }
                            }
    
                    }
    //              print_r($_POST);
            //      print_r($_FILES);
    
            }
            else
            {
            }
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Matt Gibson    14 年前

    尝试

    var_dump($_FILES['ANTI-HAV__TOTAL']);
    

    首先看看你的钥匙是否正确。