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.
Related
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.
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
I am uploading the multiple images in codeigniter when I try to upload more than 10 images it shows blank screen without any error but same code worked when I upload the 10 or less than 10 images. I unable to find out whats going wrong please help.
I also set the following settings in php.ini:
upload_max_filesize = 128M
max_file_uploads = 50
Here is my html code:
<form method="post" role="form" enctype="multipart/form-data" action="<?php echo base_url();?>admin/allbums/add_new">
<input type="file" name="myfile[]" multiple="multiple">
<input type="hidden" name="user_id" value="<?php echo $user_id;?>">
</form>
here is code in my controller:
public function add_new() {
$config['upload_path']="./uploads/";
$config['allowed_types']='*';
$config['encrypt_name'] = TRUE;
$config['overwrite'] = false;
$this->load->library('upload', $config);
if($this->upload->do_multi_upload("myfile")) {
$file_arr = $this->upload->get_multi_upload_data();
$arr_lenth = count($file_arr);
$user_id = $_POST['user_id'];
for($i=0;$i<=$arr_lenth-1;$i++) {
$data[] = array(
'img_name' => $file_arr[$i]['file_name'],
'size' => $file_arr[$i]['file_size'],
'user_id' => $user_id
);
}
$this->allbum_model->insert_allbum($user_id,$data);
$data['success'] = '<div class="note note-success"> Allbum Added Successfully!</div>';
$data['user_id'] = $user_id;
$this->load->view('admin/allbum', $data);
} else { // else if file not uploaded correctly
echo $this->upload->display_errors();
}
you should also add post_max_size to php.ini so you can be able to send the data
;Maximum allowed size for uploaded files.
upload_max_filesize = 128M
;Must be greater than or equal to upload_max_filesize
post_max_size=128M
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().
I'm having issues uploading a pdf to my server. The upload_max_filesize is 2M and the file(s) are more then that, around 4M. I found a post with a similar issue to mine here
$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size
What I can gather from php.net for the correct usage of ini_set commands is this, which I am currently using.
ini_set('upload_max_filesize', 100000000);
ini_set('post_max_size', 110000000);
ini_set('memory_limit', 120000000);
ini_set('max_input_time', 20);
But in the link I posted it seems they are using a different method (if they aren't just summarizing the correct code that is). But It seems my code isn't working as is either. I have <?php phpinfo(); ?> at the bottom of my page and it says the upload_max_filesize is still 2M. Am I using the correct syntax for ini_set? or is my issue with upload my pdfs something else?
My code handling the upload is
//======================pdf upload=====================
if ($_POST['erasePDF'] == "Yes") //checking if erase box was checked and erasing if true
{
if (file_exists($pdf))
{
unlink( $pdf );
$pdf = "";
}
}
print_r($_FILES['pdf']);
if (!empty($_FILES['pdf']['name'])) //checking if file upload box contains a value
{
$saveDirectoryPDF = 'pdfs/'; //name of folder to upload to
$tempName = $_FILES['pdf']['tmp_name']; //getting the temp name on server
$pdf = $_FILES['pdf']['name']; //getting file name on users computer
$test = array();
$test = explode(".", $pdf);
if((count($test) > 2) || ($test[1] != "pdf" && $test[1] != "doc" && $test[1] != "docx")){
echo "invalid file";
}else{
$count = 1;
do{
$location = $saveDirectoryPDF . $count . $pdf;
$count++;
}while(is_file($location));
if (move_uploaded_file($tempName, $location)) //Moves the temp file on server
{ //to directory with real name
$pdf = $location;
}
else
{
echo "hi";
echo '<h1> There was an error while uploading the file.</h1>';
}
}
}else{
$pdf = "";
}
if(isset($_POST['link']) && $_POST['link'] != ""){
$pdf = $_POST['link'];
}
//======================end of pdf upload==============
The output of the line 'print_r($_FILES['pdf']);' is
Array ( [name] => takeoutmenu.pdf [type] => [tmp_name] => [error] => 1 [size] => 0 )
Some providers does not allow you change certain values in running time. Instead of this, try to either change it in the real php.ini file or use an .htaccess (For Apache web servers) where you can add your configuration. You can find more information in the PHP.net article about this subject: How to change configuration settings.
Based on your story, example .htaccess
<IfModule mod_php5.c>
php_value upload_max_filesize 100000000
php_value post_max_size 110000000
php_value memory_limit 120000000
php_value max_input_time 20
</IfModule>