如何上传图片和保存数据库的路径? [英] How to upload image and save path to database?

查看:137
本文介绍了如何上传图片和保存数据库的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中显示一些图像(数据库驱动)。这是我的gallery.php的代码:

I have a page where some images are shown (database driven). Here is the code of my gallery.php :

<ul id="portfolio-list" class="gallery">
    <?php
        $sql="select * from eikones ";
        $res=mysql_query($sql);
        $count=mysql_num_rows($res);

        for ( $i = 0; $i < $count; ++$i )
        {
            $row = mysql_fetch_array( $res );
            $co=$i+1;
            if(isset($row[ "path" ]))
            {
                $path= $row[ "path" ];
            }

            if(isset($row[ "auxon" ]))
            {
                $auxon = $row[ "auxon" ];
            }


            if($_SESSION['role'] == "admin")
                echo "<li class=\"pink\"><a href=\"$path\" rel=\"group1\" class=\"fancybox\" title=\"Προιόν \"><img src=\"$path\" alt=\"Pic\"></a></li>\n";

        }

        ?>


</ul>

现在我想要一个表单,我将能够上传一个图像。我尝试这个但它不工作:

Now I want to have a form where I will be able to upload an image. I am trying this but it doesn't work :

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

<?php

include 'conf.php'; //database connect

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 


  $tmpName  = $_FILES['image']['tmp_name'];  


  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  $query = "INSERT INTO eikones"; //table name = "eikones" and it has two columns named "auxon" and "path". The auxon is the id.
  $query .= "(image) VALUES ('','$data')";
  $results = mysql_query($query, $link) or die(mysql_error());

  print "DONE";

  }
  else {
  print "NO IMAGE SELECTED";
  }

?>

它说NO IMAGE SELECTED,数据库中没有新内容。

It says "NO IMAGE SELECTED" and nothing new comes into the database.

推荐答案

几个小时后,我找到了一个解决方案。有用。虽然我仍然很高兴找到第二个解决方案(根据代码我首先发布在这里)。这是第二个解决方案:

After some hours I found a solution. It works. Although I would still be happy to find a second solution (according to the code I first posted here). Here is the second solution :

表单页面:

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

插入数据库页面:

<?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 eikones (auxon, path) VALUES ('','$file')";

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

   }

   mysql_close();

?>

这篇关于如何上传图片和保存数据库的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆