将POST数据传递给PHP函数

时间:2015-04-12 11:58:32

标签: php function 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']);

}

目前,当我点击提交时收到此错误消息:

注意:第55行的C:\ xampp \ htdocs \ advanced \ classes \ Login.php中的未定义索引:image.jpg

提前谢谢!

2 个答案:

答案 0 :(得分: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']);

}

答案 1 :(得分:1)

改变这个:

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

对此:

$this->editUserPicture('profil_slika');

并将enctype="multipart/form-data"添加到HTML <form>的属性中。

原因是profil_slika传递给$_POST数组。它将位于$_FILES数组中,键为profil_slika。换句话说,您需要用于$_FILES数组的密钥 HTML name的{​​{1}},您不需要使用完全input