我正處於開發招標系統的初期階段,用戶可以插入他們想要出售的物品。以下是從臨時路徑複製圖像並將圖像路徑存儲到另一個文件夾的腳本。顯示來自MySQL中PHP路徑的圖片
define ("MAX_SIZE","10000");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
};
//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors = 0;
//Checks if the form has been submitted
if (isset($_POST['Submit']))
{
//Reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//If it is not empty
if ($image)
{
//Get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//Get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//If it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") &&
($extension != "jpeg") &&
($extension != "png") &&
($extension != "gif"))
{
//Print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//Get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//Compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*111111111111024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//We will give an unique name, for example the time in Unix time format
$image_name=time().'.'.$extension;
//The new name will be containing the full path where will be stored (images folder).
$imagepath='C:\\xampp\\htdocs\\biddingsystem\\Images\\' . $image_name;
//We verify if the image has been uploaded, and print an error instead
$copied = copy($_FILES['image']['tmp_name'], $imagepath);
if (!$copied)
{
echo '<h1>Picture upload failed!</h1>';
$errors=1;
}
}
}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors && isset($_POST['image']))
{
echo "<h1>Picture Uploaded Successfully! Try again!</h1>";
}
然後,我插入與其他數據沿着圖像路徑MySQL數據庫與下面的腳本,並試圖顯示它們回到使用一個表中的用戶。其他數據工作得很好,但對於圖像顯示(圖像路徑),只顯示圖像的路徑,而不是圖像本身。
mysql_query("INSERT INTO items
(username, item, price, description, start_date, start_time, imagepath)
VALUES ('$username', '$_POST[item]', '$_POST[price]', '$_POST[description]','$_POST[start_date]', '$_POST[start_time]', '$imagepath') ")
or die ("Error - Couldn't add item");
echo "Item added successfully";
echo "<h1>You have added the following item:</h1>";
$sql = "SELECT item, price, description, start_time, start_date, imagepath FROM items WHERE username = '$username' AND item='$_POST[item]'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo"<table border=2>
<tr><td>Item</td>
<td>Price</td><td>Description</td><td>Start time</td><td>Start date</td><td>Picture</td></tr>
<tr><td> $row[item]</td>
<td> $row[price]</td>
<td> $row[description]</td>
<td> $row[start_time]</td>
<td> $row[start_date]</td>
<td> $row[imagepath]</td>
</tr>
</table></br>";
我試過使用<img src="<?php $imagepath ?>">
來顯示圖像,但無濟於事。我甚至嘗試使用BLOB類型將實際圖像本身存儲在數據庫中。然而,結果是一個充滿奇怪字符的頁面。我該如何解決這個問題?
已將其更改爲相對路徑,並且以下警告顯示: 警告:copy(/biddingsystem/Images/1320662886.jpg)[function.copy]:未能打開流:C:\中沒有此文件或目錄。 xampp \ htdocs \ biddingsystem \ auction.php在線87 – user1033038
順便說一句,我已經改變了這一行:$ imageData ='/ biddingsystem/Images /'.$ image_name; – user1033038
$ copied = copy($ _ FILES ['image'] ['tmp_name'],$ imageData); << --- line87 – user1033038