【问题标题】:Image not displaying using imageUrl, Everything seems right.PHP使用 imageUrl 不显示图像,一切似乎都正确.PHP
【发布时间】:2020-08-06 22:20:47
【问题描述】:

这是我的控制器中的功能

 public function singleEdit($propertyId){

  $property = $this->propertyModel->getPropertyById($propertyId);
  $image = $this->propertyModel->getImageByPropertyId($propertyId);
  $data = [
    'property' => $property,
    'image' => $image
  ];
  $this->view('adminPages/singleEdit', $data);
}

这是我模型中的函数

    public function getImageByPropertyId($propertyId){
    $this->db->query('SELECT imageId, imageName, imageUrl, propertyId, imageRank FROM images WHERE                                          propertyId = :propertyId');
    $this->db->bind(':propertyId', $propertyId);
    $results = $this->db->resultSet();
    return $results;
}

这是我的观点

    <?php
include APPROOT . "/views/templates/header.php"; ?>  
  <div class="container-xl">
      <div class="row">
          <div class="col">
           <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
                <ol class="carousel-indicators">
                    <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
                    <li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
                    <li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
                </ol>
                 <div class="carousel-inner">    
                    <div class="carousel-item active">
                    <?php foreach ($data['image'] as $img): ?>
                    <?php echo $img->imageUrl; ?>
                    <img src="<?php echo $img->imageUrl; ?>" class="d-block w-100" alt="image">
                    <div class="carousel-caption d-none d-md-block">
                        <h5><?php echo $img->imageName; ?></h5>
                        <p>Nul </p>
                    </div>
                    </div> 
                    <?php
endforeach; ?>
                </div>
                <a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev">
                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
                </div>
          </div>
          </div>
      </div>
      <div class="row p-3 m-3">
            <div class="col">
                <div class="container-xl">
                    <h3>Add Images</h3>
                    <table class="table table-hover p-4">
                    <thead>
                        <tr>
                        <th scope="col">
                            <div>
                            <?php flash('imageError'); ?>
                                <form action="<?php echo URLROOT; ?>/Admins/imageUpload/<?php echo $data['property']->propertyId; ?>" method="post" enctype="multipart/form-data">
                                <input type="hidden" name="imageRank" value="mainImage" id="imgRank">
                                <input type="hidden" name="propertyId" value="<?php echo $data['property']->propertyId; ?>"> 
                                <input type="File" name="picFile">
                                <input type="submit" value="Upload" formaction="<?php echo URLROOT; ?>/Admins/imageUpload/<?php echo $data['property']->propertyId; ?>" formmethod="post" formenctype="multipart/form-data">            
                                </form>
                            </div>
                        </th>
                        </tr>
                        <tr>                     
                        </tr>
                    </thead>
                    </table>
                </div> 
                </div>
            </div> 
            <div class="col"></div>
        </div>
    </div>  
<?php include APPROOT . "/views/templates/footer.php"; ?>

如果我尝试通过在图像标签中输入 src 来显示图像;例如,它可以工作

<img src="<?php echo URLROOT . "/images/properties/188118_1596663122.jpg"; ?>">

一旦我尝试使用 imageUrl 显示图像将不会显示。 如果我在图像标签外显示 imageUrl,它会显示出来,但在图像标签内,则不会显示任何图像。

【问题讨论】:

  • 处理此行时,您的输出 HTML 是什么样的? &lt;img src="&lt;?php echo $img-&gt;imageUrl; ?&gt;" class="d-block w-100" alt="image"&gt;src 的值是188118_1596663122.jpg,还是/images/properties/188118_1596663122.jpg,还是别的什么?
  • 我在上传脚本中发现了问题。我有两个变量 $filePath = $folderName. rand(10000, 990000). '_'. time().'.'.$ext;'imageUrl' =&gt; $imageUrlBase. rand(10000, 990000). '_'. time().'.'.$ext, 。第一个是上传到正确的文件夹,第二个是将 imageUrl 保存到数据库。我只是意识到他们正在为图像创建差异名称。我在一个新变量中通过rand(10000, 990000). '_'. time().'.'.$ext 解决了这个问题。

标签: php model-view-controller


【解决方案1】:

您在 imageUrl 中存储了什么? 如果您使用 img 标签存储信息,那么您可以简单地通过 echo imageUrl 显示 或检查 imageUrl 实际存储的图像的完整路径,图像名称后跟扩展,然后标签将显示图像,否则提供保存图像的完整路径,如 img src="path/imagename.extension" 将显示图像

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2022-10-06
  • 1970-01-01
  • 2021-11-28
  • 2012-03-28
相关资源
最近更新 更多