gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:55:02 25 4
gpt4 key购买 nike

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

插入.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.";
}
}
?>

和显示.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 天的时间。

最佳答案

更新您的代码...

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.";
}
}
?>

显示.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";
}
?>

关于php - 通过文件夹和 mysql url 使用 Php 和 mysql 显示图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com