php img src输入错误的网址

时间:2018-10-19 15:37:11

标签: php html

echo '<img src="'.$row['image_url'].'" class="img-thumbnail">'; 

这给了我

的链接
http://localhost/events-uploads/5bc9f6074a2788.39011795.png'

代替

http://localhost/cinematheque-app/events-uploads/5bc9f6074a2788.39011795.png

这就是为什么我上传的图片不会显示...

如何显示正确的网址?

每当我保存在数据库中时,我都添加了'cinematheque-app'关键字,例如,URL可以像这样保存:“ cinematheque-app / events-uploads / 5bc9f6074a2788.39011795.png”

但是只要我使用此将其添加到img src中

echo '<img src="'.$row['image_url'].'" class="img-thumbnail">';

它给了我http://localhost/cinematheque-app/cinematheque-app/events-uploads/5bc9f6074a2788.39011795.png

我正在这样保存它...

$file =$_FILES['image_url'];
$fileName = $_FILES['image_url']['name'];
$fileTmpName = $_FILES['image_url']['tmp_name'];
$fileSize = $_FILES['image_url']['size'];
$fileError = $_FILES['image_url']['error'];
$fileType = $_FILES['image_url']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));



if (in_array($fileActualExt, $allowed)){
    if ($fileError === 0){
        if($fileSize > 20000) {
            $fileNameNew = uniqid('', true).".".$fileActualExt;
            $fileDestination = '../events-uploads/'.$fileNameNew;
            $actual_url = "cinematheque-app".trim( $fileDestination, "." );

            move_uploaded_file($fileTmpName, $fileDestination);

            $mysqli->query("INSERT INTO events (title, time, date, branch, description, price, status, created_at, image_url) 
            VALUES ('$title', '$time', '$date', '$branch', '$description', '$price', '$status', '$created_at', '$actual_url')") or die($mysqli->error);

        header("Location: ../manage-events.php?added=success"); 
        }
        else {
            //File is too big
        }
    }else{
        //There is some error
    }
}       
}

我正在得到这样的东西...

                    <?php while ($row = mysqli_fetch_array($events_per_page)) { ?>
                    <tr class="table table-borderless table-sm text-center">
                       <td><?php echo $row['title']; ?> </td>
                       <td><?php echo date("h:ia", strtotime($row['time'])); ?> </td>
                       <td><?php echo date("M-d-Y", strtotime($row['date'])); ?> </td>
                       <td><?php echo $row['branch']; ?></td>
                       <td><?php echo $row['description']; ?></td>
                       <td><?php echo $row['price']; ?></td>
                       <td>
                       <?php
                        // $system_url = "cinematheque-app";
                        // $removed_front_slash_url = trim( $row['image_url'], "/" );
                        // $actual_url = trim( $row['image_url'], "." );

                        echo '<img src="'.$row['image_url'].'" class="img-fluid">'; 
                        // echo $row['image_url'];
                       ?>
                       </td>

查询数据

$total_events = $mysqli->query("SELECT * from events") or die($mysqli->error);

The events table

这是我进行转换的地方...

$number_of_results = $total_events->num_rows;
$results_per_page  = 4;
$number_of_pages   = ceil($number_of_results / $results_per_page);

$this_page_first_result = ($page-1)*$results_per_page;
$sql="SELECT * FROM events ORDER BY created_at DESC LIMIT " .$this_page_first_result . "," .  $results_per_page;
$events_per_page = $mysqli->query($sql) or die($mysqli->error);

我知道了!

我用过

echo '<img src="/'.$row['image_url'].'" class="img-fluid">'; 
$actual_url = "cinematheque-app".trim( $fileDestination, "." );

2 个答案:

答案 0 :(得分:2)

  

如何显示正确的网址?

您将正确的URL存储在数据库中,或者查询正确的表。

答案 1 :(得分:1)

尝试添加包含如下文件的文件夹的名称:

false

在数据库中,仅保存文件名(不包含路径或文件夹名称),并在html中添加文件夹名称。