PHP File Upload Not Submitting - php

I'm having an issue with a file upload where it's not uploading. When uploading a 7mb video the $_FILES['video']['tmp_name'] is empty and when I upload a 15mb file the form doesn't actually "submit", really just refreshes.
Here is my code to handle the submission:
if(isset($_POST['submit'])){
$blah = "".$_FILES['video']['size']."";
var_dump($blah);
if( empty($_FILES['video']) && empty($_POST['create_video']) ){
echo "<div class='alert alert-danger' role='alert'><center><strong>Missing Fields:</strong> Please choose a video or request the ##### team create a video.</center></div>";
} else {
if( empty($_FILES['video']['name']) && $_POST['create_video'] == "true" ){
$_SESSION['create_video'] = protect($_POST['create_video']);
?>
<script type="text/javascript">
window.location = "NEXT_PAGE";
</script>
<?
exit();
}else{
//if all were filled in continue
$allowedExts = array("mp4", "MP4", "m4a");
$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
if ( ($_FILES["video"]["size"] <= 15728640) && (in_array($extension, $allowedExts)) ) {
if ($_FILES["video"]["error"] > 0){
echo "Return Code: " . $_FILES["video"]["error"] . "<br />";
}else{
//Get the height and width of our video
//$getID3 = new getID3;
//$file = $getID3->analyze($_FILES["video"]["tmp_name"]);
//$width =$file['video']['resolution_x'];
//$height = $file['video']['resolution_y'];
$img = getimagesize($_FILES['video']['tmp_name']);
$width = $img[0];
$height = $img[1];
var_dump($width); var_dump($height);
if( ($height < 719) || ($width < 1279)){
echo "<div class='alert alert-danger' role='alert'><center><strong>Invalid file dimensions</strong> Please ensure your image is the correct size.</center></div>";
} else {
$ext = findexts ($_FILES["video"]["name"]);
$ran = rand ();
$file_name = $_FILES["video"]["name"] = "".$_SESSION['uid'] ."".$ran.".".$ext."";
if (file_exists("uploads/video_ads/".$_SESSION['uid']."_" . $_FILES["video"]["name"])){
echo $_FILES["video"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["video"]["tmp_name"],
"uploads/video_ads/".$_SESSION['uid']."_" . $_FILES["video"]["name"]);
//Save the link of our ad
$_SESSION['video'] = "####/uploads/video_ads/".$_SESSION['uid']."_" . $_FILES["video"]["name"]."";
$_SESSION['create_video'] = protect($_POST['create_video']);
?>
<script type="text/javascript">
window.location = "NEXT_PAGE";
</script>
<?
exit();
}
}
}
} else {
echo "<div class='alert alert-danger' role='alert'><center><strong>Invalid file type</strong> Please upload a video in MP4 format.</center></div>";
}
}
}
}
Here is my actual form:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin" role="form" enctype="multipart/form-data">
<br />
<input type="file" name="video" id="video" /><br />
<br />
<div class="row">
<div class="col-md-12">
<strong class="ad-header"
style="font-size: 150%; font-weight: bold;">Quick Tips:</strong>
</div>
</div>
<hr>
<h3 class="ad-sub-header" style="color: #559fd3; font-size: 150%;">Need
help to create engaging artwork for your brand?</h3>
<strong class="ad-header" style="font-size: 100%;">####
has the creative team to get it done for you</strong><br /> Only ##/hr -
3 revisions - Artwork is yours to keep. <br />
<br /> <input type="checkbox" name="create_video" value="true" />
I don't have an ad. Please create one. <br />
<br /> <input class="btn btn-lg btn-primary btn-block"
type="submit" name="submit" value="Continue To Step 5" />
</form>

The odds are your server does not accept uploads greater than 2M in size. You need to check phpinfo() (or php.ini if you have access to it) to see what your current limit is. If it is only 2M, or smaller than your upload size, you need to edit it to allow for bigger uploads. If you're on shared hosting you may be out of luck.

Try adjusting your php.ini with this settings:
php_value memory_limit 96M
php_value post_max_size 96M
php_value upload_max_filesize 96M
and ensure that the file_uploads setting is in On

For anyone who has this issue. I had to increase my post_max_size which was defaultly set to 8M.

Related

Issue on Uploading Multiple Images to Server By One Click

I am trying to upload multiple images to server by one click. I am not getting any error on JavaScript console but no images uploaded to the server. Can you please let me know what I am doing wrong?
Here is my HTML markup
<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-6 col-md-3 text-center">
<a href="#" class="thumbnail ">
<div class="img-box">
<input type="file" class="file" name="image" /> <img class="img img-responsive" />
</div>
</a>
</div>
<div class="col-xs-6 col-md-3 text-center">
<a href="#" class="thumbnail ">
<div class="img-box">
<input type="file" class="file" name="image" />
<img class="img img-responsive" />
</div>
</a>
</div>
</div>
<input type="submit" value="Upload" class="submit" />
</form>
js file as:
$("#uploadimage").on('submit', (function(e) {
e.preventDefault();
$.ajax({
url: "loader.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
}
});
}));
and PHP (loader.php) as
<?php
if (isset($_FILES["file"]["type"])) {
$validextensions = array(
"jpeg",
"jpg",
"png"
);
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")) && ($_FILES["file"]["size"] < 100000) //Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
} else {
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/" . $_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>";
}
}
} else {
echo "<span id='invalid'>***Invalid file Size or Type***<span>";
}
}
?>\
Please refer this .
when you send multiple files, php accept it as an array of files but in your code you are accepting them as a single file.
Also, try to give array of name as
<input type="file" name="images[]" id="images" multiple >
In a form, the name is the identifier of the input. So you should do $_FILES["image"] to access your file.
When a form have multiple inputs with the same name, it must be name with [] at the end (at least in php). So rename your image[] in the html source.
first, your file array you are taking in controller is wrong,
$temporary = explode(".", $_FILES["file"]["name"]);
you need to take image instead of file because in form file input name you used is image so,
$temporary = explode(".", $_FILES["image"]["name"]);
then after in your html you can take like this as thisisme_22 suggests
<input type="file" name="images[]" id="images" multiple >
then after in you action php file you can take count of the files and the using for or foreach you can get single file and upload it, like this
$count = count($FILES['image']['name']);
for($i = 0 ; $i < $count ; $i++){
$file = $FILES['image']['name'][$i];
}
I hope it helps.
check this code and change it as you need
$arr = array();
$arr = $_FILES['image']['name'];
for($i = 0; $i < count($arr) ; $i++)
{
$file_name = $_FILES['image']['name'][$i];
$file_size = $_FILES['image']['size'][$i];
$file_tmp = $_FILES['image']['tmp_name'][$i];
$file_type = $_FILES['image']['type'][$i];
$responce = move_uploaded_file($file_tmp, "orders/".$file_name);
}

Can't post data along with large file size upload PHP

I am trying to upload file along with some data in form. It is working perfectly fine for small files. But, when I am trying to submit data along with large files, it is not capturing data.
This is Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<div class="form-group">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<input type="text" class="form-control" name="title" required placeholder="Title">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<input type="file" class="form-control" name="report" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<center>
<input type="submit" name="upload_report" value="Upload" class="btn btn-primary btn_font">
</center>
</div>
</div>
</form>
PHP Code:
if(isset($_POST["upload_report"])) {
echo $_POST['title']; // giving title index undefined
$max_report_id += 1;
$target_dir = "Reports/";
$file_title = mysqli_real_escape_string($connection,trim($_POST['title']));
$file_name = mysqli_real_escape_string($connection,trim($_FILES['report']["name"]));
//$file_size = mysqli_real_escape_string($connection,trim($_FILES['report']["size"]));
$target_file = $target_dir . basename($file_name); //basename gets filename from path
$uploadOk = 1;
$file_type = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = $target_dir . $max_report_id . "." . $file_type;
// Check if file already exists
if (file_exists($target_file)) {
//echo "Sorry, file already exists.";
$uploadOk = 0;
}
/* Check file size
if ($file_size > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
*/
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$status_fail = true;
$fail_message = "Sorry, there was an error uploading your file.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["report"]["tmp_name"], $target_file)) {
$status_success = true;
$success_message = "The file ". $file_title. " has been uploaded.";
$query = "insert into reports(title,fname,path,platform,added_by,added_on) values ('$file_title','$file_name','$target_file','$platform','$user_id',now())";
$result = mysqli_query($connection, $query) or die($query);
}
else {
$status_fail = true;
$fail_message = "Sorry, there was an error uploading your file.";
}
}
}
I have enough updated max_post_size, memory_limit, max_upload_size in php.ini file.
The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size. Depending on your host, changing these two PHP variables can be done in a number of places with the most likely being php.ini or .htaccess (depending on your hosting situation).
You need to set the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M (ex. 40MB and you can change more size)
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M (ex. 40MB and you can change more size)
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.

add limit php upload image

I use a php ajax script for uploading image in my site and it's don't has limit for upload image.
I want a user can upload only for example 3 image. how can add this limit in my code?
This is my php code for uploading image:
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$vpb_upload_image_directory = "uploads/";
$vpb_allowed_extensions = array("gif", "jpg", "jpeg", "png");
foreach($_FILES as $file)
{
/* Variables Declaration and Assignments */
$vpb_image_filename = basename($file['name']);
$vpb_image_tmp_name = $file['tmp_name'];;
$vpb_file_extensions = pathinfo(strtolower($vpb_image_filename), PATHINFO_EXTENSION);
//New file name
$random_name_generated = time().rand(1234,9876).'.'.$vpb_file_extensions;
if($vpb_image_filename == "")
{
//Browse for a photo that you wish to use
}
else
{
if (in_array($vpb_file_extensions, $vpb_allowed_extensions))
{
if(move_uploaded_file($vpb_image_tmp_name, $vpb_upload_image_directory.$random_name_generated))
{
//Display Uploaded Files
$image .= '
<div class="vpb_wrapper" style="padding:10px;">
<img src="'.$vpb_upload_image_directory.$random_name_generated.'" class="vpb_image_style" />
</div>';
//$image .= '<div class="vpb_wrapper" style="padding:10px; text-decoration:none;">'.$vpb_image_filename.' uploaded</div>';
}
}
else
{
// Do not upload files which are not in the allowed file array
}
}
}
//Display the files
if($image != "") echo $image;
}
?>
In javascript, you can do something like this,
$(document).ready(function(){
$('#photoUploader').click(function(){
$('#photoUI').toggle('slow');
});
var ctr = 1;
$('#add').click(function(){
if(ctr < 3)
{
$('#ulPhoto').append("<li><input type='file' name='file[]' onchange='loadPreview(this,"+ctr+")' id='photo"+ctr+"' /><br /><img id='img"+ctr+"' src='#' alt='' style='width:100px' /><input type='text' autocomplete='off' name='caption[]' id='caption"+ctr+"' placeholder='ID Number' /></li>");
ctr++;
}
else
{
alert('Only 3 Images Allowed at a time');
}
});
});
HTML
<div id="photoUI" style="display:none; height: auto">
<ul id="ulPhoto">
<li>
<input type="file" name="file[]" id="photo0" />
<br />
<img id="img0" src="#" alt="" style="width:100px" />
<input type="text" autocomplete="off" name="caption[]" onchange="loadPreview(this,0)" id="caption0" placeholder="Default ID Number" readonly disabled />
</li>
</ul>
<span id="add" style="cursor:pointer">Add More...</span>
</div>

I can't upload pdf files in php

Now i am trying to upload pdf files by the form in php but when i upload it, it doesn't work because function is_uploaded_file($_FILES['file']['tmp_name']) return false
How can i solve it please ??
I am using localhost and I verify php.ini
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "E:/wamp/tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 50M
This is my code :
Class
<?php
require '../includes/master.inc.php';
// Kick-out users who aren't logged in as an admin
$auth->requireAdmin('../login_admin_cp.php');
if (isset($_POST['btnsubmit'])) {
$error->blank($_POST['field_name'], "field_name");
if ($error->ok()) {
$field_name = $_POST['field_name'];
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
// exit;
$mypath = "upload/";
$file_name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$n = $mypath . $file_name;
$t = $_FILES['file']['size'];
$filesize = round($t / 1024) . " KB";
$extintion = strchr($n, ".");
$extintion = strtolower($extintion);
$file_extintion_allow = array(".pdf");
if ($n == "") {
echo "Error in upload";
} elseif (!in_array($extintion, $file_extintion_allow)) {
echo "من فضلك ادخل المادة بطريقة صحيحة";
exit;
} else {
//rename the file when upload by query name
$quaryname = rand(11111, 99999);
$rename = "../" . $mypath . $quaryname . $extintion;
$URL = substr($rename, 10, strlen($rename));
move_uploaded_file($tmp, $rename);
if (!empty($_POST['hide'])) {
$hide = $_POST['hide'];
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','$hide')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
} else {
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','0')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
}
}
} else {
exit;
}
} else {
$sess->set_flashdata('error_msg', 'حقل المادة فارغ');
}
}
// View
include('views/header.php');
include('views/field_form_new.php');
include('views/footer.php');
?>
Form
<!-- Form elements -->
<div class="grid_9">
<div class="module">
<h2><span> رفع المادة</span></h2>
<div class="module-body">
<?php echo $error;?>
<form action="field_new.php" method="post" name="myform" enctype="multipart/form-data">
<p>
<label> اسم المادة</label>
<input type="text" class="input-medium" name="field_name" value="" />
</p>
<p style="font-size:14px"> رفع المادة </p>
<p>
<label> الرابط</label>
<input type="text" class="input-medium" name="link" value="<?php $link; ?>" />
</p>
أو<br/> <br/>
<p>
<input type="hidden" class="input-medium" name="MAX_FILE_SIZE" value="1000000">
<label>رفع الملف </label>
<input type="file" class="input-medium" name="file" />
</p>
<p>
<input type="checkbox" name="hide" value="1" />تفعيل المادة
</p>
<fieldset>
<input class="submit-green" type="submit" name="btnsubmit" value="اضافة" />
</fieldset>
</form>
</div> <!-- End .module-body -->
</div> <!-- End .module -->
<div style="clear:both;"></div>
</div> <!-- End .grid_6 -->
Maybe the file is to big or the destination directory is not writeable for php/apache.
Check the folder permission "E:/wamp/tmp" and try to upload a smaller file.
If a smaller file works, check these values in the php.ini file:
post_max_size
max_input_time
memory_limit

Photo upload not uploading files bigger than 2MB

I have a php file upload in place in which the forms target is an iframe.
It uploads files less than 2mb fine. But anything larger it will not upload. I cant see any reason behind this in the code.
<div id="upload_wrapper">
<img src="../images/logo.png" alt="logo5" width="" height="" style="padding:0px;" />
<h3>Profile picture upload</h3>
<form id="upload" name="upload" enctype="multipart/form-data" method="post" action="index.php" target="upload_target">
<input name="folder" type="hidden" value="<?php echo $folder ?>" />
<input name="filename" type="hidden" value="<?php echo $filename ?>" />
<input name="uploaded_file" type="file" size="5120" id="uploaded_file" />
<input id="sent" name="sent" type="submit" value="Upload" />
</form>
</div>
<div id="loading" style="background:url(ajax-loader.gif) no-repeat left; height:50px; width:370px; display:none;">
<p style="margin-left:40px; padding-top:15px;">Uploading File... Please wait</p>
</div>
<div id="image_wrapper" style="display:none;"><p id="preview"></p></div>
<iframe id="upload_target" name="upload_target" style="width:10px; height:10px; display:none"></iframe>
</div>
And heres the php upload code
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targetFolder = $_POST['folder'] . "/";
$filename2 = $_POST['filename'];
if (!is_dir($targetFolder))
{
mkdir($targetFolder, 0777);
}
$tempFile = $_FILES['uploaded_file']['tmp_name'];
$targetPath = dirname(__FILE__) . '/' . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['uploaded_file']['name'];
// Validate the file type
$fileTypes = array('jpg'); // File extensions
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if (in_array($fileParts['extension'],$fileTypes))
{
$uploadfile = $targetFolder. basename($filename2 .".".$fileParts['extension']);
move_uploaded_file($tempFile,$uploadfile);
$fileName = $uploadfile;
}
echo "<div id='filename'>$fileName</div>";
}
Maybe changing your php server's configuration would help (example for 10MB):
ini_set('upload_max_filesize', '10M');
make sure your php.ini configuration support large files. upload_max_filesize and post_max_size also you can change the configuration temporary using ini_set before you handle the upload process.
try this may be it would work put it in your code and then try
define ("MAX_SIZE","1000"); // put your max size value
$size=filesize($_FILES['uploaded_file']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
or else you have to edit in php.ini found in document root/php/php.ini
You can change your server setting. Look for the php.ini file and search "upload_max_filesize"

Categories