【问题标题】:Show uploaded files from MySQL based on file extension and number of files with hyperlinks根据文件扩展名和带有超链接的文件数显示从 MySQL 上传的文件
【发布时间】:2018-03-19 16:34:40
【问题描述】:

我正在尝试将以前上传的文件加载到 MySQL 数据库中。提交表单时文件存储在文件夹中,其中预览页面将包含指向这些文件的超链接(如附件)

我遇到的问题是:

  • 如果我只选择 1 个或多个具有特定文件扩展名的文件来上传,那么我将列出正确数量的文件,并且超链接将起作用。
  • 如果我选择 2 个具有 2 个不同文件扩展名(PDF 和 JPG)的文件,那么我将看到 4 个超链接。
  • 如果我选择 4 个具有 2 个不同文件扩展名(PDF 和 JPG)的文件,我将获得 8 个显示的超链接

我的代码中的 foreach 命令可能有错误。

请查看附件图片,了解加载文件后的外观。

index.php

<?php
include('dbconfig.php');

date_default_timezone_set('Europe/Oslo');
$loaddate1 = date('Y-m-d');
$loadtime1 = date('H-i-s');
?>

<form name="myForm" id="myForm" class="workshop add" action="" method="post" enctype="multipart/form-data">
<input id="" type="text" name="toolsn" value="">
<input id="" type="text" name="actioninsertdate" value="<?php echo $loaddate1;?>" readonly> <br/>
<input id="" type="text" name="actioninserttime" value="<?php echo $loadtime1;?>" readonly><br/>
<input type="file" name="files[]" multiple/><br/>
<button type="submit" name="submit">Submit</button></div>
</form>



<?php


if(isset($_POST['submit']))
{
$path = 'images/';
$toolsn = $_POST['toolsn'];
$actioninsertdate = $_POST['actioninsertdate'];
$actioninserttime = $_POST['actioninserttime'];
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_basename = substr($file_name, 0, strripos($file_name, '.')); // get file extension
    $file_ext = substr($file_name, strripos($file_name, '.')); // get file name
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];
    $newfilename = $toolsn.'-'.$loaddate1.'_'.$loadtime1.'_'.rand(1,20).$file_ext;
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }

    $query="INSERT workshop1 SET toolsn='$toolsn',file='$newfilename',type='$file_type',size='$file_size',actioninsertdate='$actioninsertdate',actioninserttime = '$actioninserttime'"
    or die(mysqli_error ($connection));
    if(empty($errors)==true || empty($toolsn)==false){
        if(is_dir($path.$toolsn)==false){
            mkdir("images/$toolsn", 0700);      // Create directory if it does not exist
        }
        if(is_dir("images/$toolsn/".$file_name)==false){
            //$newfilename = $toolsn.'-'.$loaddate1.'_'.$loadtime1.'_'.rand(1,4).$file_ext;
            move_uploaded_file($file_tmp,"images/$toolsn/".$newfilename);
        }else{                                  // rename the file if another one exist
            //$newfilename = $toolsn.'-'.$loaddate1.'_'.$loadtime1.'_'.rand(1,4);
            $new_dir="images/$toolsn/".time().$newfilename;
             rename($file_tmp,$new_dir) ;
        }
     mysqli_query($connection, $query);
    }else{
            print_r($errors);
    }
}

}
?>

read.php

<?php
include('dbconfig.php');
$sqls=mysqli_query($connection, "
SELECT
GROUP_CONCAT(DISTINCT toolsn SEPARATOR '<br />') as toolsn,
GROUP_CONCAT(DISTINCT type SEPARATOR '<br />') as type,
GROUP_CONCAT(DISTINCT file ORDER BY type SEPARATOR '<br />') as file,
GROUP_CONCAT(DISTINCT actioninsertdate SEPARATOR '<br />') as actioninsertdate,
GROUP_CONCAT(DISTINCT actioninserttime SEPARATOR '<br />') as actioninserttime
from workshop1
group by actioninserttime");

//get feedback why database not working
if (!$sqls) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
?>

<table id="table" class="table table-hover table-responsive">
<thead class="thead-default">
    <tr>
    <th>Toolsn</th>
    <th>Date added</th>
    <th>Time added</th>
    <th>Attachment</th>
    </tr>
</thead>
<?php
echo '<tbody id="tbody"><tr>';
while ($row = mysqli_fetch_array($sqls)) {
   echo '<td>'.$row['toolsn'].'</td>';
   echo '<td>'.$row['actioninsertdate'].'</td>';
   echo '<td>'.$row['actioninserttime'].'</td>';

echo '<td>';
$eachtoolsn=explode('<br />',$row['toolsn']);
$eachfile=explode('<br />',$row['file']);
$eachtype=explode('<br />',$row['type']);

foreach($eachfile as $listfile) {
//echo $listfile;

    foreach($eachtoolsn as $key => $listoolsn) {
    //echo [$key];
    }

    foreach($eachtype as $listtype) {
        if ($listtype === 'image/jpeg'){
            echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/'.$row['toolsn'].'/'.$listfile.'" width="48" height="48"></a>';
        } elseif ($listtype === 'application/pdf'){
            echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/icon-pdf.png" width="48" height="48"></a>';
        }
    }

echo '</td>';
echo '</tr>';}
echo '</tbody></table>';
?>

【问题讨论】:

标签: php mysql sql mysqli file-upload


【解决方案1】:

你的逻辑不好:

<?php
foreach($eachfile as $listfile) {
    foreach($eachtype as $listtype) { // <= your bug is here
        if ($listtype === 'image/jpeg'){
            echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/'.$row['toolsn'].'/'.$listfile.'" width="48" height="48"></a>';
        } elseif ($listtype === 'application/pdf'){
            echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/icon-pdf.png" width="48" height="48"></a>';
        }
    }
}

这是新版本:

<?php
foreach($eachfile as $key => $listfile) {
    if ($eachtype[$key] === 'image/jpeg')
        echo '<a href="images/'.$row['toolsn'].'/'.$listfile
        .'" target="_blank"><img src="images/'.$row['toolsn']
        .'/'.$listfile.'" width="48" height="48"></a>';
    elseif ($eachtype[$key] === 'application/pdf')
        echo '<a href="images/'.$row['toolsn'].'/'.$listfile
        .'" target="_blank"><img src="images/icon-pdf.png" '
        .'width="48" height="48"></a>';
}

$key 是您在使用 explode() 创建的数组中的行的索引

$eachfile = array(
    0=>'file1',
    1=>'file2',
)

$eachtype = array(
    0=>'PDF',
    1=>'JPG',
)

所以你只需使用文件的密钥来获取类型的好行。

【讨论】:

  • 通过使用你的代码我得到一个错误:注意:未定义的偏移量:1,或注意:未定义的偏移量:2?你知道为什么吗?
  • @stvlada 你在 $eachfile 和 $eachtype 中有什么? (使用 var_dump)
  • 对于上图中的第一行,我在 mysql 中存储了两个图像,我的 var_dump 看起来像数组:(size=2) 0 => string 'ASM5000-006-2017-10-08_11-05 -42_18.jpg'(长度=38)1 =>字符串'ASM5000-006-2017-10-08_11-05-42_16.jpg'(长度=38)
  • @stvlada 对于 $eachfile 的 var_dump 没问题,那么 $eachtype 的那个呢?
  • 如果我上传了一个或两个文件扩展名,取决于行:array (size=1) 0 => string 'image/jpeg' (length=10) OR array (size=2) 0 => 字符串“应用程序/pdf”(长度=15)1 => 字符串“图像/jpeg”(长度=10)
【解决方案2】:

如果只上传一个扩展名,我已经设法获得出色的文件预览。如果我上传具有 2 个不同扩展名的多个文件,那么我无法使代码在上传的 PDF 文件在预览时获得 PDF 图标的情况下工作。 所有文件都可以具有相同的图标(不同的代码)或图像上显示的图标。 出于某种原因,当 GROUP_CONCAT 用于收集一个或多个按“actioninsertime”分组的文件时,就会发生这种情况。

图片预览:

使用的代码:

$eachfile=explode('<br />',$row['file']);
$eachtype=explode('<br />',$row['type']);
$eachextension=explode('<br />',$row['extension']);

foreach ($eachtype as $listtype){}
foreach ($eachextension as $listextension){}


foreach ($eachfile as $listfile){
if ($listextension === 'pdf') {
echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/icon-pdf.png" width="48" height="48"></a>';}
}

foreach ($eachfile as $listfile){
if ($listextension === 'jpg') {
echo '<a href="images/'.$row['toolsn'].'/'.$listfile.'" target="_blank"><img src="images/'.$row['toolsn'].'/'.$listfile.'" width="48" height="48"></a>';}
}

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2015-01-03
    • 1970-01-01
    • 2019-06-19
    • 2013-12-19
    相关资源
    最近更新 更多