[英]upload PDF file and send filename to database
OK tying to submit a small form, with an file input and a textfield 单击确定以提交一个带有文件输入和文本字段的小表格
on submit i want it to upload the (PDF FILES ONLY) and send the filename to my "resources" table. 在提交时,我希望它上传(仅PDF文件)并将文件名发送到我的“资源”表。
Resources table looks like this: 资源表如下所示:
id (AUTO_INC & Primary)
title (data from text field)
filename (from uploaded file, preferably with extension aswell)
dateadded (pulled from current date() )
here is the code i have so far; 这是我到目前为止的代码;
<form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>" method="post" id="myform" class="basic-form">
<?php
// IF FORM SUBMIT
if (isset($_POST['submit']))
{
$title = $_POST['title'];
$dateLog = date("y-m-d"); // DATE OF ADDITION
$timeLog = date("H:i:s", time() - 3600); // TIME OF ADDITION
// target directory & Extensions
$uploads_dir = '../resource_docs/';
$allowedExts = array("pdf", "doc");
//Loop file uploads
foreach ($_FILES["upload"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["upload"]["tmp_name"][$key];
$name = $_FILES["upload"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
// INSERT QUERY
$sql="INSERT INTO resources (filename, title, dateadded)
VALUES ('$name', '$title', '$dateLog')";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
}
?>
<input type="file" name="upload" />
<input name='title' type='text' value='' placeholder='Title Here'/>
<input type="submit" name="submit" value="Upload"/>
</form>
The form submits and puts everything in database except the filename and it shows at bottom of browser that its uploading but nothing in my target directory. 表单将文件名以外的所有内容提交并放入数据库,并在浏览器底部显示其正在上传,但在我的目标目录中什么也没有。
did i miss something obvious and can i get it to only accept .pdf files from my coding? 我是否错过了明显的东西,并且我只能接受编码中的.pdf文件吗?
Use echo statement to check whether the loop is executed or not. 使用echo语句检查循环是否执行。
Example: 例:
foreach ($_FILES["upload"]["error"] as $key => $error) {
echo "foreach working";
if ($error == UPLOAD_ERR_OK) {
echo "entering into if";
$tmp_name = $_FILES["upload"]["tmp_name"][$key];
$name = $_FILES["upload"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
您的文件路径可能不存在或无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.