Convert file size in MB from KB - codeigniter

I am done with uploading file in codeigniter. Saved information of uploaded file in DB. I am getting file size in KB, now.
I want file size in MB.

Retrieve all data from uploaded file
$file = $this->upload->data();
print_r($file);//show array elements
$size_in_kb = $file[0]['file_size'];
$size_in_mb = $size_in_kb/1024;
echo 'File size in KB:'.$size_in_kb;
echo '<br>File size in MB:'.$size_in_mb;

Related

php-zip save extracted files in s3 laravel

If you are familiar with Ne-Lexa/php-zip, I want to upload all my extracted files in s3 bucket. I tried this code but instead, it is storing in my public folder instead of s3.
$file = $request->file('filename');
$filename = $file->getClientOriginalName();
$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile($file)
->extractTo(Storage::disk('s3')->makeDirectory($directory));
$zipFile->close();

How do create a file from ascii, then create a link to download the file with Laravel

I'm using a shipping api, that has a method that spits out a pdf in this format (ascii, I believe):
%PDF-1.3\n1 0 obj\n
<<\n
/Type /Pages\n
/Count 1\n
many lines removed here
startxref\n
982\n
%%EOF\n
How do I..
Convert this code into a downloadable .pdf file?
Create a link to download the file?
Note - I do not need to store the file.
ADDENDUM
So, this is moving the correct file to the correct place, but is giving me an error "Call to a member function move() on string".
$pdf_raw = $this->create_label2($data->label_url);
$filename = 'label'.auth()->user()->id.'.pdf';
file_put_contents($filename, $pdf_raw);
$filename->move(public_path().'/img/pdf', $filename);
How can that be?
Here is the solution:
use Illuminate\Support\Facades\Storage;
$pdf_raw = $this->create_label2($data->label_url); //this is raw ascii data
$filename = 'label'.auth()->user()->id.'.pdf'; //can be any name .pdf
Storage::disk('pdf')->put($filename,$pdf_raw); //creates file and moves to correct path
$pdf_link = "/mypath/$filename"; //url to file

Ruby File Upload Size

I'm trying to test the size of an upload to validate its size. Outside of the upload algos, simply looking at the temp file is where I'm having the issue. I have a test file on my desktop named test1.png, which is 115 KB.
a = '/users/rich/desktop/test1.png'
s = File.open(a, 'wb')
r = File.size(a)
p r => 0
p s.size => 0
Not sure what I'm doing wrong here, but both resolve to 0. Not true.
How can I get the size of a file?
The problem is the 'w' flag, since it truncates existing file to zero length, so, since you open the file before getting its size, you get 0.
To get the size you could just use the path of the file without using File.open:
a = '/users/rich/desktop/test1.png'
File.size(a)
Or, if you need to create the File object, just use the 'r' flag:
a = '/users/rich/desktop/test1.png'
s = File.open(a, 'r')
Now you can either use File.size(a) or s.size to get the size of the file.

mime type of uploading file is application/octet-stream fails

I have a form for uploading pdf document and am using laravel validation in my controller like,
$validator = Validator::make($request->all(), [
'document' => 'required|mimes:jpeg,jpg,png,pdf|max:25600',
]);
some of the images and pdf documents mime types like application/octet-stream
is there any way to check that application/octet-stream file mime is png, jpeg or pdf file ?
I've got the same problem, it wasn't a Laravel issue.
I fixed this modifying
php.ini
; Maximum allowed size for uploaded files.
upload_max_filesize = 10M ; from 5M to 10M
; Must be greater than or equal to upload_max_filesize
post_max_size = 10M ; from 5M to 10M

How do I set the individual upload file names?

I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.
Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.

Resources