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);

                            }
4

2 回答 2

1

将 enctype="multipart/form-data" 添加到表单标签中,您可以通过 var_dump($_FILES) 检查这是否是问题,在您的情况下将为空

于 2013-02-05T14:43:49.570 回答
0

添加enctype='multipart/form-data'到您的<form>标签。

这允许将文件作为表单数据的一部分包含在内,缺少这很可能$_FILES是空的原因。

于 2013-02-05T14:40:43.153 回答