gpt4 book ai didi

php - 无法显示文档文件

转载 作者:行者123 更新时间:2023-11-28 16:26:42 26 4
gpt4 key购买 nike

这是我的一小段代码,我试图在其中上传各种文档(文字+图像)。但似乎只有图像上传得很好。 Rest word 文件、excel 文件等未显示。需要进行哪些更改才能使它们正确可见?(请查看我的屏幕截图)screenshot of my output.注意:在屏幕截图中,第三个文件不可见

代码:

         <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="<?php echo base_url("css/bootstrap.css"); ?>">
</head>
<body>
<div class="container">

<p><?php echo $this->session->flashdata('statusMsg'); ?></p>
<form enctype="multipart/form-data" action="" method="post">
<div class="form-group">
<label>Choose Files</label>

<input type="file" class="form-control" name="userFiles[]" multiple/>
<button type="submit" class="btn-success form-control" name="fileSubmit" value="UPLOAD" width="100px">UPLOAD</button>
</div>
</form>



<div class="row">


<div class="thumbnail">
<?php $thumbnails = array_chunk( $files, 3 );
?>
<?php if(!empty($files)) {
foreach($thumbnails as $files) { ?>
<div class="row">
<?php foreach($files as $file) { ?>
<div class="col-md-4">
<div class="thumbnail">
<img src="<?php echo base_url('uploads/files/'.$file['file_name']); ?>" alt="" >
<p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
<?php } else { ?>
<p>Image(s) not found.....</p>
<?php } ?>

</div>
</div></div></div>
</body>
</html>

最佳答案

您应该添加一张图片并将其作为 $var 分配给文件扩展名

例子

<?php 
$notfound_image = "notfound.png"; //your not file found image
$excel_image = "excel.png"; //your excel image icon
$excel_file = "ok.xls"; //your excel file name

//get file extension
$parts=pathinfo($excel_file);
//echo $parts['extension']; //Returns "xls"
$excel_extension = $parts['extension'];

//check with if
//compare file extension

if ($excel_extension == "xls"){
echo '<img src="'.$excel_image.'" />';
} else {
echo '<img src="'.$notfound_image.'" />';
}

?>

通过我创建的这个代码示例,您可以对其他文件扩展名执行相同操作并检查 if 条件

让我们看看这是否适合你

 <?php $thumbnails = array_chunk( $files, 3 );
?>
<?php if(!empty($files)) {
foreach($thumbnails as $files) { ?>
<div class="row">
<?php foreach($files as $file) {
$filename = $file['file_name']; ?>
<div class="col-md-4">
<div class="thumbnail">
<?php

$parts=pathinfo($filename);
$extension = $parts['extension'];

switch ($extension) {
case 'xls':
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="path_to_excel_icon.png" alt="" >
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
break;

case 'docx':
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="path_to_word_icon.png" alt="" >
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
break;

case 'jpg':
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="<?php echo base_url('uploads/files/'.$file['file_name']); >
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
break;
case 'png':
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="<?php echo base_url('uploads/files/'.$file['file_name']); >
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
break;

default:
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="path_to_not_found_icon.png" alt="" >
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';

}

?>

</div>
</div>
<?php } ?>
</div>
<?php } ?>
<?php } else { ?>
<p>Image(s) not found.....</p>
<?php } ?>

关于php - 无法显示文档文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43617270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com