gpt4 book ai didi

php - 图像路径保存在数据库中但图像不在目标文件夹中

转载 作者:行者123 更新时间:2023-12-04 02:04:55 26 4
gpt4 key购买 nike

你好,我在这里遇到这个问题,我这里有一个任务,我需要上传文件夹中的图像并将图像的路径保存在数据库中。现在我有这个代码,路径正在保存在数据库中,如果我尝试上传我之前上传的相同文件,它会给我消息文件已经存在但在目标文件夹中没有图像显示。请在这里帮助我。这是我的代码

目录-image.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>image in directory</title>
</head>

<body>
<form method="post" action="directory-imagedatabase.php" enctype="multipart/form-data">

<label>Choose File to Upload:</label><br />

<input type="hidden" name="id" />

<input type="file" name="uploadimage" /><br />

<input type="submit" value="upload" id="upload" />

</form>
</body>
</html>

目录-imagedatabase.php

<?php
$target_Folder = '/officework/php-startup/images/';

$uid = $_POST['id'];

$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

$file_name = $_FILES['uploadimage']['name'];

if(file_exists('/officework/php-startup/images/'.$file_name))
{
echo "That File Already Exisit";
}
else
{

// Database
$con=mysqli_connect("localhost","root","sal123","test"); //Change it if required

//Check Connection
if(mysqli_connect_errno())
{
echo "Failed to connect to database" . mysqli_connect_errno();
}

$sql = "INSERT INTO directoryimage (id,image, image_name)
VALUES ('','$target_Folder$file_name','$file_name') ";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added successfully in the database";
echo '<br />';
mysqli_close($con);

// Move the file into UPLOAD folder

move_uploaded_file( $_FILES['uploadimage']['tmp_name'], $target_Path );

echo "File Uploaded <br />";
echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
echo '<br />';
echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
echo'<br />';
echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
echo'<br />';
echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

}
?>
<a href="show-directory.php">Show Image</a>

最佳答案

如果你的主文件在同一个文件夹然后更新

$target_Folder = '/officework/php-startup/images/';

$target_Folder = 'officework/php-startup/images/';

那么这是你的全部代码

<?php
$target_Folder = 'officework/php-startup/images/';

$uid = $_POST['id'];

$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

$file_name = $_FILES['uploadimage']['name'];

if(file_exists('officework/php-startup/images/'.$file_name))
{
echo "That File Already Exisit";
}
else
{

// Database
$con=mysqli_connect("localhost","root","sal123","test"); //Change it if required

//Check Connection
if(mysqli_connect_errno())
{
echo "Failed to connect to database" . mysqli_connect_errno();
}

$sql = "INSERT INTO directoryimage (id,image, image_name)
VALUES ('','$target_Folder.$file_name','$file_name') ";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added successfully in the database";
echo '<br />';
mysqli_close($con);

// Move the file into UPLOAD folder

move_uploaded_file( $_FILES['uploadimage']['tmp_name'], $target_Path );

echo "File Uploaded <br />";
echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
echo '<br />';
echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
echo'<br />';
echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
echo'<br />';
echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

}
?>
<a href="show-directory.php">Show Image</a>

关于php - 图像路径保存在数据库中但图像不在目标文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44238613/

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