Laravel: error while trying to upload multiple files - laravel

I am trying to upload multiple images but unfortunately, I am getting an error "message": "The file "phpzP428U.png" does not exist", How can I solve this? Thanks.
return request
dd($request->all())
InboxController
foreach ($request["file"] as $key => $value) {
$file = Storage::disk('yourstitchart')->putFile('', $value);
array_push($files, $file);
}
foreach ($request["users"] as $key => $users) {
$digitzingInbox = new Inbox;
$digitzingInbox->file = json_encode($request[$files]);
$digitzingInbox->order_id = $request->order_id;
$digitzingInbox->order_name = $request->order_name;
$digitzingInbox->width = $request->width;
$digitzingInbox->height = $request->height;
$digitzingInbox->placement = $request->placement;
$digitzingInbox->format = $request->format;
$digitzingInbox->fabric = $request->fabric;
$digitzingInbox->instruction = $request->instruction;
$digitzingInbox->to_user = $users;
$digitzingInbox->from_user = Auth::user()->id;
$digitzingInbox->type = 2;
$digitzingInbox->save();
}

I think its self explanatory, that file may not exist in folder. Look it up the name if it matches.

Related

I am trying to send multiple title and multiple files into database using laravel but i am getting error

I am trying to send multiple title and multiple files into database using laravel but i am getting error please help me how can i resolve this issue ? thanks.
getting error
Cannot use object of type Illuminate\Http\UploadedFile as array
CONTROLLER
public function store(Request $request)
{
foreach ($request->title as $key => $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $request->file('audio_file_upload')->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $request->audio_file_upload[$key], $extension);
$audiodetail->title = $value;
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}
title and audio_file_upload both are different array. So you need to combine both :
public function store(Request $request)
{
$data = array_combine($request->title, $request->audio_file_upload);
foreach ($data as $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $value['audio_file_upload']->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $value['audio_file_upload'], '.' . $extension);
$audiodetail->title = $value['title'];
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}

laravel tries to store multiple images but only store of them multiple times

I've sending multiple distinct (already checked them) images to server and in laravel controller I have this:
//...
if ($request->has('images')) {
$images = [];
foreach ($request->file('images') as $image){
$img = new \App\Image;
$name = Str::slug($validated['name']).'_'.time().'.'.$image->getClientOriginalExtension();
$folder = '/uploads/images/authors/';
$image->storeAs($folder, $name, 'public');
$img->url = $folder.$name;
array_push($images,$img);
}
$author->images()->saveMany($images);
}
//...
then I look into the destination path in storage and found one of the images repeated multiple time with different names.
so what's the problem?
Make your filename destination unique.
if ($request->has('images')) {
$images = [];
foreach ($request->file('images') as $image){
$img = new \App\Image;
$name = Str::random(8).'_'.Str::slug($image->getClientOriginalName()).'.'.$image->getClientOriginalExtension();
// Debug:
logger('Received a file named '.$image->getClientOriginalName().' storing as '.$name);
$folder = '/uploads/images/authors/';
$image->storeAs($folder, $name, 'public');
$img->url = $folder.$name;
$images[] = $img;
}
$author->images()->saveMany($images);
}

Laravel 5.4 file uploading error - fileName not uploaded due to an unknown error

Im trying to upload multiple images to via below code in laravel. In my form there are 3 types of images to be selected to upload. When user select all the images and then submit the form. I need to upload all the images to same folder . First images get uploaded in to the folder. But then it gives me below error.
The file "1575738164-main-slider2.webp" was not uploaded due to an unknown error.
Controller
if ($request->hasFile('image') && $request->hasFile('image_575') && $request->hasFile('image_768')){
$file = $request->image;
$file_575 = $request->image_575;
$file_768 = $request->image_768;
$name = time().'-'.$file->getClientOriginalName();
$name_575 = time().'-'.$file_575->getClientOriginalName();
$name_768 = time().'-'.$file_768->getClientOriginalName();
$names = [ $name , $name_575 , $name_768];
foreach ( $names as $n){
$file->move('uploads/banners/',$n);
}
$banner = new Banner();
$banner->name = $name;
$banner->name_575 = $name_575;
$banner->name_768 = $name_768;
$banner -> side_color = $request -> side_color ;
$banner->type = $request->type;
$banner->save();
}
Please note that I have almost gone through below questions.
Laravel: The file was not uploaded due to an unknown error
First only using time() method won't work to generate unique file name for all three images all the time and when a concurrent request occurs.
Second:
$names = [ $name , $name_575 , $name_768];
foreach ( $names as $n){
$file->move('uploads/banners/',$n);
}
What you are looping is totally wrong. You are trying to move the same image, $file for three times.
You have to move all the three images inside the loop:
`
$file = $request->image;
$file_575 = $request->image_575;
$file_768 = $request->image_768;
`
So, you should probably do:
$filesToMoves = [$name=> $file, $name_575 => $file2 , $name_768 => $file3];
foreach($filesToMoves as $fileName => $fileToMove){
$fileToMove->move('uploads/banners/',$fileName);
}
I will add my code for future references that I used to solve this issue
public function store(Request $request)
{
$this -> validate ( request () , [
'image' => 'required|mimes:webp|dimensions:max_width=1200,max_height=380|max:50' ,
'image_575' => 'required|mimes:jpeg,png,jpg|dimensions:max_width=575,max_height=380|max:80' ,
'image_768' => 'required|mimes:jpeg,png,jpg|dimensions:max_width=768,max_height=380|max:80' ,
] ) ;
if ($request->hasFile('image') && $request->hasFile('image_575') && $request->hasFile('image_768')){
$fils = [$request->image, $request->image_575, $request->image_768];
$formats = ['webp' , '575','768'];
$fileNames = [];
$i = 0;
foreach($fils as $file){
$name = time().'_'.$formats[$i].'.'.$file->getClientOriginalExtension();
$file->move('uploads/banners/', $name);
array_push($fileNames, $name);
$i++;
}
$a= new X();
$a->name = $fileNames[0];
$a->image_575 = $fileNames[1];
$a->image_768 = $fileNames[2];
$a->save();
}
This is just for information.

Error Exception: Indirect modification of overloaded property laravel

I'm making an API that store a new version of an application. Here is my code:
public function saveNewVersion() {
$artifact_url = Input::get('url');
$artifact_id = Input::get('id');
$artifact_name = Input::get('name');
$artifact_version = Input::get('version');
$urls = explode('/', $artifact_url);
//Exploding URL to dir
$app_dir = "";
for($i=5; $i<sizeof($urls); $i++)
$app_dir .= $urls[$i] . '/';
$app_dir .= $artifact_id;
//Checking if the artifact_id exists in the database
$app = Applications::where('app_dir', '=', $app_dir);
if (!$app->count()) {
//Save it as new application
$new_app = new Applications();
$new_app->application->name = $artifact_name;
$new_app->app_dir = $app_dir;
$new_app->save();
$app_id = Applications::where('app_dir', '=', $app_dir)->first()->id;
} else {
$app_id = $app->first()->id;
//Checking if the application name is not same as newrelic (Optional)
if ($app->first()->application_name != $artifact_name) {
$app = $app->first();
$app->application_name = $artifact_name;
$app->save();
}
}
//check if the last version exists in the database
$version = Versions::where('application_id', '=', $app_id)->orderBy('id', 'desc');
$lastVersion = $version->first()->version_number;
if ($lastVersion != $artifact_version) {
//check if the new version is exists before
$flag = 0;
$versions = Versions::where('application_id', '=', $app_id)->orderBy('id', 'desc')->get();
foreach ($versions as $item) {
if ($item->version_number == $artifact_version) {
$flag = 1;
break;
}
}
if (!$flag) {
$version = new Versions();
$version->application_id = $app_id;
$version->version_number = $artifact_version;
$version->save();
echo 'Application' . $app_id . 'has been updated to version ' . $artifact_version;
}
}
}
When I call the API using postman, the API runs successfully and stores the new version of the application. But when I'm using CURL. I got ErrorException: Indirect modification of overloaded property App\Applications::$application and it points to my $new_app->save() code.
Is there any problem with my code ? Or are there some parameters used in postman that make this error invisible ?
I may be wrong here but it looks like the issue is the issue is here:
$new_app->application->name = $artifact_name;
I think it should be:
$new_app->application_name = $artifact_name;
based on the other code.

How to upload multiple images in php

I am developing a module of epaper in codeigniter(PyroCMS).
I want to know how can I upload multiple images ar once.
Can anyone guide me in uploading multiple images?
I tried but I only found code for uploading single image which I have already used in news module.
In the view file give this code for image upload:
echo form_label('Multi Images','',$label_attr);
echo form_upload($multi_photo_attr);
where
$multi_photo_attr = array(
'id' => "cat_multi_images",
'class' => "multi",
'name' => "cat_multi_images[]",
'maxlength' => "25",
'multiple' => "multiple"
);
Now you need to create a folder in the root directory where your photos will be uploaded.
After that in the controller's method you need to store the path to that folder in a variable.This variable will be used to upload the images in the folder.
Next,get the names of all the images in a array something like this:
foreach($_FILES["cat_multi_images"] as $key => $value)
{
$i=0;
foreach($value as $key1 => $value1)
{
$multi_photo_array[$i][$key] = $value1;
$i++;
}
After that for every array element,i.e.,for every image run the below code to upload it:
function UploadFile($files,$path)
{
$extensions = array('jpeg','JPEG','gif','GIF','png','PNG','jpg','JPG','pdf','PDF','ZIP','zip','rar','RAR','html','HTML','TXT','txt','doc','docx','DOC','DOCX','ppt','PPT','pptx','PPTX','xlsx','XLSX','xls','XLS','exe','EXE','mp3','MP3','wav','WAV','m4r','M4R','mpeg','MPEG','mpg','MPG','mpe','MPE','mov','MOV','avi','AVI',);
$destination = $path.$files["name"];
//print_r($destination);exit;
// GET FILE PARTS
$fileParts = pathinfo($files['name']);
$file_name = $files['name'];
$file_name_only = $fileParts['filename'];
$file_name_only = preg_replace('/[^a-zA-Z0-9]/','',$file_name_only);
$file_extention = $fileParts['extension'];
$Count = 0;
$destination = $path.$file_name_only.".$file_extention";
$file_name = $file_name_only.".$file_extention";;
// THIS SHOULD KEEP CHECKING UNTIL THE FILE DOESN'T EXISTS
while( file_exists($destination))
{
$Count += 1;
$destination = $path. $file_name_only."-".$Count.".$file_extention";
$file_name = $file_name_only."-".$Count.".$file_extention";
}
$fileextension='';
$filename='';
if(!empty($files))
{
$filename=$files['name'];
$fileextension=substr($filename,strpos($filename,".")+1);
if(in_array($fileextension,$extensions))
{
$uploadstatus=move_uploaded_file($files["tmp_name"],$destination);
if($uploadstatus)
{
return $file_name;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
Just copy the above code.It should work as it is made for a general case by me!You can copy that code in your model file and call it in the controller like this :
$pr_photo_data = $this->admin_model->UploadFile($value,$targetPath_images);
$photo_list[] = $pr_photo_data;
And then store every image in the database
foreach($photo_list as $image)
{
$pro_image["cat_multi_images"] = $image;
$pro_retId = $this->admin_model->add_multipic_cat($pro_image);
}
where
function add_multipic_cat($data)
{
$retId = $this->database->query_insert("photo", $data);
return $retId;
}
Be careful.Take care and do every step accurately
check this one
https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload,---Multi-file-upload-with-CodeIgniter
Struggling To Use PyroCMS Files Library To Upload Multiple Files

Resources