這裏是解決方案,我想你想的是: 有三頁。 1. index.php(具有上傳圖片的形式) 2. upload.php(將圖片保存在目錄及其在數據庫中的路徑) 3. showimage.php(最後會顯示圖片)
這裏是代碼 (的index.php)
<form method="post" action="upload.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" />
</form>
(upload.php的)
<?php
$target_Folder = "upload/"; // directory where images will be saved
$target_Path = $target_Folder.basename($_FILES['uploadimage']['name']);
$savepath = $target_Path.basename($_FILES['uploadimage']['name']);
$file_name = $_FILES['uploadimage']['name'];
if(file_exists('upload/'.$file_name))
{
echo "That File Already Exisit";
}
else
{
// Database
$con=mysqli_connect("localhost","user_name","pasword","database"); // Change it if required
//Check Connection
if(mysqli_connect_errno())
{
echo "Failed to connect to database" . mysqli_connect_errno();
}
$sql = "INSERT INTO image (id,image, image_name)
VALUES ('$uid','$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: ' . $target_Path;
echo '<br />';
echo 'File Name: ' . $_FILES['uploadimage']['name'];
echo'<br />';
echo 'File Type: ' . $_FILES['uploadimage']['type'];
echo'<br />';
echo 'File Size: ' . $_FILES['uploadimage']['size'];
}
?>
<a href="showimage.php">Show Image</a>
(showimage。PHP)
<?php
$con=mysqli_connect("localhost","user_name","password","database_name"); // Change it if required
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM image ");
while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['image'] . '" width="200" />';
echo'<br /><br />';
}
mysqli_close($con);
?>
特點
它會檢查文件名,如果該名稱的文件已經找不到,它將不會上傳下載文件,並提醒用戶。
數據庫結構
ID INT(4)自動遞增 - 圖像VARCHAR(100) - IMAGE_NAME VARCHAR(50)
檢查答案。 –