I'm working on a website and this page has a form submission including a file upload, the form is going from html to php to mysql without an issue but the file upload doesn't seem to want to process.
HTML (inside a form posting to submit):
Document: <input type="file" name="fileToUpload" required></br></br>
PHP (without mysql start):
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($FileType != "doc" && $FileType != "docx" && $FileType != "pdf") {
echo "Sorry, only DOC, DOCX, & PDF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
The error either kicks at the filetype or just at the bottom. I couldn't find a php.ini anywhere on the server.
Thanks for your time, should I just make a php.ini?
Edit: Here's the print_r($_FILES);
How about something like this? This is untested, I just took out your filetype check and replaced it using an array.
<?php
$target_dir = "uploads/";
$allowed_types =array('doc','docx','pdf')
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$error = null;
// Get the file extension
$ext = pathinfo($target_file, PATHINFO_EXTENSION);
// Search the array for the allowed file type
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if (in_array($ext, $allowed_types, false) != true) {
$error = "Sorry, there was an error uploading your file.";
return $error; // or use exit;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>