显示来自数据库的图像,当我尝试访问图像时给出 about:blank#blocked
Display image from database, gives about:blank#blocked when i try to acces image
我正在尝试显示来自数据库的图像,但我不知道如何显示它,这就是我上传图像的方式
//image upload + validation
$file = $_FILES['image'];
$file_name = $_FILES['image']['name'];//file name
$file_location = $_FILES['image']['tmp_name']; //temporary location
$file_size = $_FILES['image']['size'];// size
$file_error = $_FILES['image']['error'];// error 0 if no error or 1 if there is an error
// $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$temp_extension = explode('.',$file_name);//explode from . file extension (here we have file name and extension)
$file_extension = strtolower(end($temp_extension)); // extension name (ex: .jpg)
$allowed_extensions = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($file_extension, $allowed_extensions)) {
if ($file_error === 0) {
if ($file_size < 31457280) { //31457280b(bytes) in 30mb
$new_file_name = uniqid('',true).".".$file_extension;
var_dump($new_file_name);
$file_destination = dirname(__FILE__, 2)."/images/".$new_file_name;
move_uploaded_file($file_location, $file_destination);
}else {
echo "Sorry your file size it's too big!";
}
}else {
echo "Sorry, there was an error, try again";
}
}else {
echo "Sorry, your file type is not accepted";
}
$sql = "SELECT * FROM `product` WHERE `id` = '{$_POST['id']}'";
$result = mysqli_query(get_connection(), $sql);
$row = $result->fetch_assoc();
if($error_message == ""){
if(is_array($row)){
$sql = "UPDATE `product` SET `category_id` = '$category_id', `name` = '$name', `description` = '$description', `price` = '$price', `quantity` = '$quantity', `type` = '$type', `image` = '$file', `modified_on` = NOW(), `color_id` = '$color_id', `session_id` = '$session_id' WHERE `id` = '{$_POST['id']}'";
$result = mysqli_query(get_connection(), $sql);
// print_r($sql);
// var_dump($sql);
}else{
$sql = "INSERT INTO `product` (`category_id`, `name`, `description`, `price`, `quantity`, `type`, `image`, `added_on`, `color_id`, `session_id`) VALUES ('$category_id', '$name', '$description', '$price', '$quantity', '$type', '$new_file_name', NOW(), '$color_id', '$session_id')";
$result = mysqli_query(get_connection(), $sql);
// var_dump($sql);
// var_dump($sql);
}//end elseif
header("Location: admin.php?page=product_list");
}//end if
这就是我尝试显示来自数据库的图像的方式
<!-- TABLE BODY-->
<tbody>
<?php
foreach ($result as $row) {
$sql_category = "SELECT * FROM `category` WHERE `id` = '{$row['category_id']}'";
$result_category = mysqli_query(get_connection(), $sql_category);
$row_category = $result_category->fetch_assoc();
$sql_color = "SELECT * FROM `color` WHERE `id` = '{$row['color_id']}'";
$result_color = mysqli_query(get_connection(), $sql_color);
$row_color = $result_color->fetch_assoc();
?>
<tr>
<td><p><?=$nr++?></p></td>
<td><p><?=$row_category['name']?></p></td>
<td><p><?=$row['name']?></p></td>
<td><p><?=$row['description']?></p></td>
<td><p><?=$row['price']?></p></td>
<td><p><?=$row['quantity']?></p></td>
<td><p><?=$row['type']?></p></td>
<?php var_dump(dirname(__FILE__, 2)."/images/".$row['image']); ?>
<td><p><img src="<?=dirname(__FILE__, 2)."/images/"?><?=$row['image'];?>" alt="" style="width: 100px; height: 100px;"></p></td>
<td><p><?=$row['added_on']?></p></td>
<td><p><?=$row['modified_on']?></p></td>
<td><p><?=$row_color['name']?></p></td>
<td>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=product_add_edit&id=<?=$row['id']?>">Edit</a></button>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=product_list&action=delete&id=<?=$row['id']?>" onclick="return confirm('Are you sure ?')">Delete</a></button>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=cart&action=add_to_cart&id=<?=$row['id']?>">Add to cart</a></button>
</td>
</tr>
<?php }//end foreach ?>
</tbody>
<!-- END OF THE TABLE BODY -->
</table>
Var_dump 正在显示 C:\wamp64\www\Project6Bootstrap4-V1\modules\product_list.php:101:string 'C:\wamp64\www\Project6Bootstrap4-V1/images/6103fb6fe3da77.64812338.jpg'
如果我将该地址粘贴到浏览器中,它会显示该图像,但如果我尝试复制图像地址,它会给我 about:blank#blocked
同样在数据库中图像类型是 varchar(200)
这不是有效的 HTTP URL:
C:\wamp64\www\Project6Bootstrap4-V1/images/6103fb6fe3da77.64812338.jpg
您正在尝试让网页访问本地文件,但浏览器出于安全原因阻止了该文件。
删除 dirname(__FILE__, 2)
部分,仅使用与正在查看的页面相关的图像的 URL:
/images/6103fb6fe3da77.64812338.jpg
请注意,我不知道这是否是图像的正确 URL。你必须确定那是什么。但大概您要显示的图片是您网站的一部分,您只需要为该图片使用 URL。
我正在尝试显示来自数据库的图像,但我不知道如何显示它,这就是我上传图像的方式
//image upload + validation
$file = $_FILES['image'];
$file_name = $_FILES['image']['name'];//file name
$file_location = $_FILES['image']['tmp_name']; //temporary location
$file_size = $_FILES['image']['size'];// size
$file_error = $_FILES['image']['error'];// error 0 if no error or 1 if there is an error
// $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$temp_extension = explode('.',$file_name);//explode from . file extension (here we have file name and extension)
$file_extension = strtolower(end($temp_extension)); // extension name (ex: .jpg)
$allowed_extensions = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($file_extension, $allowed_extensions)) {
if ($file_error === 0) {
if ($file_size < 31457280) { //31457280b(bytes) in 30mb
$new_file_name = uniqid('',true).".".$file_extension;
var_dump($new_file_name);
$file_destination = dirname(__FILE__, 2)."/images/".$new_file_name;
move_uploaded_file($file_location, $file_destination);
}else {
echo "Sorry your file size it's too big!";
}
}else {
echo "Sorry, there was an error, try again";
}
}else {
echo "Sorry, your file type is not accepted";
}
$sql = "SELECT * FROM `product` WHERE `id` = '{$_POST['id']}'";
$result = mysqli_query(get_connection(), $sql);
$row = $result->fetch_assoc();
if($error_message == ""){
if(is_array($row)){
$sql = "UPDATE `product` SET `category_id` = '$category_id', `name` = '$name', `description` = '$description', `price` = '$price', `quantity` = '$quantity', `type` = '$type', `image` = '$file', `modified_on` = NOW(), `color_id` = '$color_id', `session_id` = '$session_id' WHERE `id` = '{$_POST['id']}'";
$result = mysqli_query(get_connection(), $sql);
// print_r($sql);
// var_dump($sql);
}else{
$sql = "INSERT INTO `product` (`category_id`, `name`, `description`, `price`, `quantity`, `type`, `image`, `added_on`, `color_id`, `session_id`) VALUES ('$category_id', '$name', '$description', '$price', '$quantity', '$type', '$new_file_name', NOW(), '$color_id', '$session_id')";
$result = mysqli_query(get_connection(), $sql);
// var_dump($sql);
// var_dump($sql);
}//end elseif
header("Location: admin.php?page=product_list");
}//end if
这就是我尝试显示来自数据库的图像的方式
<!-- TABLE BODY-->
<tbody>
<?php
foreach ($result as $row) {
$sql_category = "SELECT * FROM `category` WHERE `id` = '{$row['category_id']}'";
$result_category = mysqli_query(get_connection(), $sql_category);
$row_category = $result_category->fetch_assoc();
$sql_color = "SELECT * FROM `color` WHERE `id` = '{$row['color_id']}'";
$result_color = mysqli_query(get_connection(), $sql_color);
$row_color = $result_color->fetch_assoc();
?>
<tr>
<td><p><?=$nr++?></p></td>
<td><p><?=$row_category['name']?></p></td>
<td><p><?=$row['name']?></p></td>
<td><p><?=$row['description']?></p></td>
<td><p><?=$row['price']?></p></td>
<td><p><?=$row['quantity']?></p></td>
<td><p><?=$row['type']?></p></td>
<?php var_dump(dirname(__FILE__, 2)."/images/".$row['image']); ?>
<td><p><img src="<?=dirname(__FILE__, 2)."/images/"?><?=$row['image'];?>" alt="" style="width: 100px; height: 100px;"></p></td>
<td><p><?=$row['added_on']?></p></td>
<td><p><?=$row['modified_on']?></p></td>
<td><p><?=$row_color['name']?></p></td>
<td>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=product_add_edit&id=<?=$row['id']?>">Edit</a></button>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=product_list&action=delete&id=<?=$row['id']?>" onclick="return confirm('Are you sure ?')">Delete</a></button>
<button type="button" class="btn btn-outline-primary"><a href="admin.php?page=cart&action=add_to_cart&id=<?=$row['id']?>">Add to cart</a></button>
</td>
</tr>
<?php }//end foreach ?>
</tbody>
<!-- END OF THE TABLE BODY -->
</table>
Var_dump 正在显示 C:\wamp64\www\Project6Bootstrap4-V1\modules\product_list.php:101:string 'C:\wamp64\www\Project6Bootstrap4-V1/images/6103fb6fe3da77.64812338.jpg'
如果我将该地址粘贴到浏览器中,它会显示该图像,但如果我尝试复制图像地址,它会给我 about:blank#blocked
同样在数据库中图像类型是 varchar(200)
这不是有效的 HTTP URL:
C:\wamp64\www\Project6Bootstrap4-V1/images/6103fb6fe3da77.64812338.jpg
您正在尝试让网页访问本地文件,但浏览器出于安全原因阻止了该文件。
删除 dirname(__FILE__, 2)
部分,仅使用与正在查看的页面相关的图像的 URL:
/images/6103fb6fe3da77.64812338.jpg
请注意,我不知道这是否是图像的正确 URL。你必须确定那是什么。但大概您要显示的图片是您网站的一部分,您只需要为该图片使用 URL。