如何将用户投资组合图像上传到同一目录并在每个用户配置文件上显示图像

时间:2018-02-13 12:02:39

标签: php image file

我在PHP中遇到了非常普遍的问题。我有一个针对不同用户组合的网站。现在,我所说的是,如果一个用户将图像上传到目录(所有用户的一个目录)组合,如果任何人将检查他们的投资组合,那么用户的图像将出现在该用户投资组合页面上。 header.php中提供了数据库连接和会话这些是我的代码..



<?php require_once('inc/header.php');


$_SESSION['user_id'];


?>
<body>

<div class="wrapper">
    <div class="sidebar" data-background-color="white" data-active-color="danger">

    <!--
    Tip 1: you can change the color of the sidebar's background using: data-background-color="white | black"
    Tip 2: you can change the color of the active button using the data-active-color="primary | info | success | warning | danger"
  -->

      <div class="sidebar-wrapper">
            <?php require_once('inc/text-logo.php');?>
            
            <!-- sidebar Nav -->
            <ul class="nav">
                <li>
                    <a href="dashboard.php">
                        <i class="ti-panel"></i>
                        <p>DASHBOARD</p>
                    </a>
                </li>
                <li>
                    <a href="index.php">
                        <i class="ti-user"></i>
                        <p>MY PROFILE</p>
                    </a>
                </li> 
                <li class="active">
                    <a href="portfolio.php">
                        <i class="fa fa-camera-retro"></i>
                        <p>PORFTOLIO</p>
                    </a>
                </li>
                <li>
                    <a href="icons.html">
                        <i class="fa fa-share-alt"></i>
                        <p>SOCIAL LINKS</p>
                    </a>
                </li>
                <li>
                    <a href="notifications.html">
                        <i class="ti-bell"></i>
                        <p>Notifications</p>
                    </a>
                </li>
            </ul>
      </div>
    </div>

    <div class="main-panel">

<?php require_once('inc/topnav.php');?>

        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-3 col-sm-6">
                        <div class="card">
                            <div class="content">
                                <div class="row">
                                    <div class="col-xs-5">
                                        <div class="icon-big icon-danger text-center">
                                            <i class="fa fa-upload"></i>
                                        </div>
                                    </div>
                                    <div class="col-xs-7">
                                        <div class="numbers">
                      <p>UPLOAD <br> MULTIIMAGES</p>
                                        </div>
                                    </div>
                                </div>
                                <div class="footer">
                                    <hr />
                                    <div class="stats">
                                    
                                    <?php
                    if(isset($_POST['upload'])){
                      $date = date('d-m-Y H:ia');
                      $images = $_FILES['images']['name'];
                      $images_tmp = $_FILES['images']['tmp_name'];
                      $uploads_dir = '../uploads';
                    move_uploaded_file($images_tmp, "$uploads_dir/$images");
                      if(empty($images)){
                        $error = "PLEASE SELECT AT LEAST ONE PHOTO.";
                      }
                      else{
                        $insert_query = "INSERT INTO `portfolio` (`id`, `date`, `images`, `user_id`) VALUES (NULL, '$date', '$images', '".$_SESSION['user_id']."')";
                        if(mysqli_query($con, $insert_query)){
                          $msg = "IMAGE UPLOADED TO YOUR PORTFOLIO";
                        }
                        else{
                          $error = "IMAGE UPLOADING FAILED, PLEASE CHECK";
                        }
                      }
                    }
                  ?>
                                    
                    <form action="" method="POST" enctype="multipart/form-data">
                      <input type="file" name="images" class="form-control" accept="image/x-png,image/gif,image/jpeg"><hr>
                      <input type="submit" name="upload" value="Upload" class="btn btn-info">
                    </form>
                                   
                                   
                                    </div>
                                </div>
                            </div>
                        </div>
          </div>
                   
                                    
                                    <?php
                    if(isset($error)){
                  ?>
                    <div class="col-lg-3 col-sm-6">
                        <div class="card">
                            <div class="content">
                                <div class="row">
                                    <div class="col-xs-5">
                                        <div class="icon-big icon-danger text-center">
                                            <i class="fa fa-exclamation-triangle"></i>
                                        </div>
                                    </div>
                                    <div class="col-xs-7">
                                        <div class="numbers">
                                        </div>
                                    </div>
                                </div>
                                <div class="footer">
                                    <hr />
                                    <div class="stats">
                                   <?php
                      echo "<center><span style='font-size:12px;color:darkred'><h4>$error</h4></span></center>";
                                   ?>
                                    </div>
                                </div>
                            </div> <!-- end content -->
                        </div>  <!--end card-->
          </div> <!--- end Col -->
                      
                                <?php
                    }
                    else if(isset($msg)){
                  ?>
                    <div class="col-lg-3 col-sm-6">
                        <div class="card">
                            <div class="content">
                                <div class="row">
                                    <div class="col-xs-5">
                                        <div class="icon-big icon-danger text-center">
                                            <i class="fa fa-check"></i>
                                        </div>
                                    </div>
                                    <div class="col-xs-7">
                                        <div class="numbers">
                                        </div>
                                    </div>
                                </div>
                                <div class="footer">
                                    <hr />
                                    <div class="stats">
                                   <?php
                        echo "<center><span style='font-size:12px;color:darkgreen'><h4>$msg</h4></span></center>";
                                   ?>
                                    </div>
                                </div>
                            </div> <!-- end content -->
                        </div>  <!--end card-->
          </div> <!--- end Col -->
               <?php } ?>
                </div> <!-- end row here -->
        </div>
                <div class="row">
                <?php
          $query = "SELECT * FROM portfolio WHERE '".$_SESSION['user_id']."' = user_id ORDER BY id DESC";
          $run = mysqli_query($con, $query);
          
          if(mysqli_num_rows($run) > 0){
            
          }
          else{
            echo "<center><span class='text-danger'><h3>You Didn't Upload any Portfolio Image Now!</h3></span></center>";
        ?>
          <?php
            while($row = mysqli_fetch_array($run)){
              $id = $row['id'];
              $images = $row['images']['name'];
              $images_tmp = $row['images']['tmp_name'];
              $user_id = $row['user_id'];
          ?>
        
          <div class="col-sm-3">
          <img src="../uploads/<?php echo $images;?>" width="200px" alt="<?php echo $user_fullname;?>" class="img-thumbnail">
          </div>
        <?php } }?>
                </div>
            </div>
        </div>


<?php require_once('inc/footer.php');?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

有几个选项

1)将用户图像的名称保存在数据库中,并在显示组合时获取并显示它们。 (我推荐)

2)为每个用户或投资组合创建一个文件夹

3)保存可接受时,在图像名称上标记user12__filename.jpg。并通过glob('/images/user'.$id.'__*')功能

显示接收文件时