How to upload multiple files from single form input in Laravel? - php

I'm trying to upload multiple pdf files using Laravel. I've made the form using simple HTML and enabled the multiple attribute in the input field. I've tried many other methods mentioned in the stack community but they didn't work for me.
In the controller, I've the following to check whether the files are being uploaded or not. The foreach loop will return the path of the files uploaded.
Controller Code
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path = $attachment->store('corporate');
dd($path);
}
HTML Form Code
<input type="file" name='corpfile[]' accept=".xls, .xlsx, .xlsm, .pdf" multiple>
The dd($file) is returning an array with all the files uploaded but the dd($path) is returning only one file's path.
I don't know what the issue is. Any help is appreciated.

you're diying the loop with dd
if you want to store the path in array , you should do that
$path = [];
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path[] = $attachment->store('corporate');
}
}

Related

Download multiple uploaded files in Laravel

I have a form input which can upload multiple files. So all the files paths are saved in the database as an array of strings . Below is my controller to download the files. I wanted to know how I can go about presenting each file to be downloaded in blade view. So I could download a single file at a time. My controller below. It works only when downloading a single file. I get this error Invalid argument supplied for foreach().
public function download($id) {
$deal = Deal::findorFail($id);
$files = $deal->uploads;
foreach ($files as $file) {
return Storage::download($file);
}
}
public function download($id) {
$deal = Deal::findorFail($id);
$files = $deal->uploads;
foreach ($files as $file) {
return Storage::download($file);
--------^^^^^^-------------------------
}
}
when you return first file, it breaks foreach loop. So you have to return all files together. And only way to achieve this, is creating a zip file that contains all files..
For this purpose you may use chumper/zipper package
$zipper = Zipper::make(public_path('/documents/deals.zip'));
foreach ($files as $file) {
$zipper->add(public_path($file)); // update it by your path
}
$zipper->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);
update
Add accessor to Deal model to get files as array
Deal Model
php artisan getUploadsAttribute($attribute){
return explode(",",$attributes);
}

How to upload multiple files with simple form in symfony 3

I want to create a form with multiple files (images) upload in Symfony 3 and and simple form (i'm not using symfony form builder), but i get only one file (the first file). i'm using POSTMAN for send files via post method.
public function testAction(Request $request)
{
$file = $request->files->get('images');
$ext = $file->guessExtension();
$file_name = time() . '.' . $ext;
$path_of_file = 'uploads/test';
$file->move($path_of_file, $file_name);
var_dump($file);
die();
}
You didn't provide enough information, but maybe the problem is that you didn't set key property as array in Postman like this 'images[]' - than your Symfony endpoint will get an array of UploadedFile objects with all the needed data about your files and you also need to put foreach in your code here:
public function testAction(Request $request)
{
$file = $request->files->get('images');
foreach ($file as $item) {
do some operations
}

Laravel 5.4 upload original file name and extention

how can i upload original filename (file.jpg) to database when submitting file via form. Controller:
public function addCv(Request $request){
$cv = Cv::create($request->all());
$file = $request->file_name;
$filename = $file->getClientOriginalName();
Storage::putFileAs('public/uploads', $file, $filename);
return redirect()->back();
}
at the moment, this function uploads a path like this C:\xampp\tmp\php18DD.tmp.
Instead of that i want just filename and extension (file.extension).
Storage is working fine - storing with original name.
You could try
$file = $request->image->getClientOriginalName(); //Get Image Name
$extension = $request->image->getClientOriginalExtension(); //Get Image Extension
$fileName = $file.'.'.$extension; //Concatenate both to get FileName (eg: file.jpg)
I would suggest adding enctype="multipart/form-data" in the form tag in the view, from where you are uploading the file:
<form enctype="multipart/form-data">
You can use like below,
$this->getRequest()->files['name_of_file_field_in_post']->getClientOriginalName();
Reference: Get Uploaded File's Original Name

Upload a file to specific folder using WordPress

I need help for how to upload a file in WordPress, I don't want to upload files into the default path of media files i.e. into uploads/../.., I want to upload my files into wp-content/uploads/my_folder, I just created one file in wp-admin folder and added some functionality there, from that form I want to upload my file (not creating plugin).
Is it possible to do like this? If yes then how? If no then what I want to do for uploading file?
I tried the following solution for it:
$path_array = wp_upload_dir();
$upload_path = $path_array['baseurl'].'/myfoldername/';
$target_path = $upload_path."/".$file_name;
$file_name = $_FILES['fieldname']['name'];
$tmp_name = $_FILES["fieldname"]["tmp_name"];
upload_user_file($_FILES,$upload_path); // Called this function
In functions.php of my theme, I defined the above called function upload_user_file() like as follows:
function upload_user_file( $file = array(),$path) {
if(!empty($file))
{
$uploaded=move_uploaded_file($file['fieldname']['tmp_name'],$path.$file['fieldname']['name']);
if($uploaded)
{
echo "Uploaded successfully ";
}
else
{
echo "Some error in upload ";
print_r($file['error']);
}
}
}
Please help me for this issue.
Thanks.
It's not working because of FILES array loses its values very soon..
Upload it in the same code where you called function it will work...
Thanks cale_b its really helpful.

Basic image upload in Joomla custom component

I am trying to add image upload function to my view where I am creating model. I tried many sample codes here but I can't get to full path to file. My code looks like this:
public function submit() {
jimport ( 'joomla.filesystem.file' );
// Check for request forgeries.
JRequest::checkToken () or jexit ( JText::_ ( 'JINVALID_TOKEN' ) );
// Initialise variables.
$app = JFactory::getApplication ();
$model = $this->getModel ( 'createaction' );
// Get the data from the form POST
$data = JRequest::getVar ( 'jform', array (), 'post', 'array' );
echo $data['image']; <-- here
$createdItem = $model->createItem ( $data );
if ($createdItem) {
$redirect = JRoute::_ ( 'index.php?option=com_akcehned&view=actions', false );
$this->setRedirect ( $redirect, "Akce byla vytvořena" );
} else {
echo "<h2>Omlouváme se, ale něco se stalo špatně</h2>";
}
return true;
}
Part of file input in xml:
<field
name="image"
type="file"
description="COM_AKCEHNED_FORM_DESC_CREATEACTION_IMAGE"
label="COM_AKCEHNED_FORM_LBL_CREATEACTION_IMAGE"
size="10"
accept="image/*" />
Where I tried to echo file file input I get just name (image_name.jpg and so) but I need fullpath right? I saw examples with ['tmp_name'] but It's not working for me.
I tried code like this:
$jinput = $app->input;
$files = $jinput->files->get('jform');
$file = $files['image'];
echo $file;
echo $file['tmp_name'];
But it's not working for me either. I just get empty values. Can someone give me working block of code where I get data from other inputs and fullpath to file for upload? It's for joomla 2.5, Thanks
You need to make sure the the form tag contains enctype attribute and that it is set to "multipart/form-data" if your are going to upload files.
eg
<form action="" method="post" enctype="multipart/form-data">
Also see: What does enctype='multipart/form-data' mean?
Did you try DPAttachments, it takes only three lines of code to integrate attachment support into your component.
if (JLoader::import('components.com_dpattachments.libraries.dpattachments.core', JPATH_ADMINISTRATOR)) {
echo DPAttachmentsCore::render('com_demo.item', $object->id);
}
Drag and drop and copy paste from your clipboard (no need to save the file to your hard disk) is supported. Perhaps you want to give it a try. If not here is a link to the Github repo how the upload function is implemented:
https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/models/attachment.php#L50
[I'm the author of this component]

Categories