简体   繁体   English

将图像文件上传到数据库

[英]Uploading of Image file to database

I have the html code like : - 我有类似的HTML代码:-

<form id="form" name="form" action="banner_ad_post.php" method="post" enctype="multipart/form-data">
                <p>
                  <label for="banner_name"><font color="#FF0000"> * </font>Banner Name: </label>
                  <input type="text" name="banner_name" id="banner_name" value="" maxlength="100" required="required"/>
                </p>
                <p>
                  <label for="Banner_website_url"><font color="#FF0000"> * </font>Banner website Url: </label>
                  <input type="url" name="banner_site_url" id="banner_site_url" value="" maxlength="100" required="required"/>
                </p>

                <p>
                  <label for="banner_image_url">Banner Image Url: </label>
                  <input type="file" name="file" id="file" value="" accept="image" placeholder="Browse from hard disk" onchange="img_path()"/> &nbsp;
                   <font color="#FF0000"> OR</font> &nbsp;
                  <input type="url" name="banner_image_url" id="banner_image_url" value="" maxlength="100" placeholder="Enter the url from website." onchange="validate()"/> &nbsp; 


                </p>
                <p>
                <label for="submit"> </label>
                  <input type="submit" id="submit" name="submit" value="Submit" onclick="preview()" />
                </p>
              </form>

and the php script like : - 和类似的PHP脚本:-

<?php 
     if(isset($_POST['banner_name']) && isset($_POST['banner_site_url']) && isset($_POST['banner_image_url']) && isset($_FILES['file']['name']))
     {
        $banner_name=$_POST['banner_name'];
        $banner_name=strip_tags($banner_name);
        $banner_site_url=$_POST['banner_site_url'];
        $banner_site_url=strip_tags($banner_site_url);
        $banner_image_url=$_POST['banner_image_url'];
        $banner_image_url=strip_tags($banner_image_url);
        $name=$_FILES['file']['name'];

        $size=$_FILES['file']['size'];
        $type=$_FILES['file']['type'];
        $tmp_name=$_FILES['file']['tmp_name'];
        $error=$_FILES['file']['error'];
        $maxsize = 512000;
        $location='uploads/';
        if(!empty($banner_image_url))
            {
            $file_location=$banner_image_url;
            ?>
            <div id="img"> 
            <img src="<?php echo $file_location?>" align="left" width="200px" height="200px">
            </div>
            <?php
            } elseif (!empty($name))
            {
            $file_location=$location.$name;
            ?>
                    <div id="img">  
                    <img src="http://localhost/leadstool/<?php echo $file_location?>" align="left" width="200px" height="200px">
                    </div>

                    <?php 
            }

          if(!empty($banner_name) && !empty($banner_site_url))
          {
             $query="INSERT INTO `banner_ad` VALUES('','$id','$banner_name','$file_location','$banner_site_url','0',now())";
             if($query_run=mysql_query($query))
             {
                if($size <= $maxsize)
                {                         
                  if(move_uploaded_file($tmp_name,$location.$name))
                    {
                    echo'<font color="green">File uploaded & has been sent for approval.</font>';
                    } 
                    else 
                    {
                    echo'<font color="red">File can not be uploaded.</font>';

                    }
                } else {
                echo'<font color="green">Your information has been uploaded and has been sent for approval.</font>';
                }
             }
             else
             {
                  echo'<font color="red"> The request can not be performed.</font>';
             }
         } 
         else 
         {
          echo'<font color="red">These fields are mandatory.</font>';
         }
    }

     ?>

Here, the problem is due to some reason move_uploaded_file() method is unable to upload the file in the database. 在这里,问题是由于某种原因move_uploaded_file()方法无法在数据库中上传文件。 But it still gives the output like " File uploaded & has been sent for approval ". 但是它仍然提供类似“文件已上传并已发送以供批准”的输出。 Now, I don't understand what exactly am i missing here? 现在,我不明白我在这里到底想念什么?

Any help will be appreciated ... Thank you in advance! 任何帮助将不胜感激...预先感谢您!

Sorry! 抱歉! there is no programming error in this question. 这个问题没有编程错误。 Just a simple logical error. 只是一个简单的逻辑错误。

I am re posting the code... that i have edited to rectify the problem. 我正在重新发布代码...我已对其进行编辑以纠正该问题。

if($size <= $maxsize)
                {                         
                  if(move_uploaded_file($tmp_name,$location.$name))
                    {
                    echo'<font color="green">File uploaded & has been sent for approval.</font>';
                    } 
                    else 
                    {
                    echo'<font color="red">File can not be uploaded.</font>';
                    ?>
                    <div id="img">  
                    <img src="http://localhost/leadstool/<?php echo $file_location?>" align="left" width="200px" height="200px">
                    </div>

                    <?php 

                    }
                } else {
                echo'<font color="red">File size must be less than 500KB.</font>';
                }

You can do that as well. 您也可以这样做。 It works very well : 效果很好:

images to database via form: 通过表单将图像导入数据库:

form page : 表格页面:

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

insert to database page : 插入数据库页面:

<?php

  include 'conf.php';

  if ($_FILES["image"]["error"] > 0)
  {
     echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
     echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
   }
   else
   {
     move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
     echo"<font size = '5'><font color=\"#0CF44A\">SAVED<br>";

     $file="images/".$_FILES["image"]["name"];
     $sql="INSERT INTO database_table (table_column_1, table_column_2) VALUES ('','$file')";

     if (!mysql_query($sql))
     {
        die('Error: ' . mysql_error());
     }
     echo "<font size = '5'><font color=\"#0CF44A\">SAVED TO DATABASE";

   }

   mysql_close();

?>

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

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