我有一个registration.php文件,文件上传。执行查询时,通过用户选择的部门名称动态创建文件夹,然后在ID内创建子文件夹。部门文件夹及其内部可以成功保存上传的文件。成功运行
但是问题是有一个list.php在表格视图中显示了所有上传的文件。当我单击列表中的上传文件名链接时,我无法下载上传的文件。它将在浏览器中打开新的标签内容为空。
请有人帮我解决这个问题。
Registration.php
<?php
$message = ""; // initial message
if( isset($_POST['submit_data']) ){
// Includs database connection
include "db_connect.php";
// Gets the data from post
$filename = "RC";
$department = $_POST['department'];
$title = $_POST['title'];
$applicant = $_POST['applicant'];
$head = $_POST['head'];
$date = $_POST['date'];
$submitted = $_POST['submitted'];
if (isset($_POST['excelFile1'])) {
@$aMyUploads[0] = $_POST['excelFile1'];
}
if (isset($_POST['excelFile2'])) {
@$aMyUploads[1] = $_POST['excelFile2'];
}
if (isset($_POST['ppt'])) {
@$aMyUploads[2] = $_POST['ppt'];
}
// Makes query with post data
$query = "INSERT INTO registration (department, title, applicant, head, date, excelFile1, excelFile2, ppt, submitted) VALUES ('$department', '$title', '$applicant', '$head', '$date',' ".$aMyUploads[0]." ',' ".$aMyUploads[1]." ',' ".$aMyUploads[2]." ', '$submitted')";
// Executes the query
// If data inserted then set success message otherwise set error message
// Here $db comes from "db_connection.php"
if( $db->exec($query) ){
$id = $db->lastInsertRowID();
$aMyUploads = array();
foreach($_FILES as $aFile)
{
if(!empty($aFile))
{
$path = "$department/" . $filename.sprintf('%04d', $id).'/';
if(!is_dir($path)){
mkdir ($path, 0777, true);
}
$path = $path . $aFile["name"];
if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $path))){
$aMyUploads[] = $path;
} else{
$aMyUploads[] = '';
}
}
}
header('Location: list.php');
$message = "Data is inserted successfully.";
}else{
$message = "Sorry, Data is not inserted.";
}
}
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
<link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/cupertino/jquery-ui.min.css" />
<script type="text/javascript">
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
</script>
<script>
$(function() {
$('#input_date').datepicker();
});
</script>
<style type="text/css">
input[type=submit] {
background: url(ok.png);
border: 0;
height: 18px;
width: 100px;
float:left;
display: block;
}
</style>
</head>
<body>
<br>
<br>
<div><?php echo $message;?></div>
<form enctype="multipart/form-data" id="data" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table>
<tr>
<th style="width:100px;">department</th>
<td><input type="radio" name="department" value="AT" <?php if(isset($_POST['department']) && $_POST['department']=="AT") { ?>checked<?php } ?>> AT
<input type="radio" name="department" value="CT" <?php if(isset($_POST['department']) && $_POST['department']=="CT") { ?>checked<?php } ?>> CT
<input type="radio" name="department" value="IC" <?php if(isset($_POST['department']) && $_POST['department']=="IC") { ?>checked<?php } ?>> IC
<input type="radio" name="department" value="NS" <?php if(isset($_POST['department']) && $_POST['department']=="NS") { ?>checked<?php } ?>> NS
<input type="radio" name="department" value="SE" <?php if(isset($_POST['department']) && $_POST['department']=="SE") { ?>checked<?php } ?>> SE
<input type="radio" name="department" value="QM" <?php if(isset($_POST['department']) && $_POST['department']=="QM") { ?>checked<?php } ?>> QM
</td>
</tr>
<tr>
<th style="width:100px;">title</th>
<td><input type="text" size="24" name="title"</td>
</tr>
<tr>
<th style="width:100px;">applicant</th>
<td><input type="text" size="24" name="applicant"</td>
</tr>
<tr>
<th style="width:100px;">head</th>
<td><input type="text" size="24" name="head"</td>
</tr>
<tr>
<th style="width:100px;">date</th>
<td><input type="text" id="input_date" name="date"></td>
</tr>
<tr>
<th style="width:100px;">excelFile1</th>
<td><label for="file" class="input input-file">
<input type="text" id="fileText" name="excelFile1" />
<input type="file" size="24" id="f1" name="excelFile1" style="display: none;" >
<input type="button" value="excelFile1" onclick="document.getElementById('f1').click();" />
</label>
</td>
</tr>
<tr>
<th style="width:100px;">excelFile2</th>
<td><label for="file" class="input input-file">
<input type="text" id="fileText1" name="excelFile2" />
<input type="file" size="24" id="f2" title="" name="excelFile2" style="display: none;" >
<input type="button" value="excelFile2" onclick="document.getElementById('f2').click();" />
</label>
</td>
</tr>
<tr>
<th style="width:100px;">ppt</th>
<td><label for="file" class="input input-file">
<input type="text" id="fileText2" name="ppt" />
<input type="file" size="24" id="f3" title="" name="ppt" style="display: none;" >
<input type="button" value="ppt" onclick="document.getElementById('f3').click();" />
</label>
</td>
</tr>
</tbody>
</table>
<br><br>
<input name="submit_data" type="submit" value="" onClick="return empty()" >
<input type="hidden" id="submitted" name="submitted" value="submitted">
<input type="image" src="image/cancel.png" onclick="Test();return false;">
</form>
<script type="text/javascript" language="javascript">
var filename;
var x = document.getElementById('f1').onchange = function () {
filename = this.value.split(String.fromCharCode(92));
document.getElementById("fileText").value = filename[filename.length-1];
};
var filename1;
var y = document.getElementById('f2').onchange = function () {
filename1 = this.value.split(String.fromCharCode(92));
document.getElementById("fileText1").value = filename1[filename1.length-1];
};
var filename2;
var z = document.getElementById('f3').onchange = function () {
filename2 = this.value.split(String.fromCharCode(92));
document.getElementById("fileText2").value = filename2[filename2.length-1];
};
</script>
<script>
document.getElementById("submit_data").onclick = function() {
document.getElementById("submitted").value = "1";
// ^
};
function Test() {
window.location.href = "list.php";
}
</script>
<br>
<br>
<!-- footer -->
<div id="footer">
</div>
<!-- footer end -->
</body></html>
在list.php中,
<td>
<a href="download.php?link=<?php print $row['excelFile1'];?>"><?php echo basename($row['excelFile1']);?></a>
</td>
download.php
<?php
if(isset($_GET['link']))
{
include "db_connect.php";
// Makes query with rowid
$filename = "RC";
$id = isset($_POST['id']) ? $_POST['id'] : '';
$department = isset($_POST['department']) ? $_POST['department'] : '';
$var_1 = $_GET['link'];
$query = "SELECT rowid, * FROM registration";
// Run the query and set query result in $result
// Here $db comes from "db_connection.php"
$result = $db->query($query);
$dir = "$department/" . $filename.sprintf('%04d', $id).'/';
$file = $dir . $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>