是否有使用PHP将<img src="XXXXX">
映像路径保存到数据库的方法。为什么PHP中没有选择src路径。我的PHP代码是
<?php
include('conn.php');
if(!empty($_POST["submit1"]) || isset($_POST["submit1"])){
if (isset($_POST['img1'])) {
$filename1= $_POST['img1'];
}
if (isset($_POST['img2'])) {
$filename2= $_POST['img2'];
}
}
?>
我的html代码是
<html>
<head>
<title>Test
</title>
</head>
<body>
<form action="phpscript/test.php" method="post" enctype="multipart/form-data">
<div>Images </div>
<input type="file" id="file" name="file">
<div class="row align-text-center">
<div class="col-sm2">
<input type="image" id="img1" name="img1" src="img/1.jpg" width="150" height="150" style="border: "/>
</div>
<div class="col-sm2">
<input type="image" src="img/2.jpg" width="150" height="150" id="img2" name="img2" style="border: " />
</div>
<div class="col-sm2">
<button class="btn " type="submit" id="submit1" name="submit1">Submit</button>
</form>
</body>
</html>
实际上,我需要在php代码中选择图像路径。但在php代码中,$filename1
和$filename2
的值都是NULL。这是在php代码中获取图像路径的另一种方法吗?
您对<input type="image">
有一个误解。这只是将图像定义为提交按钮。看看这个作为参考https://www.w3schools.com/tags/att_input_type_image.asp现在,要将图像路径保存在PHP变量中,您必须选择该图像作为文件。
<input type="image" id="img1" name="img1" src="img/1.jpg" width="150" height="150" style="border:"/>
<input type="image" src="img/2.jpg" width="150" height="150" id="img2" name="img2" style="border:" />
这仅仅使得输出img/1.jpg
和img/2.jpg
的图像。要在PHP变量中获取图像路径,您必须更改以下行:
<input type="file" name="img1">
<input type="file" name="img2">
然后您可以选择一个图像文件。在PHP部分,用以下代码更改代码:
<?php
include('conn.php');
if(!empty($_POST["submit1"]) || isset($_POST["submit1"])){
if (isset($_FILES["img1"])) {
$filename1= $_FILES["img1"]["name"];
}
if (isset($_FILES['img2'])) {
$filename2= $_FILES["img2"]["name"];
}
}
?>
要了解有关PHP文件/图像上传的详细信息,请查看https://www.php.net/manual/en/features.file-upload.post-method.php