2015-04-12 162 views
1

我想用PHP上傳圖像文件。我怎樣才能POST數據傳遞到另一個文件中的PHP函數,點擊提交按鈕時將執行...將POST數據傳遞到PHP函數

這裏是形式:

<!-- edit users profile picture --> 
<form method="post" action="edit.php" name="user_edit_profile_picture"> 
    <input type="file" name="profil_slika" id="profil_slika"> 
    <input type="submit" name="user_edit_submit_profile_picture"> 

    <?php 
     $con = mysqli_connect("localhost","root","","login"); 
     $q = mysqli_query($con,"SELECT * FROM users WHERE user_name = '".$_SESSION['user_name']."'"); 
     while($row = mysqli_fetch_assoc($q)){ 
      echo $row['user_name']; 
      if($row['image'] == ""){ 
       echo "<img width='100' height='100' src='profile_pictures/default_user.png' alt='Default Profile Pic'>"; 
      } else { 
       echo "<img width='100' height='100' src='profile_pictures/".$row['image']."' alt='Profile Pic'>"; 
      } 
      echo "<br>"; 
     } 
    ?> 
</form> 

,這是其他PHP文件功能:

elseif(isset($_POST["user_edit_submit_profile_picture"])) { 
       $this->editUserPicture($_POST['profil_slika']); 
      } 

和函數體:

public function editUserPicture($profilimage){ 

     $slika = $_FILES[$profilimage]['tmp_name']; 
echo $slika; 
     move_uploaded_file($_FILES[$profilimage]['tmp_name'],"profile_pictures/".$_FILES[$profilimage]['name']); 

} 

目前我得到這個

注意:當我點擊提交錯誤信息未定義指數:image.jpg的在C:\ XAMPP \ htdocs中\高級\ \類上的login.php在線55

預先感謝您!

回答

1

更改此:

$this->editUserPicture($_POST['profil_slika']); 

要這樣:

$this->editUserPicture('profil_slika'); 

,並添加enctype="multipart/form-data"到HTML <form>的屬性。

原因是profil_slika不是被傳遞給$_POST陣列。它將位於$_FILES陣列中,其密鑰爲profil_slika。換句話說,您需要用於$_FILES陣列的密鑰是,name的HTML input,您根本不需要使用$_POST

+0

有趣,但它工作:)謝謝。 – Volkan

1

請與此更換你的表單標籤,並嘗試:

<form method="post" action="edit.php" name="user_edit_profile_picture" enctype="multipart/form-data"> 

功能:

elseif(isset($_POST["user_edit_submit_profile_picture"])) { 
    $this->editUserPicture('profil_slika'); 
} 

函數體:

public function editUserPicture($profilimage){ 
     $slika = $_FILES[$profilimage]['tmp_name']; 
     echo $slika; 
     move_uploaded_file($_FILES[$profilimage]['tmp_name'],"profile_pictures/".$_FILES[$profilimage]['name']); 

} 
+0

現在我得到另一個錯誤:注意:未定義的索引:profil_slika在C:\ xampp \ htdocs \ advanced \ classes \ Login.php上... – Volkan

+0

請同時修改以下代碼:elseif(isset($ _ POST [ user_edit_submit_profile_picture「])){ $ this-> editUserPicture($ _ FILES ['profil_slika']); } – Iffi

+0

注意:Array to string conversion in C:\ xampp \ htdocs \ advanced \ classes \ Login.php on line 562 Array 警告:C:\ xampp \ htdocs \ advanced \ classes \ Login.php中的非法偏移類型在線563 警告:C:\ xampp \ htdocs \ advanced \ classes \ Login.php中的非法偏移類型在行565上 警告:C:\ xampp \ htdocs \ advanced \ classes \ Login.php中的非法偏移類型在線565 – Volkan