2013-02-05 116 views
0

我試圖在if/elseif語句中上傳圖片。我將$ _FILES作爲函數參數傳遞,因爲print_r輸出array()我認爲傳遞正常。或者在我創建的邏輯中有什麼錯誤?請有人指出它嗎?提前致謝!文件(圖片)不會上傳

<?php 
    require_once('../resources/library/admin.class.php'); 
    $obj_admin = new admin(); 
    echo $obj_admin->product_list; 
    echo '<h1>inventory list</h1></br>'; 
    $obj_admin->displayList(); 
    $obj_admin->editData(); 
    $obj_admin->deleteData(); 

          $add[] = '<form name="invent" method="post" action="index.php?res=resources&adm=admin&page=inventory.php" class="ínvent">'; 
            $add[] = '<fieldset>'; 
             $add[] = '<legend>Add products</legend>'; 
              $add[] = "<label for='name'>name</label>"; 
              $add[] = "<input type='text' name='user' value='Grachten' />"; 
             $add[] = '</br>'; 
              $add[] = "<label for='price'>price</label>"; 
              $add[] = "<input type='number' name='price' value='150' />"; 
             $add[] = '</br>'; 
              $add[] = "<label for='description'>description</label>"; 
              $add[] = "<textarea name='description' rows='10' cols= '80'>Its the best!</textarea>"; 
             $add[] = '</br>'; 
              $add[] = "<label for='img'>img</label>"; 
              $add[] = "<input name='image' accept='image/jpeg' type='file' value='files'>"; 
             $add[] = '</br>'; 
             $add[] ="<input type='submit' name='submit'/>"; 
            $add[] = '</fieldset>'; 
            $add[] = '</form>'; 
            echo implode($add); 

    if(isset($_POST['submit'])) 
    { 
     $name = $_POST['user']; 
     $price = $_POST['price']; 
     $description = $_POST['description']; 
        $obj_admin->addData($name, $price, $description, $_FILES); 


    } 

function addData($name, $price, $description, $files) 
        { 

            if(!empty($name) || !empty($price) || !empty($description)) 
            { 
              if($sql = $this->db->prepare("SELECT * FROM products WHERE name= ? LIMIT 1")) 
              { 
               $sql->bind_param('s', $name); 
               $sql->execute(); 
               $sql->store_result(); 
               $sql->fetch(); 
               $numrows = $sql->num_rows; 
               print_r($numrows); 
              } 
              else 
              { 
               echo "something is wrong"; 
              } 
              if($numrows>0) 
              { 
                 echo 'duplicate, go to <a href="index.php?res=resources&adm=admin&page=inventory.php">same page</a>'; 
              } 
              else 
              { 
               if($insert = $this->db->prepare("INSERT INTO products (name, price, description) VALUES(?, ?, ?)")) 
               { 

                  $insert->bind_param('sds', $name, $price, $description); 
                  $insert->execute(); 
               } 
               //The uploaden image is sent to temp map 
               elseif(isset($files['image']['size'])<=2048000) 
               { 

                if ($files["image"]["error"] > 0) 
                { 
                  echo "Return Code: " . $files["image"]["error"] . "<br />"; 
                } 
                else 
                { 
                  $uploaddir = "C:/xampp/htdocs/webshop/public/img/content"; 
                  $moved = move_uploaded_file($files['image']['tmp_name'], $uploaddir/$files['image']); 
                  if($moved) 
                  { 
                   echo "succes"; 
                  } 
                  else 
                  { 
                   echo "failure"; 
                  } 
                } 

                 //Shows where its stored 
                 echo "Stored in: " . "C:/xampp/htdocs/webshop/public/img/content/" . $files["file"]["name"]; 


               } 
               else 
               { 
                  echo "didnt inserted the damn thing!"; 
               } 
              } 

             } 

             print_r($files); 

          } 
+0

這麼多的代碼問題......使這個問題很簡單。人們不會爲你做所有的工作。 – Alvaro

+1

這需要進行基本的調試,這不是我們能爲您做的事情。 '$ _FILES'是否存在並填充在第一位? –

+0

我同意這裏的大多數其他人:刪除所有其他代碼,只是試圖看看你的$ _FILES var包含表單提交的數據。如果是這樣,請嘗試上傳,如果有效的話,請嘗試添加數據庫內容,以便了解究竟是什麼阻礙了流程。合理? – user1477388

回答

0

添加enctype='multipart/form-data'<form>標籤。

這允許將文件作爲表單數據的一部分包含在內,缺少這個很可能是爲什麼$_FILES爲空。

1

加ENCTYPE =「的multipart/form-data的」窗體標籤,你可以檢查,如果這是由的var_dump($ _ FILES),而你的情況將是空