好的。自 2011 年 8 月 24 日以来,我一直在这个网站上搜索(这并不重要)以寻找一种显示用户上传文件的方法。我在管理员端有我的表格,一切正常。我还有一个表格,显示用户在表格中填写的任何内容,该表格也有效。但是我无法在表格中查看该文件或其名称。
我的数据库表有一个主 ID auto_increment int(11) unsigned。
这是我写的代码:
//This gets all the other information from the form
$company=$_POST['company'];
$location=$_POST['location'];
$pic=($_FILES['userfile']['name']);
$query = "INSERT INTO user_DB VALUES ('','$company', '$location', '$userfile' )";
//target to the path of my files
$target_path = "uploads/post_id/";
if(!is_dir($target_path)) mkdir($target_path);
$uploadfile = $target_path . basename($_FILES['userfile']['name']);
//Move the uploaded file to $taget_path
(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile));
在您填写详细信息的表格上,我有以下内容:
<tr>
<td><label for="company_name">Company Name</label></td>
<td><input type="text" name="company" id="company" value="" size="38" /></td>
</tr>
<tr>
<td><label for="location">Location</label></td>
<td><input type="text" name="location" id="location" value="" /></td>
</tr>
<tr>
<td>Upload a File:</td>
<td><input name="userfile" id="userfile" type="file" /></td>
</tr>
前端显示查询结果的表格是这样的,就是文件字段。
echo "<td>";
echo "<a href=../admin/uploads/post_id/> $row'userfile'</a>";
echo "</td>";
如您所见,我正在尝试获取文件名以及文件本身。如果它是 pdf/jpg/doc,我应该可以在单击链接时查看/下载它。
Ant 思想...
最佳答案
关于您可以更改哪些内容以使其正常工作的一些建议。
1。上传表格
您的表单标签是什么样的?不要忘记按以下方式包含 enctype
参数:
<form type="post" action="" enctype="multipart/form-data">
...
</form>
2。 sanitizer
$company = mysql_real_escape_string($_POST['company']);
$location = mysql_real_escape_string($_POST['location']);
$pic = mysql_real_escape_string($_FILES['userfile']['name']);
以上几行是帮助防止您的查询遭受 SQL 注入(inject)攻击的第一步。
3。 SQL查询
$userfile
不存在,因为您实际上已将文件名分配给 $pic
,因此您的查询应如下所示:
$query = "INSERT INTO user_DB
VALUES ('','$company', '$location', '$pic')";
4。 HTML 输出
现在链接到输出表中的文件:
echo "<td>";
echo "<a href=" . $target_path . basename($row['userfile']) . ">
{$row['userfile']}</a>";
echo "</td>";
关于php - 如何使用php检索上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255213/