使用php + mysql上传,显示和移动图像文件

时间:2014-02-20 18:58:34

标签: php

我是PHP的新手,我正在努力让一些代码工作。 我有一个简单的形式,假设安全的名称,路径并将其移动到一个文件夹。我还应该显示图像本身(直到10分钟才开始工作)。

与wamp的连接工作正常。问题是照片没有显示,也没有移动到指定的文件夹。

知道我做错了什么?

提前致谢!

<!DOCTYPE HTML> 
<html>
    <head><h1> Upload your photo here! </h1></head>
<body> 

<?php
// define variables 
$file = $filename = $caption = $imgData = "";
$hostname = "**"; 
$db_user = "**"; 
$db_password = "**";
$database = "example"; 
$db_table = "album"; 
$image = "";
$filepath = "";

//selecting data from form
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   //$filename = $_POST["filename"];
   $file = $_FILES["file"];
   $caption = $_POST["caption"];
}


if ($_FILES)
{
        $filepath = "C:\images" . $filename; 
    $image = $_FILES ['file']['name'];    
    move_uploaded_file($image, $filepath);
        $imgData = $filepath .$_FILES['file']['name'];    
}

//connect to database
$db=mysqli_connect($hostname,$db_user,$db_password,$database);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


//insert info to the database
$query = sprintf("INSERT INTO album (filename,caption,file) VALUES ('$image','$caption','$imgData')",
    mysql_real_escape_string($imgData));
    mysql_query($query);  

if (!mysqli_query($db,$query))
  {
  die('Error: ' . mysqli_error($db));
  }

?>


<form method="post" action="upload2.php" enctype="multipart/form-data"/>
Select File: <input type="file" name="file" size="10" />
Add a caption: <input type="text" name="caption"size="10"/>
<input type="submit" value="image" />
</form>

<?php
echo "<h2>$caption</h2>";
echo "<IMG src='$file'/><p>";
echo "<IMG src='$image'/>";
?>
</body>
</html>

0 个答案:

没有答案