[英]How to upload an Image onto localhost server whilst path is stored in mysql database?
我正在使用本教程http://www.onlinebuff.com/article_step-by-step-to-upload-an-image-and-store-in-database-using-php_40.html來構建我現在擁有的代碼。 我對php也很陌生,所以我在更正他的一些錯誤時遇到問題。
我試圖將圖像存儲在數據庫中並從那里讀取它們,但是以后我想對它們執行的操作將不允許我這樣做,而且很難監視。 學生們會將圖像上傳到網站上,我想監視他們添加的內容,以便如果有人舉報了圖像,那么我可以輕松地將其刪除。 因此,我決定使用文件訪問路徑
我想將圖像上傳到服務器,並且當路徑在數據庫中后不久,我想將其顯示在數據庫中,但是現在我主要關心的是將文件放入服務器,並將圖像路徑放入數據庫。
當我運行php代碼時,出現錯誤。 我也不知道為什么
這是我的php代碼:
<?php
$con = mysqli_connect('localhost', 'root', 'root', 'koleesy');
if (mysqli_connect_errno()) // Check connection
{ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$dirpath = dirname(getcwd());
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = $dirpath.$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
$Imageup="INSERT into 'images_tbl' ('images_path','submission_date') VALUES
('".$target_path."','".date("Y-m-d")."')";
if ($con->query($Imageup) === TRUE) {
echo "New record created successfully"; }
else{
echo("Error While uploading image on the server");
}
}
}
?>
這是我正在測試的索引文件。 只需一個上傳按鈕,然后選擇文件。
<html lang="en">
<head>
<title>Uploading Image to Folder Test</title>
</head>
<body>
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>
</body>
</html>
我看過其他示例,但它們看起來都非常不同。 他們使用公共空白和東西。 不,那是什么。
我也有這個問題-盡管我確實弄清楚了。 我將快速介紹如何將文件上傳到目錄,並將路徑保存到MySQL,看到初始查詢后可以執行多個查詢,這樣便可以根據需要刪除和刪除文件。 。 開始:
picture_upload.php
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:<br>
<input type="file" name="fileToUpload" id="fileToUpload"><br><br>
<input type="submit" value="Upload Image" name="submit">
</form>
upload.php的
<?php $target_dir = "ANY_DIRECTORY_YOU_LIKE/PICTURES/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 1000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$pictureName = "ANY_DIRECTORY_YOU_LIKE/PICTURES/". basename( $_FILES["fileToUpload"]["name"]);
$servername = "localhost";
$username = "MySQL_USERNAME";
$password = "MySQL_PASSWORD";
$dbname = "MySQL_DATABASE_NAME";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE TableName SET myPicture='$pictureName' WHERE email='$myEmail' AND password='$myPassword'";
// Make Sure to tell MySQL which user you want to update which means setting the variable $myEmail and $myPassword accordingly
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
玩得開心! 哦,我的數據庫使用的是VarChar圖片。 如果您想發揮創意並想出另一種方法-請繼續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.