php - can only upload image files not audio - php

Solution
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$file = ($_FILES["file"]["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "audio/mpeg")
)
&& ($_FILES["file"]["size"] < 209715200)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}
Non Working Code
I have adapted a script I use to upload images into a folder to upload audio but audio files do not save - neither the path into the table or the audio into the folder.
How do I change this to check the sizes of all files and not just images? getfilesize gives a function does not exist error?
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
//function to check for valid image formats
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$file = getimagesize($_FILES["file"]["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($file["mime"] == "image/gif")
|| ($file["mime"] == "image/jpeg")
|| ($file["mime"] == "image/jpg")
|| ($file["mime"] == "image/pjpeg")
|| ($file["mime"] == "image/x-png")
|| ($file["mime"] == "image/png")
|| ($file["mime"] == "audio/mp3")
)
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}

try this one:
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
//function to check for valid image formats
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($file["file"]["type"] == "image/gif")
|| ($file["file"]["type"] == "image/jpeg")
|| ($file["file"]["type"] == "image/jpg")
|| ($file["file"]["type"] == "image/pjpeg")
|| ($file["file"]["type"] == "image/x-png")
|| ($file["file"]["type"] == "image/png")
|| ($file["file"]["type"] == "audio/mp3")
)
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}

Related

Fatal error: Call to undefined method PHPMailer::filenameToType()

I have this following piece of code of PHPMailer. The problem is, that the file uploads to the server successfully but the attachment is not sent in the mail.
Here is HTML:
<div class="image-upload"><label for="file-input">
<p>Upload File</p></label><input id="file-input" name="file" type="file"/>
</div>
And PHP is
if (isset($_POST['submit'])) {
if (!isset($_SESSION['mail_send'])) {
require'PHPMailer/class.phpmailer.php';
// $mail = new PHPMailer();
$fileName = '';
$filePath = '';
$type = '';
$name = $_POST['name'];
$phone = "Phone number : " . $_POST['phone'];
$email = $_POST['email'];
$country = "Country : " . $_POST['country'];
$dropdown = "Services:" . (isset($_POST['options']) ? implode(", ", `enter code here`array_map("htmlspecialchars", $_POST['options'])) : " not selected ") . "<br />"; // Multimple select box
$allowedExts = array("doc", "docx", "xls", "xlsx", "pdf", "png", "jpg", "jpeg", "gif");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/excel")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel") || ($_FILES["file"]["type"] == "application/x-excel") || ($_FILES["file"]["type"] == "application/x-msexcel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|| ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/gif")) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "<script>alert('Error: " . $_FILES["file"]["error"] . "')</script>";
} else {
$d = 'upload/';
$de = $d . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$fileName = $_FILES['file']['name'];
}
} else {
echo "<script>alert('Invalid file')</script>";
exit();
}
$detail = "Message : " . $_POST['details'];
$message = '<html>
<head>
<meta http-equiv=\"Content-Type: multipart/alternative; boundary=$boundary". "\r\n". />
<title>Verify Your Account</title>
</head>
<body>
<div>' . $phone . '</div>
<div>' . $country . '</div>
<div>' . $dropdown . '</div>
<div>' . $detail . '</div>
</body>
</html>';
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->IsHTML(true);
$mail->AddAddress('mushahid.hussain#app-desk.com');
$mail->Subject = 'Get a Quote';
$mail->MsgHtml($message);
$mail->AddAttachment("upload/$fileName");
$mail->Send();
}
}
I have also checked with
$mail->Body = $message;
$mail->AddAttachment("upload/$fileName");

PHP Overwrite Uploaded Image

I am trying some file upload code which I Googled.
Now Issue is when I try to upload image and if its already in folder then its over write and issues come. I Try Code From here and here as well but I face some error.
Here is my code. Can I do in this code that its upload file with some extra name which stop over write of existing file??
if(isset($_REQUEST['main']))
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
/*if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) {*/
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
if( move_uploaded_file($_FILES["file"]["tmp_name"], "../img/catalog/" . $_FILES["file"]["name"]) ){
$filepath = "img/catalog/" . $_FILES["file"]["name"];
}else{
echo $_FILES["file"]["name"]." unable to store";
}
}
/*}
} else {
echo "Invalid file";
}*/
}
$main = $_REQUEST['main'];
$sql="INSERT INTO image VALUES ('', '$filepath', '$main')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
}
You can check if the file exists using file_exists.
If it exist, add some extra characters to the file name. Then you can save it.
Here is a function that generates random characters :
function randomString($length) {
$str="";
$chars = "subinsblogabcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen($chars);
for($i = 0;$i < $length;$i++) {
$str .= $chars[rand(0,$size-1)];
}
return $str;
}
Replace the code :
if( move_uploaded_file($_FILES["file"]["tmp_name"], "../img/catalog/" . $_FILES["file"]["name"]) ){
$filepath = "img/catalog/" . $_FILES["file"]["name"];
}else{
echo $_FILES["file"]["name"]." unable to store";
}
with :
$newLocation = "../img/catalog/" . $_FILES["file"]["name"];
if(file_exists($newLocation)){
$newLocation .= randomString(10); // We append 10 new characters
}
if( move_uploaded_file($_FILES["file"]["tmp_name"], $newLocation) ){
$filepath = str_replace("../img", "img", $newLocation); // make File Path starting with img/
}else{
echo $_FILES["file"]["name"]." unable to store";
}
The above code will check if the file exists. If yes, then a string of 10 random characters is appended to the file name and stored in the destination folder.
You didnt mention your problem but I think you have missed a point in your
if else
part. write it like this :
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
$_FILES["file"]["name"] = $_FILES["file"]["name"].$your_new_number;
}
if( move_uploaded_file($_FILES["file"]["tmp_name"], "../img/catalog/" . $_FILES["file"]["name"]) ){
$filepath = "img/catalog/" . $_FILES["file"]["name"];
}else{
echo $_FILES["file"]["name"]." unable to store";
}
}

Error uploading file in php

Using Dreamweaver CS6,I have a problem in uploading files.
The ERROR is "INVALID FILE"
This is my code:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
Try Something like this:
if ($_FILES["file"]["error"]== 0) {
$upload_dir = dirname(__FILE__) . "/photos/";
if (file_exists($upload_dir)) {
if (is_writable($upload_dir)) {
$target = $upload_dir;
$newName=$_SESSION['theUsername'];
$target = $target . basename($newName);
$moved = move_uploaded_file($_FILES['file']['tmp_name'], "$target");
} else {
echo 'Upload directory is not writable<br>';
}
} else {
echo 'Upload directory does not exist.<br>';
}
}

how to upload doc and docx in php?

Am trying to upload document and docx in php through html input but am unable to upload doc and docx even i mentioned the mime type correctly.please help me out to upload doc and docx.i wrote my code above..thanks in advance..
<?php
$allowedExts = array(".doc", ".docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& 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"] / 200000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
2 fixes :
1 . change extension array
$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");
2 . to allow docx change the doubled line to
|| ($_FILES["file"]["type"]
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Here is The correct code
$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& 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"] / 200000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
You don't need the . for doc and docx
$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");
Try with this :
<?php
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Success";
}
}
$docx = $_FILES['file']['name'];
$docxTmp = $_FILES['file']['tmp_name'];
$docxSize = $_FILES['file']['size'];
$type = $_FILES["file"]["type"];
if ($docx) {
$file_dir = '../upload/';
$allowedExts = array("doc", "docx");
if (($type == "application/msword" || $type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && in_array($extension, $allowedExts)) {
move_uploaded_file($docxTmp, $file_dir . $docx);
}
}

Error in uploading image and text file to server using php

PHP FILE :
This is my form action file in php.
I am successful in uploading images but text files are producing 'Invalid File Error'.
What may be the error and how to resolve it?
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "text/txt"))
&& ($_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("C:/inetpub/wwwroot/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"C:/inetpub/wwwroot/" . $_FILES["file"]["name"]);
echo "Stored in: " . "C:/inetpub/wwwroot/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Thanks in advance.
I don't think your mime type for .txt files is correct. You could try txt/plain instead of txt/text in your IF statement.
Use the below code:
|| ($_FILES["file"]["type"] == "text/plain"
Instead of
|| ($_FILES["file"]["type"] == "text/txt"
Here is the list of mime-content-type.

Categories