【发布时间】:2014-10-05 01:21:08
【问题描述】:
他,我试图弄清楚如何在上传后将文件名和扩展名显示为确认消息。
这就是我现在拥有的
<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'test_db');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `file` (
`name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo "Your file has been uploaded";
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>
而不是回显“您的文件已上传”,我希望它列出已上传文件的名称和扩展名,然后在末尾“已成功上传”。
示例:我上传contact.pdf。我喜欢显示“contact.pdf 已成功上传” 我希望这适用于任何文件和扩展名。 提前非常感谢。
【问题讨论】:
-
您可以更改相应的
echo字符串。不过,在输出过于乐观的消息之前,您可能需要测试数据库存储的 temporary 文件名是否比另一个页面请求更持久。 -
我该怎么做呢?我对这一切还是陌生的。
标签: php forms file-upload