【问题标题】:Im making a website where anyone can upload image to my website我正在制作一个网站,任何人都可以将图像上传到我的网站
【发布时间】:2016-02-05 09:18:49
【问题描述】:

自从我是 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;
?>

图片

(新代码)

<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 中有一个错字:您必须在addslashes 之后添加括号:addslashes( $_REQUEST ['id'] )

标签: javascript php html mysql css


【解决方案1】:

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

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

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

你也应该停止使用mysql_*函数,开始使用mysqli的面向对象风格。

【讨论】:

  • 原来的查询是完全正确的。字符串中的双引号变量将被评估。
  • 上传图片后看到的echo消息是什么?
  • 使用您的代码后,我得到了这个;解析错误:语法错误,第 40 行 C:\xampp\htdocs\robert\upload.php 中的意外 'echo' (T_ECHO)
  • 你错过了一些东西(;()if,...)。检查您的代码并再次测试。
  • 找不到我丢失的东西。
猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多