I want to upload one or more files at the same time in postman. I would like to create a condition to check if the number of uploaded files is equal to 1 write some code else write another code. I would like to be able to count the file number. Any idea how to do that please?
enter image description here
In laravel try something like this:
if (is_array($request->file('file')) and count($request->file('file')) != 1) {
// uploaded files is not equal to 1
}
This should be done in the validation
'file' => ['min:1', 'max:1']
Use this to make sure at least one file is uploaded along with other validation rules
'file' => ['min:1', ....]
Then check the number of files uploaded and add your code accordingly.
if (count($request->file('file')) == 1) {
// code for a single file
} elseif (count($request->file('file)) > 1) {
// code for multiple files
}
In Laravel I can do database transactions by passing a closure to the DB::transaction function.
Does Laravel have any support for a similar feature for the File or Storage facade? Where the file operations are run in a transaction, with rollback in case a file operation fails?
I'm imagining something like
$files = ... // Something that returns a collection
File::transaction(function () use ($files) {
$files->each(function() {
File::move(....);
});
});
There is no built in way of doing it so you'd have to make an implementation yourself.
A simple method of achieving it would be
$fileName = ""; // or $fileNames ( array ) if multiple file uploads
$files = "" // to be used if you're going to update or delete files. Again if multiple file modifications then use array
try{
/* Just a note, but your new file could overwrite any existing files,
so before uploading, check if another file exists with same filename
And if it does, load that file and keep it in the $files variable
*/
// Upload File
$fileName = // name of uploaded file
$files = // Any existing file you're going to modify. Load the entire file data, not just the name
// Modify/Delete a file
}( \Exception $e ){
// Now delete the file using fileName or $fileNames if the variable is not empty
// If you modified/deleted any file, then undo those modifications using the data in $files ( if it's not empty )
}
In this method, existing files are loaded to memory, but if there are multiple large files, it might be better to move them to a temporary location instead, and move them back if any exception is thrown. Just don't forget to delete these temporary files if the file transaction is a success
I am trying to implement searching feature in laravel. Everything is working fine. I am passing the html table data in a variable and parsing it later.
The problem is I could not figure out how to increment a value inside the HTML element.
I am trying this
foreach ($studentDetails as $indexkey=>$studentDetail){
$output .= "<tr><td>". $indexkey + 1 ."</td></tr>";
}
This is not going to work as string concatenation is being done in the value. Any idea on how to increment the value by 1.
You can do something like this below: Take a variable for increment before the output variable and use that in output variable:-
foreach ($studentDetails as $indexkey=>$studentDetail){
$temp = $indexkey + 1;
$output .= "<tr><td>". $temp ."</td></tr>";
}
I have an INPUT_IMAGE_CHECK_ALL function that takes image. In INPUT_IMAGE_CHECK_ALL there are 7 functions that image is passed to.
How do I feed images from folder 1 by 1 to the INPUT_IMAGE_CHECK_ALL function?
image = imread('6-Capmissing/capmissing-image078.jpg');
INPUT_IMAGE_CHECK_ALL(image);
INPUT_IMAGE_CHECK_ALL code:
function [ ] = INPUT_IMAGE_CHECK_ALL( image )
CHECK_FOR_CAP(image);
CHECK_FOR_NOLABEL(image);
CHECK_FOR_NOLABELPRINT(image);
CHECK_FOR_OVERFILLED(image);
CHECK_FOR_LABELNOTSTRAIGHT(image);
CHECK_FOR_UNDERFILLED(image);
end
Images that I want to feed into INPUT_IMAGE_CHECK_ALL are in folder "All" named image001 to image141.
Since you know how the name of the images you want to read (image001, image002, ... from 1 to 141) the easiest way is to use a loop in which:
build the names using the function sprintf
read the image whith the name you've build
feed your function with the image data
A possible implementation could be
for img_idx=1:141
img_name=sprintf('All/image%3.3d.jpg',img_idx)
disp(['Reading ' img_name])
the_image=imread(img_name);
INPUT_IMAGE_CHECK_ALL(the_image);
end
In the above code:
notice the format used in the call to sprintf: %3.3d: this allows padding with 0 the number in the buildoing of the filename, so that you can have 001, 002, ... 013 and so on
if the folder All in which your images are stored is not in the MatlLab path you have to specify the folder in building the filename
I've used the_image as name of the variable in which to store the data of the image because image (the varaible you are using) is a MatLab function.
the call to the disp function is used only to print on the CommandWindow a message about what the script is dooing (which image is processing); you can remove it
If the folder All only the images you you want to process, another possibility could be to get the list of images using the function dir to get the filenames of the
The function dir returns a struct in which the information about the files are stored.
In this case you have to loop over the list returned by dir
% Get the filenames of all the images in the folder "All"
img_list=dir('All/image*.jpg')
% Loops over the list of images
for img_idx=1:length(img_list)
img_name=['All/' img_list(img_idx).name]
disp(['Reading ' img_name])
the_image=imread(img_name)
INPUT_IMAGE_CHECK_ALL(the_image);
end
Hope this helps,
Qapla'
This is my controller function where i am running a loop to create multiple document pdf
and merge into one.
public function tick_pdf(){
$tt='';
$name="iftekhar";
$title="Event Football";
$num=4;
for($i=0;$i<4;$i++){
$this->pdfHeader($row->EventId);
$this->pdf->fontpath = 'font/';
//$this->pdf->AddPage();
$this->pdf->Ln(10);
$this->pdf->SetFont('Arial','B',12);
$this->pdf->Cell(10);
$this->pdf->Cell(60,10,"Name",0,0,'L');
$this->pdf->Cell(10,10,":",0,0,'L');
$this->pdf->MultiCell(60,10,$name,0,1);
$this->pdf->SetFont('Arial','B',12);
$this->pdf->Cell(10);
$this->pdf->Cell(60,10,"Title",0,0,'L');
$this->pdf->Cell(10,10,":",0,0,'L');
$this->pdf->MultiCell(60,10,$title,0,1);
$this->pdf->SetFont('Arial','B',12);
$this->pdf->Cell(10);
$this->pdf->Cell(60,10,"Number",0,0,'L');
$this->pdf->Cell(10,10,":",0,0,'L');
$this->pdf->MultiCell(60,10,$num,0,1);
$this->pdf->SetFont('Arial','B',12);
$this->pdf->Cell(10);
$this->pdf->Cell(60,10,"Date",0,0,'L');
$this->pdf->Cell(10,10,":",0,0,'L');
$this->pdf->MultiCell(60,10,$date,0,1);
$tt.=$this->pdf->Output('', 'S');
}
echo $tt;
}
but I am gettiin an error in browser
"Undefined index: data
Filename: libraries/fpdf.php
Line Number: 1659" I am using fpdf for generating pdf
If you need to create multiple PDF documents you will need several class instances. Actually you're working with only one instance.
Also it is not possible to concatenate a PDF document by simply concatenating their bytes ($tt .= ...). Generally it is not possible to send multiple PDF documents to the client in a single request.
If you want to create multiple pages, just remove the comment before AddPage(), delete the variable $tt and move the Output() call outside of the loop.