使用Php和mysql通过文件夹和mysql url显示图像时出错

时间:2015-07-14 05:56:45

标签: php mysql

我使用php将图像存储在一个文件夹中,并在mysql中保存名称url,我需要使用mysql数据库中保存的url从文件夹中显示。我尝试了以下但是在显示中遇到错误..图像正在保存上传时的文件夹。但显示它破碎的图像..

insert.php

<form action="imagebackend.php" method="post" enctype="multipart/form-
data">
<table border="0" cellspacing="0" align="center" cellpadding="3"   
bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align="center">
<input type="submit" name="action" value="Load">
</td>
</tr>
</table>
</form>

<?php
if ($_POST["action"] == "Load")
{
$folder = "C:/wamp/www/userlogin/pic/";
move_uploaded_file($_FILES["filep"]["tmp_name"]   
,"$folder".$_FILES["filep"]["name"]);
echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
$result = mysqli_connect("localhost", "root", "") or die ("Could not    
save image name Error: " . mysql_error());
mysqli_select_db($result, "login") or die("Could not select database");
mysqli_query($result, "INSERT into picture (ul)   
VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database"; }
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
?>

和display.php

<?php
//Retrieves data from MySQL
$conn= mysqli_connect("localhost", "root", "") or die ("Could not save 
image name Error: " . mysql_error());
mysqli_select_db($conn, "login") or die("Could not select database");
$data = mysqli_query($conn, "SELECT ul FROM picture") or
die(mysqli_error());
//Puts it into an array
$file_path = "C:/wamp/www/userlogin/pic/";
while($row = mysqli_fetch_assoc($data)) 
{//Outputs the image and other data
$src=$file_path.$row['ul'];
echo '<img src=".$src."><br>';
echo"no images found";
}
?>

请帮助我。 我得到一个破碎的图像 我试了很多时间,浪费了3天的时间。

3 个答案:

答案 0 :(得分:2)

更新您的代码......

<强> insert.php

<form action="" method="post" enctype="multipart/form-data">
<table border="0" cellspacing="0" align="center" cellpadding="3" bordercolor="#cccccc">
   <tr>
       <td>File:</td>
       <td><input type="file" name="filep" size=45></td>
   </tr>
   <tr>
       <td colspan=2><p align="center"><input type="submit" name="action" value="Load"></td>
   </tr>
</table>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
   $folder = "userlogin/pic/";
   move_uploaded_file($_FILES["filep"]["tmp_name"], $folder . $_FILES["filep"]["name"]);
   echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
   $result = mysqli_connect("localhost", "root", "") or die ("Could not save image name Error: " . mysql_error());
   mysqli_select_db($result, "login") or die("Could not select database");
   $fileName = $_FILES['filep']['name'];
   mysqli_query($result, "INSERT into picture(ul)VALUES('$fileName')");
   if($result) { 
       echo " Image name saved into database";
   }else {
       //Gives and error if its not
       echo "Sorry, there was a problem uploading your file.";
   }
}
?>

<强> Display.php的

<?php
//Retrieves data from MySQL
$conn = mysqli_connect("localhost", "root", "") or die ("Could not save image name Error: " . mysql_error());
mysqli_select_db($conn, "login") or die("Could not select database");
$data = mysqli_query($conn, "SELECT ul FROM picture") or die(mysqli_error());
//Puts it into an array
$file_path = "userlogin/pic/";
if(mysqli_num_rows($data) > 0){
    while($row = mysqli_fetch_array($data)) {
        //Outputs the image and other data
        $src= $file_path . $row['ul']; ?>
        <img src="<?php echo $src; ?>"><br/>
    <?php }
}else{
    echo"no images found";
}
?>

答案 1 :(得分:1)

您应该使用网站网址而不是实际服务器文件夹的路径。

使用

$file_path = "http://localhost/userlogin/pic/";

答案 2 :(得分:0)

首先检查上传是否正确

if (move_uploaded_file($_FILES["filep"]["tmp_name"], $folder . $_FILES["filep"]["name"])) {

}
else {

}

如果上传成功,那么你可以插入其他明智的其他部分

相关问题