I'm able to upload images fine, but when when I change the types from image/jpg, image/gif to application/msword and application/pdf, it doesn't work. Here's my code. The exact same code works for images, but for uploading docs and pdf, it outputs "Invalid File." What's going on here? My file is only approx 30kb and is well under the file size limit here.
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ( ( ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "text/pdf") )
&& ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts))
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}
else
{
echo "Invalid file."
}
Please add the correct mime-types to your code - at least these ones:
.jpeg -> image/jpeg
.gif -> image/gif
.png -> image/png
A list of mime-types can be found here.
Furthermore, simplify the code's logic and report an error number to help the first level support track down problems:
$allowedExts = array(
"pdf",
"doc",
"docx"
);
$allowedMimeTypes = array(
'application/msword',
'text/pdf',
'image/gif',
'image/jpeg',
'image/png'
);
$extension = end(explode(".", $_FILES["file"]["name"]));
if ( 20000 < $_FILES["file"]["size"] ) {
die( 'Please provide a smaller file [E/1].' );
}
if ( ! ( in_array($extension, $allowedExts ) ) ) {
die('Please provide another file type [E/2].');
}
if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) )
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}
else
{
die('Please provide another file type [E/3].');
}
One of your conditions is failing. Check the value of mime-type for your files.
Try using application/pdf, not text/pdf. Refer to Proper MIME media type for PDF files
<?php
//create table
/*
--
-- Database: `mydb`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_user_data`
--
CREATE TABLE `tbl_user_data` (
`attachment_id` int(11) NOT NULL,
`attachment` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_user_data`
--
ALTER TABLE `tbl_user_data`
ADD PRIMARY KEY (`attachment_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tbl_user_data`
--
ALTER TABLE `tbl_user_data`
MODIFY `attachment_id` int(11) NOT NULL AUTO_INCREMENT;
*/
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$fileName=$_FILES["resume"]["name"];
$fileSize=$_FILES["resume"]["size"]/1024;
$fileType=$_FILES["resume"]["type"];
$fileTmpName=$_FILES["resume"]["tmp_name"];
$statusMsg = '';
$random=rand(1111,9999);
$newFileName=$random.$fileName;
//file upload path
$targetDir = "resumeUpload/";
$fileName = basename($_FILES["resume"]["name"]);
$targetFilePath = $targetDir . $newFileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(!empty($_FILES["resume"]["name"])) {
//allow certain file formats
//$allowTypes = array('jpg','png','jpeg','gif','pdf','docx','doc');
$allowTypes = array('pdf','docx','doc');
if(in_array($fileType, $allowTypes)){
//upload file to server
if(move_uploaded_file($_FILES["resume"]["tmp_name"], $targetFilePath)){
$statusMsg = "The file ".$fileName. " has been uploaded.";
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only DOC,DOCX, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
//display status message
echo $statusMsg;
$sql="INSERT INTO `tbl_user_data` (`attachment_id`, `attachment`) VALUES
('NULL', '$newFileName')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "upload success";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
<form id="frm_upload" action="" method="post" enctype="multipart/form-data">
Upload Resume:<input type="file" name="resume" id="resume">
<button type="submit" name="submit">Apply Now</button>
</form>
//output sample[![check here for sample output][1]][1]
You can use
$_FILES['filename']['error'];
If any type of error occurs then it returns 'error' else 1,2,3,4 or 1 if done
1 : if file size is over limit .... You can find other options by googling
For application/msword and application/vnd.ms-excel, when I deleted the size restriction:
($_FILES["file"]["size"] < 20000)
...it worked ok.
$folder = "Resume/";
$temp = explode(".", $_FILES["uploaded"]["name"]);
$newfilename = round(microtime(true)).'.'. end($temp);
$db_path ="$folder".$newfilename ;
//remove the .
$listtype = array(
'.doc'=>'application/msword',
'.docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.rtf'=>'application/rtf',
'.pdf'=>'application/pdf');
if ( is_uploaded_file( $_FILES['uploaded']['tmp_name'] ) )
{
if($key = array_search($_FILES['uploaded']['type'],$listtype))
{if (move_uploaded_file($_FILES['uploaded'] ['tmp_name'],"$folder".$newfilename))
{
include('connection.php');
$sql ="INSERT INTO tb_upload
(filePath) VALUES ('$db_path')";
}
}
else
{
echo "File Type Should Be .Docx or .Pdf or .Rtf Or .Doc";
}
Source: Stackoverflow.com