繁体   English   中英

如何使用php保存图像源变量

[英]How to save image source variable using php

是否有使用 PHP 将<img src="XXXXX">图像路径保存到数据库的方法。 为什么 src 路径不在 PHP 中选择。 我的 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.jpgimg/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM