【问题标题】:PHP: how to display an uploaded filename in the right place in a tablePHP:如何在表格的正确位置显示上传的文件名
【发布时间】:2016-12-21 20:21:37
【问题描述】:

我被要求创建一个网站(用作内部资源),其中 MySQL 表的结果显示为 HTML 表。每条记录都有一个上传按钮,人们可以在其中上传某些图像和 PDF。然后,他们可以发送此记录列表和上传文件。

我已经设法使(非常)基本的上传工作。我的问题是我不知道如何仅在它应该去的记录中显示上传的文件名。我怎样才能做到这一点?

我已经尝试过愚蠢的事情,只是在<td> 中显示{$file},但当然会在每条记录中显示文件名。如果t20pctID 不等于文件名的一部分,我尝试将 $file 设置为空,但最终会导致显示相同记录的永无止境的循环,然后我的 Firefox 崩溃。

这是我正在使用的代码,它改编自多个外部资源上的代码。提前谢谢你。

Claims.php(遵循与在线商店中的购物车类似的模式)

<?php
    include '../config/database.php';
    include 'layout_head.php';

    $action = isset($_GET['action']) ? $_GET['action'] : "";
    $t20pctID = isset($_GET['song_id']) ? $_GET['song_id'] : "";
    $track_title = isset($_GET['track_title']) ? $_GET['track_title'] : "";
    $file_name = isset($_GET['file']) ? $_GET['file'] : "";
    $user_id = 1;

    //display a message
    if ($action =='removed') {
        echo "<div class='alertInfo'>
                 <strong>{$track_title}</strong> was removed!
              </div>
        ";
    } elseif ($action =='file_uploaded') {
        echo "<div class='alertInfo'>
                 New file <strong>successfully</strong> uploaded!
              </div>
        ";
    } elseif ($action =='sent') { 
        echo "<div class='alertInfo'>
                 <strong>Sent OK!</strong>
              </div>
        ";
    } elseif ($action =='sent_failed') { 
        echo "<div class='alertInfo'>
                 <strong>Sent failed :/</strong>
              </div>
        ";
    }

    $query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, t.record_label, t.publication_year, c.file FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%Sony%' ORDER BY t.main_artist, t.track_title ASC";  
    $stmt=$con->prepare($query);
    $stmt->execute();

    $num = $stmt->rowCount(); 
    if ($num > 0) { 
        echo "<table class='records'>
                <tr>
                    <th>Main Artist</th>
                    <th>Track Title</th>
                    <th>Original Album</th>
                    <th>Record Label</th>
                    <th>Publication Year</th>
                    <th>Documentation (only .jpg, .gif, .png, .pdf allowed)</th>
                    <th style='width:100px;'></th>
              </tr>

        ";

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);

            $form_name = "file_upload_".$t20pctID;
            echo "<tr>
                    <td>
                        <div class='t20pctID' style='display:none;'>{$t20pctID}</div>
                        <span>{$main_artist}</span>
                    </td>
                    <td>
                        <div class='track_title'>{$track_title}</div>
                    </td>
                    <td>
                        <span>{$original_album}</span>
                    </td>
                    <td>{$record_label}</td>
                    <td>{$publication_year}</td>
                    <td>
                        <form name='$form_name' action='upload.php?id=$t20pctID' method='post' enctype='multipart/form-data'>
                            <input type='file' name='upload_file'>
                            <input type='submit' value='Upload' class=''>
                        </form>";
                        if ($file_name) {
                            $user_id = 1;
                            $query_update = "UPDATE cart SET file='$file_name', uploaded_id='$uploaded_id' WHERE t20pctID=? AND user_id=?";
                            $stmt = $con->prepare($query_update);

                            // bind values
                            $stmt->bindParam(1, $t20pctID);
                            $stmt->bindParam(2, $user_id);
                            //update
                            $stmt->execute();
                        }
                        echo "{$file}"; //

            echo "</td>
                    <td>
                        <a href='remove_from_cart.php?song_id={$t20pctID}&track_title={$track_title}' class='btnCTARemove'>
                            <span class='glyphicon glyphicon-remove'></span>Remove&nbsp;&nbsp;
                        </a>
                    </td>
                  </tr>

            ";
        }

        echo "<tr>
                <td colspan='6'></td>
                <td id='checkout'>
                    <a href='checkout.php' class='btnCTASend'>
                        <span class='glyphicon glyphicon-shopping-cart'></span>Send
                    </a>
                </td>
              </tr>
        ";
        echo "</table>";

    } else { 

        if ($action !='sent' && $action !='removed') { 
            echo "<div class='alertInfo'>
                     <strong>No songs found</strong> in your cart!
                  </div>
            ";
        }
    }

    include 'layout_foot.php';
?>

上传.php

<?php
    $t20pctID = $_GET['id'];
    date_default_timezone_set('CET');
    $date = date('Y-m-d-H:i:s');

    if (isset($_FILES['upload_file'])) {
        $file = $_FILES['upload_file'];

        //file properties
        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];
        $file_ext = explode('.', $file_name);
        $file_ext = strtolower(end($file_ext));
        $allowed = array('jpg', 'pdf');

        if (in_array($file_ext, $allowed)) {
                if ($file_size <= 2097152) { 
                    $file_name_new = $t20pctID. '_' . $date . '.' . $file_ext;
                    $file_destination = 'tmp_uploads/' .$file_name_new;

                    if (move_uploaded_file($file_tmp, $file_destination)) {
                        header('Location: claims.php?action=file_uploaded&song_id=' . $t20pctID .'&file='. $file_name_new);
                    } else {
                        echo "Error moving the file(s).";
                    }

                } else {
                    echo "Error: your file is too big. The maximum size permitted is 2 MB.";
            }
        } else {
            echo "Error: your file is not permitted (". $file_name."). Only .jpg and .pdf permitted.";
        }

    }
?>

【问题讨论】:

    标签: php mysql file-upload pdo


    【解决方案1】:

    显然我有一个天才,并通过如下更新upload.php 设法解决了这个问题。

    //previous code as in the OP
     $file_destination = $uploads_dir_name.$file_name_new;
    
    if (move_uploaded_file($file_tmp, $file_destination)) {
        $query_update = "UPDATE cart SET file='$file_name_new' WHERE t20pctID='$t20pctID' AND user_id='$user_id'";
        $stmt = $con->prepare($query_update);
        $stmt->execute();
        header('Location: claims.php?action=file_uploaded&song_id=' . $t20pctID . '&file='.$file_name_new);
        } else {
        header('Location: claims.php?action=upload_failed&song_id=' . $t20pctID);
    }
    //code after this line as in the OP
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多