Locking specific cells with a password phpExcel - codeigniter

I am uploading an excel file from a folder in my computer to a folder in the server, after the upload i am loading the uploaded file so that i can protect certain cell, the first method i used below does not work at all
function LockCertainCells(){
$labref= $this->uri->segment(3);
$objReader = new PHPExcel_Reader_Excel2007();
$path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
$objPHPExcel = $objReader->load($path);
$objPHPExcel->setActiveSheetIndexbyName('Sample Summary');
$objPHPExcel->getActiveSheet()->protectCells('A17:G85','PHPExcel');
$objPHPExcel ->getActiveSheet()->getProtection()->setSheet(true);
}
This second one
function LockCertainCells(){
$labref= $this->uri->segment(3);
$objPHPExcel = new PHPExcel;
$path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
$objSheet = $objPHPExcel->load($path);
$objSheet->setActiveSheetIndexbyName('Sample Summary');
$objSheet->protectCells('A17:G85', 'PHP');
$objSheet->getProtection()->setSheet(true);
}
Throws me this error:
Fatal error: Call to undefined method PHPExcel::load() in C:\127.0.0.1\htdocs\NQCL\.....
suggestions!

The PHPExcel class doesn't have a load method, which is precisely why you get that error.... the first method is the method provided by the PHPExcel library for loading a file into a PHPExcel object, the second is not. Use the method that works (and is the method described in all the documentation); not the one that doesn't exist.
The first method for locking the cells isn't doing anything with the PHPExcel object (such as saving it) after you have set cell protection; so as soon as the LockCertainCells function terminates, it will be out of scope and discarded by the PHP script. If you want to make the changes in the file, you need to save the file again. I believe that MS Excel itself requires you to save a file if you want to make any changes to that file permanent.
EDIT
You also miss setting the password for the protected cells:
$objSheet->getProtection()->setPassword('mypassword');

Related

laravel how to save files with predefined name

in laravel controller i am trying to give each video file i uploading should be rename as
**myfile.mp4**
and save in public folder.
but my present code makes random number for my files but i need to give name as myfile
my controller
$input['file_id'] = time() . '.' . $request->file_id->getClientOriginalExtension();
$folder1 = public_path('/public');
$path1 = $folder1 . $input['file_id']; // path 1
$request->file_id->move($folder1, $input['file_id']);
What you want is :
$input['file_id'] = 'myfile.'.$request->file_id->getClientOriginalExtension();
But you cannot give the same name 'myfile' to all your videos because each recording will be overwritten by the previous one, you need unique names.
For this you can do, for example:
$input['file_id'] = 'myfile'.time().'.'.$request->file_id->getClientOriginalExtension();
// here, time() represents the time at which the video was saved in your '/public' file
or
$input['file_id'] = 'myfile'.date().'.'.$request->file_id->getClientOriginalExtension();
// here, date() represents the date the video was saved in your '/public' file

Delete file in Laravel using Google Drive API

I'm using this command to upload the file to Google Drive (store function in controller):
$request->anexo->storeAs('1tD*******************-6', $file_name, 'google');
And this to get the link:
$link = Storage::disk('google')->url('1tD*******************-6/' . $file_name);
This is working fine. But when i try to delete the file i can't find it in the destroy function in the same controller. I've tried the commands below and all of them return false:
$att = OrdemAttachment::findOrFail($id);
$link = Storage::disk('google')->url('1tD*******************-6/' . $att->file_name);
// dd($link);
$exists = Storage::disk('google')->has('1tD*******************-6/' . $att->file_name);
// dd($exists);
$delete = Storage::disk('google')->delete('1tD*******************-6/' . $att->file_name);
// dd($delete);
Is there another command that must be used instead of these?
I figured it out, I was trying to use the file name and even tried to use the google drive link. But what worked was using the file id (you can find the id inside the link). So, for example, if the link to the file is https://drive.google.com/uc?id=1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD&export=media, to delete the file in google drive with the id would go like this:
$delete = Storage::disk('google')->delete('1tD*******************-6/1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD');
1tD*******************-6 is the subfolder id and 1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD is the id of the file extracted from the link.

File storage not working. Laravel 5

I'm trying to save a .txt file in a folder named txts inside my public folder but the method i'm using is not working:
$txt_nombre = 'nomina_'. $extras['desde'] .'_'. $extras['hasta'] .'.txt';
$txt_contenido = '';
foreach ($personal as $per) {
$txt_cadena = 'BNC ' . $per->cuenta_nomina . ' ' . $per->netos . ' ' . $per->cedula_identidad;
$txt_contenido .= $txt_cadena . "\n";
}
Storage::put('public/txts/'. $txt_nombre, $txt_contenido);
It doesn't even throw an error, my function keeps running but the file is not being saved. Any idea why?
According to the Documentation
By default, the public disk uses the local driver and stores these files in storage/app/public.
Looking at your code, assuming that you haven't changed default configuration, your file should be created in storage/app/public/public/txts/ folder.
I am not sure how Laravel's Storage abstraction handles non-existing folders so you might want, for test, try using just Storage::put('file.txt', "content") and see if it's being created in storage/app/public folder

Saving Intervention Image In Owners Folder in Laravel 5

I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.

Error while uploading in Laravel 4

I'm trying to make a image upload in lavarel 4. The upload passes by
The file "C:\xampp\tmp\phpD1F3.tmp" does not exist
This is my upload code :
$file = Input::file('image');
// Get extension
$extension =$file->getClientOriginalExtension();
// Generate a file name
$fileName = 'pool' . Str::quickRandom();
$fileNameExtension = $fileName . '.' . $extension;
// Process upload
Input::file('image')->move(public_path() . '/uploads/images/app/pools/original/', $fileNameExtension);
`
I don't know why i get this error , please can anyone help me to solve this?
You are probably making some operations on "uploaded" file after moving it to server by move() function. Please make sure there is no operation made on temporary file after calling Input::file('image')->move() function.

Resources