好的。自 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 我应该能够在单击链接时查看/下载它。
蚂蚁的想法...
关于您可以更改哪些内容以使其正常工作的一些建议。
1. 上传表格
您的表单标签是什么样的?不要忘记包括enctype
参数如下:
<form type="post" action="" enctype="multipart/form-data">
...
</form>
2. 消毒
$company = mysql_real_escape_string($_POST['company']);
$location = mysql_real_escape_string($_POST['location']);
$pic = mysql_real_escape_string($_FILES['userfile']['name']);
上述几行是帮助防止查询遭受 SQL 注入攻击的第一步。
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>";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)