So i want the user to only be able to upload docs or docx\'s. So first here\'s my html:
change
$extension!=".doc" || $extension!=".doc"
to
$extension!=".doc" || $extension!=".docx"
and use $_FILES['img']
instead of $_FILES['file']
because your file type is having img
name property.
Try to use $_FILES["img"] array to access uploaded file properties (your "Select a file" input called "img").
You may use "Location" http header to redirect back to source page.
The first question has already been answered so I won't answer it again.
2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?
It is normally better to show the success message on the redirected page but you can solve this two ways:
header("Location: old_page.php");
Also this is not the best way to get a file extension:
$extension = end(explode(".", $_FILES["file"]["name"]));
Try using pathinfo instead: http://php.net/manual/en/function.pathinfo.php
$extension = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION);
And your if
statement:
if ($extension!=".doc" || $extension!=".doc"
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
Even though this is a tiny usage of in_array
I still would recommend taking that out and using a more practical method of searching arrays: http://php.net/manual/en/function.array-search.php
So your if
would become something like:
if (($_FILES["file"]["size"] < 200000) && array_search($extension, $allowedExts)!==false) {
Your file field name is img
and you have used $_FILES["file"]
, but it should be $_FILES["img"]
.
Also you need to change your if condition
if (($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
There is no need of
$extension!=".doc" || $extension!=".doc" &&
You'll have to use $_FILES["img"]
instead of $_FILES["file"]
or rename your input name to "file".