I have a script that collects data via ftp and works great.
The issue is you have to specify the filename that you want to download.
Example:
https://mydomain/downloadFile?file=folder/file1.csv
What i want to do is save all the files in the folder without specifying a filename.
Function:
function downloadFile(Request $request) {
$file = $request->get('file');
if(!$file) {
return Response::json('please provide valid path', 400);
}
$fileName = basename($file);
$ftp = Storage::createFtpDriver([
'host' => 'ftp.site.com',
'username' => 'email.com',
'password' => 'password',
'port' => '21', // your ftp port
'timeout' => '30', // timeout setting
]);
$filecontent = $ftp->get($file); // read file content
// save file.
Storage::put('file1.csv', $filecontent);
// download file.
return Response::make($filecontent, '200', array(
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="'.$fileName.'"'
));
}
Related
I just want to save my image or file in the database as a URL that can easily get for the frontend. I tried it a lot but did not get a solution if anyone can get me out with this?
public function addPPQuestion(Request $pp_questions)
{
$validate = Validator::make($pp_questions->all(), [
'qual_cat_id' => 'required|exists:qualification_categories,qual_cat_id',
'year_id' => 'required|exists:years,year_id',
'course_id' => 'required|exists:courses,course_id',
'board_id' => 'required|exists:board_of__edus,board_id',
'paper' => 'required|mimes:pdf|max:2048',
]);
if ($validate->fails()) {
$messages = $validate->errors()->count() > 1 ? $validate->errors()->all() : $validate->errors()->first();
return response()->json(['code' => 400, 'message' => $messages], 400);
} else {
$paper = $pp_questions->paper;
$fileName = $paper->getClientOriginalName();
$pp_question = pp_question_answer::create([
'paper' => $pp_questions->paper->storeAs('', $fileName, ''),
'qual_cat_id' => $pp_questions->input('qual_cat_id'),
'year_id' => $pp_questions->input('year_id'),
'course_id' => $pp_questions->input('course_id'),
'board_id' => $pp_questions->input('board_id'),
]);
return response()->json(['code' => 201, 'message' => 'Question Created Successfully',
'object' => $pp_question], 201);
}
}
Step 1: Validate the files as you want in the api.
Content-Type: application/json
It will return JSON data as result.
$request->validate(['file' => 'required|mimes:jpeg,jpg,png,gif,svg,pdf,txt,doc,docx,application/octet-stream,audio/mpeg,mpga,mp3,wav|max:204800']);
Step 2: Make a file name & store it in the folder and get the URL of it.
$unique_id = strtolower(str_replace('.', '', uniqid('', true)) . time());
$photoFile = $request->file('paper');
$extension = $photoFile->getClientOriginalExtension();
$fileNameToStore = $unique_id . '.' . $extension;
$filepath = $photoFile->storeAs('photos', $fileNameToStore);
$pp_question = Model::create([
'paper' => $filepath,
'qual_cat_id' => $pp_questions->input('qual_cat_id'),
'year_id' => $pp_questions->input('year_id'),
'course_id' => $pp_questions->input('course_id'),
'board_id' => $pp_questions->input('board_id'),
]);
return response()->json([
'code' => 201,
'message' => 'Question Created Successfully',
'object' => $pp_question
], 201);
Step 3: To display data on the front-end side.
$imagePath = !empty($pp_question->pdf) && Storage::exists($pp_question->pdf) ? Storage::url($pp_question->pdf) : asset(Storage::url('default/pdf.png');
I'm using ftp as a filesystem driver.
'ftp' => [
'driver' => 'ftp',
'host' => 'myip',
'username' => 'myusername',
'password' => 'mypassword',
'passive' => true,
'ssl' => true,
'passive' => true,
'timeout' => 30,
// Optional FTP Settings...
// 'port' => 21,
// 'root' => '',
// 'passive' => true,
// 'ssl' => true,
// 'timeout' => 30,
],
I can upload in this ftp server but cannot list or dowload anything beacause of
File not found at path
My code for listing is this:
Storage::files()
When I try to do it with PHP functions it works! I need to do this with Storage laravel functions
$conn_id = ftp_ssl_connect("myip");
$login = ftp_login($conn_id, "myusername", "mypassword");
$file_list = ftp_nlist($conn_id, "."); // this works like a charm and Yes, I tried to do Storage::files('.') as well !
I have a function in controller
public function upload(Request $request)
{
$file = $request->file('File');
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
if i send the $request in log it shows something like this
array (
'_id' => 'tuYDOc644W6DDgAS',
'_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',
'upload_column' => 'File',
'id' => 'WU_FILE_0',
'name' => '134772132_217259409961521_6013657189083751736_o.jpg',
'type' => 'image/jpeg',
'lastModifiedDate' => '2/7/2021, 6:44:12 AM',
'size' => '629882',
'_file_' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',
'mimeType' => 'image/jpeg',
'error' => 0,
'hashName' => NULL,
)),
)
but this is giving a "Call to a member function move() on null"
i am using laravel-admin package , how can i properly save file and get information about the file?
I just found , its bit different with laravel-admin package
the request object is
array (
'_id' => 'tuYDOc644W6DDgAS',
'_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',
'upload_column' => 'File',
'id' => 'WU_FILE_0',
'name' => '134772132_217259409961521_6013657189083751736_o.jpg',
'type' => 'image/jpeg',
'lastModifiedDate' => '2/7/2021, 6:44:12 AM',
'size' => '629882',
'_file_' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',
'mimeType' => 'image/jpeg',
'error' => 0,
'hashName' => NULL,
)),
)
i can get the file name and file like this and store them
Storage::disk('public')->put($request->name, $request->_file_);
Try editing this.
From:
$file = $request->file('File');
To:
$file = $request-> File; //Use file input tag's name attribute here.
Hope this will be useful.
You can use laravel storage: https://laravel.com/docs/8.x/filesystem#the-public-disk
just need 3 basic steps
create symbolic link from command: php artisan storage:link
config your file system in the config directory: public_path('storage') => storage_path('app/public')
save file: Storage::disk('public')->put('file_name', 'file');
You can use these steps below:
Validate the requested file by your valid mimes:
$this->validate($request, [
'File' => ['required', 'mimes:jpeg,gif,bmp,png', 'max:2048']
]);
Get the file from the request
$image = $request->file('File');
Rename the given filename
// get the original file name and replace any spaces with _
// For example, Business Cards.png = timestamp()_business_cards.png
$filename = time()."_". preg_replace('/\s+/', '_', strtolower($image->getClientOriginalName()));
Move the image to the location (public)
$tmp = $image->storeAs('uploads', $filename, 'public');
Using the zend mail php package to connect to Gmail and getting an error
My Method looks like
//Get all mail
public function GetMail() {
$mail = new Imap([
'host' => 'imap.gmail.com',
'ssl' => 'tls',
'port' => '993',
'user' => 'example#gmail.com',
'password' => '*********',
]);
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
printf("Mail from '%s': %s\n", $message->from, $message->subject);
}
}
The error is as follows:
cannot read - connection closed?
Using Laravel as a framework
So i fixed using
$mail = new Imap(array(
'host' => 'imap.gmail.com',
'user' => 'noreply#gmail.com',
'password' => '*********',
'ssl' => 'ssl',
'port' => 993
));
$mail->countMessages();
$content = array();
foreach ($mail as $messageNum => $message) {
array_push($content, ['from' => $message->from,'subject' => $message->subject, 'id' => $message->messageId]);
}
return $content;
I'm uploading files to destinationPath and I'm having a problem with saving each filename when I'm trying to make a download link for those files.
foreach ($files as $file) {
$fileName = now()->format('Y-m-d-H-i-s');
$uploaded = Storage::put($destinationPath.$fileName.'.'.$file->getClientOriginalExtension(),file_get_contents($file->getRealPath()));
}
if($uploaded){
Project::create([
'description' => $request->input('description'),
'division' => $request->input('division'),
'who' => $request->input('who'),
'whom' => $request->input('whom'),
'content' => $request->input('content'),
'filename' => $division.'/'.$userName.'/'.$fileName
]);
}