Uploading a video caus $_POST/$_FILES to be empty - php

I am trying to upload the image and video in my project, but when I tried to upload video it returns an empty array for $_POST and $_FILES both.
If I try to upload an image, the same code is working fine. The code I have used is below:
extract($_POST);
$upload_dir = wp_upload_dir();
$target_dir = $upload_dir['basedir'].'/mediauploads/';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "mp4" && $imageFileType != "wmv" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg" && $imageFileType != "jpg")
{
echo "File Format Not Suppoted";
} else {
$video_path=$_FILES['fileToUpload']['name'];
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
echo "uploaded ";
}
}
Upload video form:
<form method="post" enctype="multipart/form-data">
<table border="1" style="padding:10px">
<tr>
<Td>Upload Video</td>
</tr>
<Tr>
<td><input type="file" name="fileToUpload"/></td>
</tr>
<tr>
<td><input type="submit" value="Uplaod Video" name="upd"/></td>
</tr>
</table>
</form>

if (isset($_FILES["stPic"]["name"]) && $_FILES["stPic"]["tmp_name"] != ""){
$fileName = $_FILES["stPic"]["name"];
$fileTmpLoc = $_FILES["stPic"]["tmp_name"];
$fileType = $_FILES["stPic"]["type"];
$fileSize = $_FILES["stPic"]["size"];
$fileErrorMsg = $_FILES["stPic"]["error"];
if (!preg_match("/.(gif|jpg|png|mp4|3gp|wmw|avi|mkv)$/i", $fileName) ) {
echo "File Format Not Suppoted";
}else{
$moveResult = move_uploaded_file($fileTmpLoc, "video/$fileName");
if ($moveResult != true) {
echo 'file not upload';
exit();
}
else{
//You run sql query
echo 'Uploaded';
}
}
}

Related

Uploaded image using php doesn't fully work

The problem is this one:
I have a code that uploads a picture to phpmyadmin, the code... doesn't fully work for some reason, it doesn't upload the image fully and the image ends up having a very small size, and it won't show up either. If I upload them directly to the database, they can be shown, but the ones uploaded using the code below have that problem.
This is the code I have:
Code for the button:
<div id="content">
<form method="POST" action =""/>
<table>
<tr>
<td>
Upload a picture
</td>
<td>
<input type="file" name="image"/>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Registrarme"/> <input type="reset"/>
</form>
<?php
if(isset($_POST['submit'])){
require("iniciar.php");
}
?>
</div>
Code for the uploading: (iniciar.php)
<?php
$image = $_POST['image'];
$reqlen = strlen($image);
if($reqlen > 0){
$conn = mysqli_connect('localhost','root','','image');
$sql = "INSERT INTO `images` VALUES ('', '$image', '')";
mysqli_query($conn, $sql);
echo 'Upload successful';
}else{
echo 'Please insert an image';
}
?>
This is the structure of the table:
The table I have
you're assigning like input data instead of
$_FILES["image"]["name"];
//remove thisline
$image=$_POST['evidence'];
// you have to use it like this
$image= $_FILES["image"]["name"];
you have to assign image like this
$target_file = $target_dir . '/' . basename($_FILES["image"]["name"]);
$image=$target_file;
now evidence hold address of image so dir. of image is now on evidence too.now it will be uploaded to data base you aren't passing data. that's the mistake. any questions comment .if it solves your problem mark it as solved :)
for reference ,this is the php script used to upload profile picture,
<?php
if(isset($_POST["submit"]))
{
$target_dir = "profilepic/";
$targetfile=$target_dir.basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($targetfile,PATHINFO_EXTENSION);
if($imageFileType != "jpg" &&$imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "jpeg" && $imageFileType != "JPEG" && $imageFileType != "gif" && $imageFileType != "GIF" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "<small>Sorry, your file was not uploaded.</small>";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetfile))
{
echo "<small>upload successful</small>";
$imgdir="UPDATE user SET profilepic='$targetfile' WHERE username='$uname';";
$con->query($imgdir);
header("location:profile.php");
}
else
echo "not uploaded";
}
}
?>
make changes according to your data base & operations.

Video Upload with php with mysql

My uploaded file not to move the folder but path save in mysql. what have the problem for my code how to save the video in folder, ihave already create the folder name which is test_uploads. what is exexct method to get the video in folder
<?php
error_reporting(1);
$con=mysql_connect("localhost","root","");
mysql_select_db("ngo",$con);
extract($_POST);
$target_dir = "test_upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "wmv")
{
echo "File Format Not Suppoted";
}
else
{
$video_path=$_FILES['fileToUpload']['name'];
mysql_query("insert into video(video_name) values('$video_path')");
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
echo "uploaded ";
}
}
//display all uploaded video
if($disp)
{
$query=mysql_query("select * from video");
while($all_video=mysql_fetch_array($query))
{
?>
<video width="300" height="200" controls>
<source src="test_upload/<?php echo $all_video['video_name']; ?>" type="video/mp4">
</video>
<?php } } ?>
<form method="post" enctype="multipart/form-data">
<table border="1" style="padding:10px">
<tr>
<Td>Upload Video</td></tr>
<Tr><td><input type="file" name="fileToUpload"/></td></tr>
<tr><td>
<input type="submit" value="Uplaod Video" name="upd"/>
<input type="submit" value="Display Video" name="disp"/>
</td></tr>
</table>
</form>
Debug your code to find the error. Something like this:
$result = move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
if($result) {
mysql_query("insert into video(video_name) values('$video_path')");
echo "uploaded ";
} else {
echo $_FILES["file"]["error"];
}

Need help on Uploading Multiple Image with compression

I have these code below to upload single image with compression. but right now I want to upload multiple image. Can someone help with with upload multiple image using the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Compress and Check</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept="image/*" />
<input type="submit" name="btnsubmit" value="submin" id="submit" />
</form>
</body>
</html>
<?php
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
elseif ($info['mime'] == 'image/jpg') $image = imagecreatefrompng($source_url);
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
if(isset($_POST['btnsubmit'])){
if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != ""){
$filename = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_error = $_FILES['file']['error'];
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$uploadOk = 1;
if(file_exists($target_file)){ exist or not
echo "<script>alert('sorry, file already exists.');</script>";
$uploadOk = 0;
}else if ($file_size > 500000) {
echo "<script>alert('sorry, your file is too large.');</script>";
$uploadOk = 0;
}else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "<script>alert('sorry, only JPG, JPEG, PNG & GIF files are allowed.');</script>";
$uploadOk = 0;
}else if($file_error > 0){
echo "<script>alert('Your file is corrupted!');</script>";
$uploadOk = 0;
}
if($uploadOk == 0){
echo "<script>alert('sorry, your file was not uploaded!');</script>";
}else{
$rand = md5(sha1(rand(10000, 10000000)));
$url = $target_dir . $rand . '.jpg';
$filename = compress_image($_FILES["file"]["tmp_name"], $url, 80);
}
}else{
echo "<script>alert('No image to upload!');</script>";
}
}
?>
Thank you in advance.....
Change a bit for the input "file" element:
<input type="file" multiple name="file[]" id="file" accept="image/*" />
Its ok, now you can use $_FILES["file"]["name"][0] to get the name of 1st file, or _FILES["file"]["tmp_name"][1] to get the tmp_name of 2nd file, and so on.
I hope you understand, i cannot explain more because i am using phone...

why is the file not being sent in POST variable even though I have enctype="multipart/form-data" in the form

I am trying to update an image from something that was already uploaded but the input of type file is not being sent and I can't seem to find out why. The variable fileToUpload is not being sent.
main.php
<form class="form-horizontal" action="modData.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2 col-lg-offset-2">Image:</label>
<div class="col-xs-4">
<?php
echo '<img src="'.$img.'" alt="img" height="350" width="584">';
?>
<input type="file" class="form-control" id="fileToUpload" name="fileToUpload">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 col-lg-offset-2">Description:</label>
<div class="col-xs-8">
<input name="des" id="des" value="<?php echo $des?>">
<input type='hidden' name="des2" id="des2" value="<?php echo $des?>">
</div>
</div>
<button type="submit" class="btn btn-success pull-right" name="save" value="save" id="save">Save</button>
</form>
modData.php
if(isset($_POST['fileToUpload'])){
echo 'file sent!!!';
$target_dir = "photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo $target_file;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG"
&& $imageFileType != "GIF" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF 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 {
$des = $_POST['des'];
$des2 = $_POST['des2'];
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$res = mysql_query("UPDATE `image` SET `des`='$des', `img`='".$target_file."' WHERE des='$des2'");
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
error_reporting (0);
}
}
}
else{
echo 'Did not send and only doing this';
$des = $_POST['des'];
$des2 = $_POST['des2'];
$res = mysql_query("UPDATE `image` SET `des`='$des' WHERE des='$des2'");
}
The file is sent, but you're checking an undefined variable.
Change
if(isset($_POST['fileToUpload'])){
to
if(isset($_FILES['fileToUpload'])){

How to check all type of file and display the validation message while uploading file using PHP

I need to upload the below type of file using PHP.
1-.png,
2-.jpeg
3-.jpg
4-.pdf,
5-.ppt
6-.docx and excel sheet.
I am explaining my code below.
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<div style="color:#F00; text-align:center;"> <?php echo $error; ?></div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Upload Document :</span>
<input type="file" class="filestyle form-control" data-size="lg" name="uploadme" id="bannerimage" onChange="javascript:displayImage(event);">
</div>
</form>
complain.php:
$target_dir = "upload/";
$target_file = $target_dir . basename($imageName);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["uploadme"]["tmp_name"]);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Sorry, only JPG, JPEG, PNG files are allowed.";
$uploadOk = 0;
}
Here i can only up to image type files. Here i need all files which is given above should check and if any error is coming it will display inside the form.Please help me to resolve this issue.
You can try this.
<?php
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['uploadme']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'error';
}
?>

Categories