I’m trying to develop a wordpress plugin for my dorm for different administration purposes. One of the functions should be to upload meeting summaries into a folder named “referater” in the root of the server/wordpress installation. I have had an implementation which worked, but it called another php page to upload, and I want to post the information to the same page. It inserts the data into the database, but the file is nowhere to be found. And I know I should be using mysqli but the hosting provider does not support it.
This is the code (and I know it is not so well-written, some of it I have borrowed from an earlier volunteer at the dorm; but at first I just want I to work and afterwards I can optimize it):
<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]';
}
?>
define( 'UPLOADS' )
, voting to close this though.