Unable to upload more than 10 files at a time - php

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

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.

CodeIgniter 3. Upload image twice but with different file name

[SOLVED]
I am having a problem in function in my controller. I want to upload a single image twice and save it in the same directory but have different file names.
Here's my form(view.php):
<form action="<?php echo base_url("process/testFunction"); ?>" enctype="multipart/form-data" method="post">
<input type="file" name="file_data" size="50" required>
<input type="hidden" name="user_id" value="<?php echo $this->session->userdata('user_id'); ?>" readonly>
<input type="hidden" name="purpose" value="Picture" readonly>
<input type="submit" value="Save Changes">
</form>
controller.php:
function testFunction(){
$userID = $this->input->post('user_id');
$purpose = $this->input->post('purpose');
$randomText = time();
if($purpose == "Picture"){
$uploadPath = "./uploads/images/";
$allowedTypes = "jpg|jpeg|png";
$maxSize = 10000000;
$fileName = "PP_".$userID.".jpeg";
$fileName2 = "PP_".$userID."_".$randomText.".jpeg"; //for backup
}
$config = array(
'upload_path' => $uploadPath,
'allowed_types' => $allowedTypes,
'max_size' => $maxSize,
'file_name' => $fileName
);
$configClone = array(
'upload_path' => $uploadPath,
'allowed_types' => $allowedTypes,
'max_size' => $maxSize,
'file_name' => $fileName2
);
$this->load->library('upload', $config);
$this->load->library('upload', $configClone);
if($this->upload->do_upload('file_data')){
echo "Uploaded";
}else{
echo $this->upload->display_errors();
}
}
What is currently happening in my code is. Although it uploads 2 same images. The file name with "Random Text" is not working.
[Solution]: controller file. I modified some lines.
Reference: Javier's answer below
$this->load->library('upload', $config);
if($this->upload->do_upload('file_data')){
unset($this->upload);
$this->load->library('upload', $configClone);
$this->upload->do_upload('file_data');
echo "Uploaded";
}else{
echo $this->upload->display_errors();
}
Loading the upload library twice in a row, even with different configuration arrays, won't do anything. Codeigniter will ignore the second $this->load->library(); statement if the library is already on memory. This is intended to prevent conflicts, race conditions, etc.
You'd need to:-
load the first instance of the library
process the first upload
unload the first instance of the library
load the second instance of the library
process the second upload
Point number 3 could for example be achieved using unset($this->upload); after processing the first upload.
Remove this line
$this->load->library('upload', $configClone);
Change
if($this->upload->do_upload('file_data')){
$this->upload->initialize($configClone);
if($this->upload->do_upload('file_data')){
echo "Uploaded";
}else{
echo $this->upload->display_errors();
}
}else{
echo $this->upload->display_errors();
}
Explanation: Javier Larroulet's answer

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.

PHP file upload error tmp_name is empty

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

Max_File_Uploads directive php.ini

Im trying to do a simple file upload. I've done it many times before and it's been fine. For some reason this time I keep getting error UPLOAD_ERR_INI_SIZE coming up. Even though i've uploaded bigger files on the same server before. Here is my PHP.INI:
display_errors = On
short_open_tag = On
memory_limit = 32M
date.timezone = Europe/Paris
upload_max_filesize = 10M
post_max_size = 10M
And my HTML form:
<form action="/settings/upload-image" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=(1024*1024*1024);?>">
<input name="files[]" id="attachfile" type="file" />
<br /><br />
<input type="submit" class="submit" value="Upload New Profile Image">
</form>
And my code:
foreach($files as $file)
{ $ext = strtolower(pathinfo($file[0], PATHINFO_EXTENSION));
if(in_array($ext,$allowed_upload_ext)===TRUE)
{
if(!$file[3]) { // If no error code
//$newFile = $me['id'].".$ext";
$newFile = $file[0];
resizeImage($file[2],PROFILE_IMAGES."/".$newFile,$ext,500);
genThumbFile($file[2],PROFILE_IMAGES."/thumb/".$newFile);
runSQL("UPDATE `users` SET `image`='{$file[0]}' WHERE `id`='{$me['id']}';");
array_push($msgs,"Image uploaded successfully.");
$me = select("SELECT * FROM `users` WHERE `id`='{$me['id']}';",true);
} else {
array_push($msgs,"!".fileError($file[3]));
}
} else {
array_push($msgs,"!The file ".$file[0]." could not be uploaded as it is the wrong file type.");
}
}
The only difference this time is that I am resizing and genorating thumbs with the temporary upload file instead of copying over the file first. Could that be the problem? I dont think so, because if the image is small it works perfectly fine. But I try anything like 2mb and it throws a fit.
Suggestions?
Thanks for ALL YOUR HELP guys. :P
I solved it - Missing line in PHP.INI:
file_uploads = On
Just for anyone who fins this.

Categories