文件(图片)无法上传

时间:2013-02-05 14:37:10

标签: php file-upload

我正在尝试在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);

                            }

2 个答案:

答案 0 :(得分:1)

将enctype =“multipart / form-data”添加到表单标记中,您可以检查var_dump($ _ FILES)是否存在问题,在您的情况下为空

答案 1 :(得分:0)

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

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