gpt4 book ai didi

PHP:如何在表格的正确位置显示上传的文件名

转载 作者:行者123 更新时间:2023-11-30 22:14:20 24 4
gpt4 key购买 nike

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

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

我已经尝试了愚蠢的事情,只显示 {$file}<td>但当然会在每条记录中显示文件名。如果 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.";
}

}
?>

最佳答案

很显然,我灵机一动,通过如下更新 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

关于PHP:如何在表格的正确位置显示上传的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38958292/

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