简体   繁体   English

PHP将图像上传到服务器

[英]Php uploading images to a server

I'm having a bit of a problem trying to upload an image to a server. 尝试将图像上传到服务器时出现问题。 These are the errors I'm getting: 这些是我得到的错误:

Notice: Undefined index: $this->file in /opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php on line 70 注意:未定义的索引:第70行的/opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php中的$ this-> file

Notice: Undefined index: $this->file in /opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php on line 71 注意:未定义索引:第71行的/opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php中的$ this-> file

Notice: Undefined index: $this->file in /opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php on line 72 注意:未定义的索引:第72行的/opt/lampp/htdocs/xampp/php/theislan_testdatabase/addItem.php中的$ this-> file

I thing the problem is in the constructor of this class. 我认为问题出在此类的构造函数中。

public function __construct() {

        $this->submit  = isset($_GET['submit'])? 1 : 0;
        $this->errors = array();
        //$this->product_id = $this->filter($_GET['product_id']);
        $this->name = $this->filter($_GET['name']);
        $this->description = $this->filter($_GET['description']);
        $this->cost = $this->filter($_GET['cost']);
        $this->numinstock = $this->filter($_GET['numinstock']);
        //$this->picture = $this->filter($_GET['picture']);
        $this->type = $this->filter($_GET['type']);
        $this->file = $_GET['file'];
        $this->token = $_GET['token'];
    }

This line to be precise: $this->file = $_GET['file']; 确切地说,这行代码:$ this-> file = $ _GET ['file'];

This is the part of that class that is giving me the errors 这是给我错误的那堂课的一部分

        public function addNew() {



   $databaseinst = database:: getinstance();
   $conn =   $databaseinst->connect();




   $query = "INSERT INTO  Product (name , description, cost, numinstock, type, image)  VALUES ('$this->name', '$this->description',  
   '$this->cost','$this->numinstock', '$this->type', 'images/$this->name')";
        //description = '$this->description' cost = '$this->cost' numinstock = '$this->numinstock' picture = '$this->picture' type = '$this->type'
   $result = $databaseinst->querydb($query);

   $id = mysql_insert_id();

   //upload file
   if ((($_FILES['$this->file']["type"] == "image/gif")
   ||($_FILES['$this->file']["type"] == "image/jpeg")
   ||($_FILES['$this->file']["type"] == "image/pjpeg"))
   &&($_FILES['$this->file']["type"] < 40000000))
    move_uploaded_file($_FILES["$this->file"]["tmp_name"],"images/" . $id.".jpq");



       if ( false===$result ) {

            $errorno =mysqli_errno($conn);

             if ($errorno === 1062)
             {
     $this->errors[]= "this user already exists on the database. please try signing!";
             }
             else
             {
             $this->errors[]= "error creating new user 
             name = '$this->name' description = '$this->description' cost = '$this->cost' numinstock = '$this->numinstock' 
     type = '$this->type'
             ";
             }
    }


        if($conn == null)
        {
        $this->errors[] = "difficulty is being experienced connecting to the server. Please try again in a moment!";
        }

    }

This is where I get the users input: 这是我获得用户输入的地方:

                    <li><b><cart><form method = "get" action="">
                                        <?php
                                        printf("<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"4000000\" />");
      printf("<p>Name: <input type='text' name=\"name\" id=\"styled\";\"></p>");
      printf("<p>Type: <input type='text' name=\"type\" id=\"styled\";\"></p>");
      printf("<p>Picture: <input type=\"file\" name=\"file\" id=\"file\" \></p>");
      printf("<p>Cost: <input type='text' onkeypress='validate(event)' name=\"cost\" id=\"styled\";\"></input></p>");
      printf("<p>Number Instock: <input type='text' onkeypress='validate(event)' name=\"numinstock\" id=\"styled\";\"></p>");
                   printf("<p>Description: <textarea  rows=\"6\" cols=\"200\" name=\"description\" id=\"styled\";\"></textarea></p>");
                                        ?>
                                    <p><input type="submit" value="Add Item" name="submitAdd" />
                                        <input type="hidden"  name ="token" value="<?= $_SESSION['token']; ?>"
                                               /></p></form></cart></b></li>

Thanks. 谢谢。

当用作索引时,不要在$this->file周围$this->file引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM