【问题标题】:Uploading and displaying images in php在php中上传和显示图像
【发布时间】:2021-03-01 09:05:41
【问题描述】:

我正在建立一个网站,它将具有这样的形式

<form action="demo.php" method="post" enctype="multipart/form-data">
        
         <input type="file" name="fileToUpload" id="fileToUpload">
                    
            <div class="col-12 text-center tm-submit-container">
                <button type="submit" href="#" class="btn btn-primary tm-btn-submit">Submit</button>
            </div>
            
    </form>

这里我需要在这个页面上传一张图片并在 demo.php 页面中显示(提交时重定向) 演示.php

<body>
<p style="text-align: right;" id="time"></p>
<h1 style="text-align: center;">
    welcome
</h1>
</body>

有人可以帮我怎么做吗?我在 w3 学校尝试过教程,但没有奏效。

【问题讨论】:

  • 你想要什么??
  • 我想在demo.php中显示上传的图片
  • 你也在数据库和文件夹中上传图片??
  • 只有文件夹不是数据库

标签: javascript php html file-upload


【解决方案1】:

查看页面:-

<form method="post" action="" enctype='multipart/form-data'>
  <input type="file" name="fileToUpload" id="fileToUpload">
                    
<div class="col-12 text-center tm-submit-container">
    <button type="submit" name="but_upload" class="btn btn-primary tm-btn-submit">Submit</button>
</div>
</form>

插入代码:-

<?php
include("config.php");

if(isset($_POST['but_upload'])){
 
  $name = $_FILES['fileToUpload']['name'];
  $target_dir = "upload/";    // create a folder in a directory.
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

  // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) ){
 
     // Insert record
     $query = "insert into images(images) values('".$name."')";
     mysqli_query($con,$query);
  
     // Upload file
     move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_dir.$name);

  }
 
}
?>

查看图片代码:-

<?php
    $sql = "select * from images";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)){
          $image = $row['image'];
          $image_src = "upload/".$image; 
          <img src='<?php echo $image_src;  ?>' >;
    }
?>

【讨论】:

  • 我不想插入数据库,我想插入文件夹
  • 首先你应该插入数据库并移动到文件夹中,然后查看该图像..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多