我正在为我的宿舍开发一个wordpress插件,用于不同的管理目的。其中一个功能应该是将会议摘要上传到服务器/wordpress安装根目录中名为“refereter”的文件夹中。我有一个可以工作的实现,但它调用了另一个php页面来上传,我想将信息发布到同一个页面。它将数据插入数据库,但找不到该文件。我知道我应该使用mysqli,但托管提供商不支持它。
这就是代码(我知道它写得不太好,其中一些是我从宿舍的一位早期志愿者那里借来的;但起初我只想工作,后来我可以优化它):
<div class="wrap">
<!------------------------------>
<!-- Start side -->
<!------------------------------>
<?php if(!isset($_POST[\'var_uploaded-referat\'])){ ?>
<h1>Upload referat</h1>
Navngivningen af bestyrelses referaterne er ligegyldig, dog <strong>skal</strong> det være pdf-filer. <br>
Filerne bliver automatisk navngivet som "åååå-mm-dd Referat".pdf (uden " " selvfølgelig), når de bliver uploadet til serveren. <br>
Så hvis datoen for mødet var 5/6 2013, så bliver referat filens navn 2013-06-05 Referat.pdf. <br>
<!--Form data for møde dato-->
<form action="<?php echo htmlentities($_SERVER[\'REQUEST_URI\']); ?>" method="post" enctype="multipart/form-data">
<p><?php _e("Dag: " ); ?><select size="1" name="dag">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select></p>
<p><?php _e("Måned: " ); ?><select size="1" name="maaned">
<option value="01">Januar</option>
<option value="02">Februar</option>
<option value="03">Marts</option>
<option value="04">April</option>
<option value="05">Maj</option>
<option value="06">Juni</option>
<option value="07">Juli</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select></p>
<p><?php _e("År: " ); ?><select size="1" name="aar">
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select></p>
<!--Upload form til referat-->
<label for="file">Sti til referat:</label><br>
<input type="file" name="file" id="file"><br>
<input type="submit" name="var_uploaded-referat" value="Submit">
</form>
<!------------------------------>
<!-- Upload-script af referat -->
<!------------------------------>
<?php
} elseif (isset($_POST[\'var_uploaded-referat\'])) {
// Connects to the database
$db=mysql_connect("<server>", "<username>", "<password>")or die("cannot connect");
mysql_select_db("akdtu_dk", $db)or die("cannot select DB");
$date = "$_POST[aar]-$_POST[maaned]-$_POST[dag]";
$filnavn = $_FILES["file"]["name"];
$allowedExts = array("pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("../../referater/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
} else {
$directory = "../../referater/";
$newFileName = "$_POST[aar]-$_POST[maaned]-$_POST[dag] Referat.pdf";
$target = $directory . $newFileName;
move_uploaded_file($_FILES["file"]["tmp_name"],"$target");
echo \'<h1>Referat uploaded</h1>\';
echo "Stored in: " . "../../referater/" . $_FILES["file"]["name"];
echo \'<br>\';
echo "$_SERVER[REQUEST_URI]";
// SQL insertion/query
$sql="INSERT INTO ak_refbest (dato,filnavn)"."VALUES (\'$date\',\'$newFileName\')";
mysql_query($sql);
}
}
} else {
echo "Invalid file";
}
} else {
echo \'<h1>Fejl ved upload af referat</h1>\';
echo \'Prøv igen, hvis problemet fortsætter kontakt netgruppen på [email protected]\';
}
?>