PHP file upload error tmp_name is empty - php

I have this problem on my file upload. I try to upload my PDF file while checking on validation the TMP_NAME is empty and when I check on $_FILES['document_attach']['error'] the value is 1 so meaning there's an error.
But when I try to upload other PDF file it's successfully uploaded. Why is other PDF file not?
HTML
<form action="actions/upload_internal_audit.php" method="post" enctype="multipart/form-data">
<label>Title</label>
<span><input type="text" name="title" class="form-control" placeholder="Document Title"></span>
<label>File</label>
<span><input type="file" name="document_attach"></span><br>
<span><input type="submit" name="submit" value="Upload" class="btn btn-primary"></span>
</form>
PHP
if(isset($_POST['submit'])){
$title = $_POST['title'];
$filename = $_FILES['document_attach']['name'];
$target_dir = "../eqms_files/";
$maxSize = 5000000;
if(!empty($title)){
if(is_uploaded_file($_FILES['document_attach']['tmp_name'])){
if ($_FILES['document_attach']['size'] > $maxSize) {
echo "File must be: ' . $maxSize . '";
} else {
$result = move_uploaded_file($_FILES['document_attach']['tmp_name'], $target_dir . $filename);
mysqli_query($con, "INSERT into internal_audit (id, title, file) VALUES ('', '".$title."', '".$filename."')");
echo "Successfully Uploaded";
}
}else
echo "Error Uploading try again later";
}else
echo "Document Title is empty";
}

I just check the max size in phpinfo();
Then check if php.ini is loaded
$inipath = php_ini_loaded_file();
if ($inipath) {
echo 'Loaded php.ini: ' . $inipath;
} else {
echo 'A php.ini file is not loaded';
}
Then Change the upload_max_filesize=2M to 8M
; Maximum allowed size for uploaded files.
upload_max_filesize = 8M
; Must be greater than or equal to upload_max_filesize
post_max_size = 8M
Finally reset your Apache Server to apply the changes
service apache2 restart

var_dump($_FILES['file_flag']['tmp_name']); // file_flag file key
will return empty string
array (size=1)
'course_video' =>
array (size=5)
'name' => string 'fasdfasdfasdfsd.mp4' (length=19)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0) <===== *** this point
'error' => int 1
'size' => int 0
This happen because WAMP server not accepting this much size to uploaded on server.
to avoid this we need to change php.ini file.
upload_max_filesize=100M (as per your need)
post_max_size = 100M (as per your need)
finally restart server

another option is to add a separate config file with upload limits.
i created uploads.ini:
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60
and placed it in conf.d directory
in my case using Docker: /usr/local/etc/php/conf.d/uploads.ini
that way i could keep my production php.ini and add this just for uploads control

Related

Input File Maximum Size

I'm new at Laravel. How can I limit the size of input file to 5mb?
This is my controller :
public function add_project_activity(Request $request){
$id_rotation = $request->id_rotation;
$input_activity = $request->activity_name;
$input_detail = $request->detail_activity;
$input_file = $request->file;
$nik = Sentinel::getUser()->nik;
if (!empty($request->file) && $request->hasFile('file')) {
$new_id = self::check_id();
$filename = $input_file->getClientOriginalName();
$new_filename = "evidence_" . $new_id . "-" . $filename;
$upload_file = $input_file->storeAs('public/accelerate/'.$nik.'/',$new_filename);
$submit_data = AccelerateProjectActivity::create([
'status' => 'draft',
'activity_name' => $input_activity,
'detail_activity' => $input_detail,
'evidence' => $new_filename,
'month' => $request->month,
'id_rotation' => $id_rotation,
]);
return redirect()->back()->with('success', 'your message,here');
} elseif(empty($request->file)){
$submit_data = AccelerateProjectActivity::create([
'status' => 'draft',
'activity_name' => $input_activity,
'detail_activity' => $input_detail,
'month' => $request->month,
'id_rotation' => $id_rotation,
]);
}
return redirect()->back();
}
This is my view of input :
<div class="form-group">
<label>Evidence Activity</label>
<input type="file" name="file" class="form form-control" accept="application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/pdf">
</div>
Thank you, I'm confused at this, how to place the validator in my controller.
Hope you're answer my question. Greetings.
There is a more simple setup . You can edit it in htaccess. Or you can set upload limit in php.ini file. Depending on which system you are working you can have different setting. But you should take a look at the configurations.
You could try setting the upload_max_size on the fly in the add_project_activity() controller method.
public function add_project_activity(Request $request) {
#ini_set('upload_max_size' , '5M');
...
https://www.php.net/manual/en/function.ini-set.php
OR edit php.ini file
; Maximum allowed size for uploaded files.
upload_max_filesize = 5M
; Must be greater than or equal to upload_max_filesize
post_max_size = 5M
OR edit your .htaccess file
php_value upload_max_filesize 5M
php_value post_max_size 5M
But these last 2 options will be global, for all your file upload situations.

Failed multi upload when images size goes height

I'm using php to upload my pics but when my selected images size goes to height
my upload failed. On the page no error show for this happend This is like refresh page but my selected images size is lower than 3 MG it Works Well
whats my problems.
PLEASE HELP ME.
$output_dir = "../PostImage/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$RandomNum = time();
$ImagePostName=jdate("HisYmd",$timestamp)."".$RandomNum."".convert_filename_to_md5($_POST['title'])."".$_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $ImagePostName);
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$RandomNum = time();
$ImagePostName=jdate("HisYmd",$timestamp)."".$RandomNum."".convert_filename_to_md5($_POST['title'])."".$_FILES["myfile"]["name"][$i];
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$ImagePostName );
}
}
}
}
<form name="form1" method="post" action="index.php" enctype="multipart/form-data">
<input class="form-control input-lg m-bot15" name="myfile[]" id="myfile" multiple="multiple" type="file"/>
<input type="submit" value="upload" placeholder=""/>
</form>
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
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.

Extra Large size file upload Doesn't working using PHP/HTML

I don't want to restrict user to upload large file. User can upload any file size. When i tried file size of 22 MB, It worked. But When I tried 200MB file, it seems it does not entered in if block and return me the output of else block after taking all the time in the world to upload file.
My work on this is mention below. Am I missing something?
HTML:
<form method="post" enctype="multipart/form-data" class="form-horizontal" role="form">
<label for="document">Upload your Document</label>
<input type="file" id="document" name="document">
<label for="doc_cat">Document Category:</label>
<select name="doc_cat" id="doc_cat" class="form-control" required>
<option value="">--Select a Category--</option>
<option value="1">Sales</option>
<option value="2">Technical</option>
<option value="3">Pricing</option>
<option value="4">Policies</option>
<option value="5">Other</option>
</select>
<button type="button" class="btn btn-primary" id="upload" name="upload">Upload</button>
</form>
JQUERY/AJAX:
<script type="text/javascript">
$(document).ready(function () {
$('#upload').click(function () {
var formData = new FormData($('form')[0]);
var that = this;
$.ajax({
url: 'upload-document-proccess.php', //Server script to process data
type: 'POST',
data: formData,
async: false,
beforeSend: function() {
$("#loading-image").show();
},
success: function (msg) {
$("#loading-image").hide();
$("#display-return-msg").html(msg);
$("#display-return-msg").fadeIn("slow").delay(5000).fadeOut("slow");
},
cache: false,
contentType: false,
processData: false
});
$('form')[0].reset();
return false;
});
});
</script>
upload-document-proccess.php:
if(isset($_POST) && $_FILES['document']['size'] > 0)
{
$category_id = $_POST['doc_cat'];
$doc_desc = htmlspecialchars($_POST['doc_desc'],ENT_QUOTES);
switch ($category_id) {
case 1:
$category="Sales";
break;
case 2:
$category="Technical";
break;
case 3:
$category="Pricing";
break;
case 4:
$category="Policies";
break;
case 5:
$category="Other";
break;
default:
# code...
break;
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["document"]["name"]);
$fileName = $_FILES['document']['name'];
$tmpName = $_FILES['document']['tmp_name'];
$fileSize = $_FILES['document']['size'];
$fileType = $_FILES['document']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
if (move_uploaded_file($_FILES["document"]["tmp_name"], $target_file)) {
$msg = "The file ". basename( $_FILES["document"]["name"]). " has been uploaded to ".$category." Category";
} else {
echo "Sorry, there was an error uploading your file.";
}
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
try {
$sql = "INSERT INTO document (doc_name, doc_type, doc_size, user_id, doc_category_id, doc_date, doc_desc)
VALUES ('$fileName', '$fileType', '$fileSize', '$user_id', '$category_id', now(), '$doc_desc')";
// use exec() because no results are returned
$conn->exec($sql);
?><script type="text/javascript">
var msg = '<?php echo $msg; ?>';
$(document).ready(function(){
$("#display-return-msg").html(msg);
$("#display-return-msg").fadeIn("slow").delay(5000).fadeOut("slow");
});
</script>
<?php
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
//echo "<br>File $fileName uploaded<br>";
}else{
echo "Please select File to upload.";
}
?>
In '#display-return-msg' id, It gives me "Please select File to upload" when i uploaded 200MB file. When i uploaded 22 MB file, I got value of 'msg' variable which is correct. Please suggest me What do i do?
there are a lot of settings that could restrict the filesize of your upload. here is an example based on an XAMPP installation on windows. but it should be quite similar on linux.
found at http://mewbies.com/how_to_install_setup_apache_xampp.htm:
TO CHANGE SIZE OF FILES ALLOWED TO UPLOAD:
To allow large file uploads you must change the settings on your PHP & Apache conf files, we'll use
600MB file size as the example, change it to your own needs:
Edit this file: D:\xampp\php\php.ini
Search for: upload_max_filesize
Change
to:
upload_max_filesize = 600M
Search for: post_max_size
Has this:
post_max_size = 8M
Change to (it must be larger than upload_max_filesize):
post_max_size = 700M
Search for: memory_limit
Has this: memory_limit = 128M
Change to, if you don't want any limit:
memory_limit = -1
Or change to (it must be larger than post_max_size):
memory_limit = 800M
Search for: max_execution_time
Has this: max_execution_time = 30
Change to for example:
max_execution_time = 9600
Search for (just below max_execution_time): max_input_time = 60
Has this: max_input_time = 60
Change to: max_input_time =3600
Done, save the changes.
Edit this file: D:\xampp\apache\conf\extra\httpd-default.conf
Search for: LimitRequestBody
If your conf does not have this line; add it
Has this: LimitRequestBody 102400
Change to:
LimitRequestBody 600000000
you set it to 0, meaning unlimited up to 2147483647 bytes (2GB)
Restart your web server.
To upload large files to web server, need to modify some settings (using .htaccess) in the php. Two php configuration options, the maximum upload size: upload_max_filesize and post_max_size. Both can be set to, say, "200M" for 200 megabyte file sizes.
you can test like at the very first line use this line
echo phpinfo();exit;
and find and check 'max_file_uploads' if it does have less than your requirement increase it by writing at the very first of page
ini_set('max_file_uploads', 'as per your need size')
You need to increase upload limit in php.ini file
EDITED: You should ask host to allow you owerwrite php ini configuration for upload limit, if they do this for you will be able to do this, if they can't or don't want you wont be able to to this.
After they enable this you can:
1.create local copy of php.ini in folder where is script which performs upload
2.you can do it in code, example for turning of erro reporting:
error_reporting(-1);
3.you can set this in cpanel for this account if is possible/enabled.

PHP uploading a file not working

The code below is to upload files to a local folder. Once I select a file from the Dialog Box, it should display "OK" to confirm selection of file. But the code below is not working as required.
<?php
if(isset($_FILES['file']['name']))
$name = $_FILES['file']['name'];
if(isset($_FILES['file']['tmp_name']))
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($name))
{
if(!empty($name))
echo 'OK';
else
echo 'Please chose a file';
}
?>
<form action="up.php" method="POST" encrypt="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
First try:
var_dump($_FILES);
If you are not seeing anything in there on post, then you may need to set the following in your php.ini file:
file_uploads = On;
You may also need to change the allowed file sizes in the php.ini file if the file you are trying to upload is bigger than what is allowed:
post_max_size = 8M; make larger if needed
upload_max_filesize = 8M; make larger if needed*
After making these changes make sure to restart your webserver (apache/nginx.)
* upload_max_filesize should never be bigger than post_max_size
......

PHP file database upload

don't have much experience with php programming and web in general, so I've build this code from tutorials and what it supposed to do is to store data in database, but it seems like it completely ignores any statement(e.g. exception when choosing incorrect data format) and it sort of just refreshes the page after hiting submit button, any ideas how to fix this?
<div id='box'>
<form method='post' enctype='multipart/form-data'>
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$randon_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv' && $type !='avi'){
$message = "bad format";
}
else{
mysql_query("INSERT INTO videos VALUES('', '$name', 'videos/$randon_name.$type')");
$message = "successful upload";
}
echo $message;
}
?>
Choose file : <br/>
<input type='file' name='video'/>
<br/><br/>
<input type='submit' value='Upload'/>
</form>
</div>
It seems to be the case that you selected a file larger than your upload_max_filesize or post_max_size.
If this is the case then the $_FILES variable is empty and you see nothing. If you select a smaller file you should see something when doing a print_r($_FILES) at the top of your script.
if you run php in apache as a module you can try to add a .htaccess like this:
php_value upload_max_filesize 200M
php_value post_max_size 200M
Otherwise you need to edit your php.ini. As far as i know you cannot set these php.ini values via ini_set().

Categories