简体   繁体   English

我正在建立一个网站,任何人都可以将图像上传到我的网站

[英]Im making a website where anyone can upload image to my website

I was following a guide since im new to php and i mostly code Html and Css. 由于我是php的新手,所以我一直遵循指南,并且我主要编写Html和Css代码。 Im having problem getting the picture uploaded to the server. 我在将图片上传到服务器时遇到问题。 Here is the code and a picture after that i've uploaded the picture. 这是代码和一张图片,之后我上传了图片。

<html>
<head>
    <title>upload image</title>
</head>
<body>
  <form action="upload.php" method="post" enctype="multipart/form-data">
        File:
      <input type="file" name="image"> 
            <input type="submit" value="Upload">
  </form>
  <?php
  //connect to database
  mysql_connect("localhost", "root","") or die (mysql_error());
  mysql_select_db("bildproh") or die (mysql_error());


    if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
    {
            echo 'No upload';
    }
  // Your file has been uploaded
  else
  {
      $image = addslashes(file_get_contents($_FILES ['image']['tmp_name']));
      $image_name = addslashes($_FILES ['image'] ['name']);
      $image_size = getimagesize ($_FILES ['image'] ['tmp_name']);

      if($image_size==FALSE)  
        echo "Thats not an image.";
      else
      {
            if  (!$insert = mysql_query ("INSERT INTO bildproh VALUES ('','$image_name','$image')"))
            echo "Problem uploading image.";    
          else
          {
          $lastid = mysql_insert_id();
          echo "Image upload. <p> your image: </p><img src=get.php?id=$lastid>";
        }
      }
  }?>
  </body>
</html>

And here is my get.php 这是我的get.php

<?php

[This pic is what shows after i have tried to upload the image.][1]

//connect to database
mysql_connect("localhost", "root","") or die (mysql_error());
mysql_select_db("bildproh") or die (mysql_error());

$id = addslashes$_REQUEST ['id'];

$image = mysql_query (" SELECT * FROM bildproh WHERE id=$id");
$image = mysql_fletch_assoc($image);
$image = $image ['image'];

header("Content-type: image/jpeg");

echo $image;
?>

Image 图片

在此处输入图片说明

(New code) (新代码)

<html>
<head>


    <title>upload image</title>

    </head>
<body>

    <form action="upload.php" method="post" enctype="multipart/form-data">
    File:
        <input type="file" name="image"> <input type="submit" value="Upload">
    </form>

    <?php

    //connect to database
    mysql_connect("localhost", "root","") or die (mysql_error());
    mysql_select_db("bildproh") or die (mysql_error());


if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
{
    echo 'No upload';
}

    // Your file has been uploaded

    else{
        $image = addslashes(file_get_contents($_FILES ['image']                 ['tmp_name']));
        $image_name = addslashes($_FILES ['image'] ['name']);
        $image_size = getimagesize ($_FILES ['image'] ['tmp_name']);

        if($image_size==FALSE)  
        echo "Thats not an image.";

    }
        else{

    if  (!$insert = mysql_query ("INSERT INTO bildproh VALUES ('','" . $image_name . "','" . $image . "')")
        echo "Problem uploading image.";    


        else{

            $lastid = mysql_insert_id();
            echo "Image upload. <p> your image: </p><img src=get.php?id=$lastid>";




        }

         {

         }

    }


    ?>


    </body>
</html>

You have an error in you query. 您的查询有误。 To concatenate strings in php you have to use . 要在php中连接字符串,必须使用. . So you have to change 所以你必须改变

mysql_query ("INSERT INTO bildproh VALUES ('','$image_name','$image')")

to

mysql_query ("INSERT INTO bildproh VALUES ('','" . $image_name . "','" . $image . "')")

Also you should stop using mysql_* functions and start using the Object oriented style of mysqli. 另外,您应该停止使用mysql_*函数,并开始使用面向对象的mysqli样式。

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

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