在数据库中插入图像路径(浏览图像文件夹位置)并检索它

时间:2014-04-14 17:15:04

标签: php database image

这是我从谷歌搜索得到的代码。但我不知道那将是img.php。我在谷歌搜索但无法解决此问题。

HTML:

< form action =" img.php " method = "POST" enctype = "multipart/form-data">

 < input type="file" name = "myfile" />
 < input type = "submit" value="upload" />

< /form>

PHP:

    mysql_connect("localhost","root","") or die("cant connect to server.");
    mysql_select_db("databaseimage") or die("cant connect to database");
    echo "name:";
    echo $name = $_FILES['myfile']['name'];
    echo "type ";
    echo $type = $_FILES['myfile']['type'];
    echo "<BR>";echo "Size: ";
    echo $size = $_FILES['myfile']['size'];
    echo "temp: ";
    echo $temp = $_FILES['myfile']['tmp_name'];
    echo "error code: ";
    echo $error = $_FILES['myfile']['error'];
    echo "file contents: ";
    echo $file_contents = addslashes(file_get_contents($_FILES['myfile']['tmp_name']));
    echo "<BR>";
    if (!$insert = mysql_query("INSERT INTO table1 VALUES ('','$name','$file_contents')")) 
    echo "problem uploading file";
    else {$last_id = mysql_insert_id();
    echo "Image uploaded.";
    echo "<BR>";
    echo "<img src=get.php?id=$last_id>";
    }

1 个答案:

答案 0 :(得分:0)

将您的php文件保存为img.php,并且get.phpget.php代码类似,

if(isset($_REQUEST['id'])){ 
   mysql_connect("localhost","root","") or die("cant connect to server.");
   mysql_select_db("databaseimage") or die("cant connect to database");

   $sql_result = mysql_query("SELECT name FROM table1 WHERE id= $_REQUEST['id']");
   $row = mysql_fetch_row($sql_result); 
   echo $row[0];
}