Using:
PHP5
MySQL 5.024
I am trying to add where i can upload a pdf which will in turn get displayed on client side.
Here is how I am going about it:
// START FORM PROCESSING
// only execute the form processing if the form has been submitted
if (isset($_POST['submit'])) {
// initialize an array to hold our errors
$errors = array();
// perform validations on the form data
$required_fields = array('menu_name', 'position', 'visible', 'content', 'file');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$fields_with_lengths = array('menu_name' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
// clean up the form data
$subject_id = mysql_prep($_GET['subj']);
$menu_name = trim(mysql_prep($_POST['menu_name']));
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$content = mysql_prep($_POST['content']);
$file = mysql_prep($_POST['file']);
if ((($_FILES["file"]["type"] == "image/pdf")) && ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
if (file_exists("../docs/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../docs/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../docs/" . $_FILES["file"]["name"];
}
}
}else{
echo "Invalid file";
}
// proceed only if no errors.
if (empty($errors)) {
$query = "INSERT INTO pages (
menu_name, position, visible, content, subject_id, filePath
) VALUES (
'{$menu_name}', {$position}, {$visible}, '{$content}', {$subject_id}, {"docs/" . $_FILES["file"]["name"]}
)";
if ($result = mysql_query($query, $connection)) {
// as is, $message will still be discarded on the redirect
$message = "The page was successfully created.";
// get the last id inserted over the current db connection
$new_page_id = mysql_insert_id();
redirect_to("content.php?page={$new_page_id}");
} else {
$message = "The page could not be created.";
$message .= "<br />" . mysql_error();
}
} else {
if (count($errors) == 1) {
$message = "There was 1 error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
// END FORM PROCESSING
}
<form action="new_page.php?subj=<?php echo $sel_subject['id']; ?>" method="post" enctype="multipart/form-data">
<?php $new_page = true; ?>
<?php include "page_form.php" ?>
<input type="submit" name="submit" value="Create Project" />
</form>
And the page_form.php is:
//above this is the rest of form stuff ^^^^^
<p>Project Files:<br />
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
</p>
I have the docs folder created and nothing is getting uploaded to it. It says it is on the echos but doesnt get uploaded. Do I have to enable anything in php.ini or some confg of sql? This is an example off w3schools file upload. Where does the file reside?
Thanks in advance MT