我有一個問題可以顯示可以上傳到我的網站上的圖片。在上傳過程中,我更改文件的名稱,將圖片存儲在文件夾中,並將文件名存儲在數據庫中。我在下面提供我的代碼。許多在此先感謝那些誰將會看看:顯示一個圖片哪個文件名在php中有更改
形式上傳的圖片:
<form method="post" action="PicPost.php" enctype="multipart/form-data">
<label for="Pic">Upload your pic: (JPG, JPEG PNG ou GIF | max. 1 Mo) :</label><br />
<input type="hidden" name="maxsize" value="1048576" />
<input type="file" name="Pic" id="Pic" />
<p>
<label>Tags
<input type="text" name="Tags" id="Tags" />
</label>
<br />
<input type="submit" name="submit" value="Submit" />
</p>
</form>
的代碼,以完成PicPost.php上傳:
<?php
$maxsize = '1048576';
if ($_FILES['Pic']['error'] > 0) $erreur = "Error";
if ($_FILES['Pic']['size'] > $maxsize) $erreur = "The file is too big";
$extensions_valides = array('jpg' , 'jpeg' , 'gif' , 'png');
$extension_upload = strtolower( substr( strrchr($_FILES['Pic'] ['name'], '.') ,1) );
if (in_array($extension_upload,$extensions_valides)) echo "Correct extension";
$name = md5(uniqid(rand(), true));
$resultat = move_uploaded_file($_FILES['Pic']['tmp_name'],$name);
if ($resultat) echo "Upload successful";
try
{
$bdd = new PDO('mysql:host=localhost;dbname=mydb', 'root', '');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$req = $bdd->prepare('INSERT INTO picdb (Picture, Tags)
VALUES(?, ?)');
$req->execute(array($name, $_POST['Tags']));
代碼通過標籤在數據庫中搜索圖片:
<form id="form1" name="form1" method="post" action="SearchPost.php">
<label>
<input type="text" name="Search" id="Search" />
</label>
<label>
<input type="submit" name="Submit" id="Submit" value="Search" />
</label>
</form>
顯示在SearchPo中搜索的圖片的代碼st.php:
try
{
$bdd = new PDO('mysql:host=localhost;dbname=mydb', 'root', '');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$reponse = $bdd->query('SELECT Picture FROM picdb WHERE
Tags=\''.$_POST['Search'].'\'');
while ($donnees = $reponse->fetch())
echo "<img src=".$donnees['Picture']."/>";
$reponse->closeCursor();
謝謝您的提示,我只是給了選票。我接受了其他答案。我不知道在哪裏可以添加錯誤處理,我已經很少有人在上傳過程中(第二個代碼) – user1115535