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

时间:2016-02-05 09:18:49

标签: javascript php html mysql css

我正在关注指南,因为我刚接触到php,我主要编写Html和Css代码。我有问题将图片上传到服务器。这是我上传图片后的代码和图片。

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

这是我的 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;
?>

图片

enter image description here

(新密码)

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

1 个答案:

答案 0 :(得分:0)

您的查询中有错误。要在php中连接字符串,您必须使用.。所以你必须改变

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

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

此外,您应该停止使用mysql_*函数并开始使用面向对象的mysqli样式。