How to upload image from one project to another project laravel - php

I have two laravel project i use code in my Myfirstproject to upload to Mysecondproject public\src\img\upload directory.
I try these code in Myfirstproject:
if ($request->hasFile('images')) {
$destinationPath='Mysecondproject\public\src\img\upload';
if ($files = $request->file('images')) {
foreach ($files as $file) {
$name = $file->getClientOriginalName();
$file->move($destinationPath, $name);
$images[] = $name;
}
}
}
But it's not working, any solution for these?

Please try absolute path in $destinationPath as C:\Mysecondproject\public\src\img\upload or D:\Mysecondproject\public\src\img\upload.

Related

How to upload multiple files using multiple input filed in Laravel 7?

I'm working on HRMS project, In that I'm saving employees data, Storing like ID Proof, Resume etc. But while uploading it gives me error as in screenshot.
Code:
$files= [];
if($request->hasfile('resume'))
{
$files[] = $resumeFilename;
}
if($request->hasfile('profile_photo'))
{
$files[] = $imagefilename;
}
if($request->hasfile('id_proof'))
{
$files[] = $id_proof;
}
if($request->hasfile('prevEmpType'))
{
$files[] = $prevEmpType;
}
if($request->hasfile('offer_letter'))
{
$files[] = $offer_letter;
}
if($request->hasfile('con_a_agree'))
{
$files[] = $con_a_agree;
}
foreach ($files as $file)
{
$file->move('uploads/' , $file); //Error is here i.e "Call to a member function move() on string"
}
Uploading single file from each input type file field as in below screenshot.
Error:
How to upload file in folder at once from multiple input filed?
You can use Storage facade to save a file data
$files= [];
if($request->hasfile('resume')) $files[] = $resumeFilename;
if($request->hasfile('profile_photo')) $files[] = $imagefilename;
if($request->hasfile('id_proof')) $files[] = $id_proof;
if($request->hasfile('prevEmpType')) $files[] = $prevEmpType;
if($request->hasfile('offer_letter')) $files[] = $offer_letter;
if($request->hasfile('con_a_agree')) $files[] = $con_a_agree;
// save the files
foreach ($files as $file) {
$filename = sprintf('%s.%s', md5(\Str::random(5)), $file->getClientOriginalExtension());
Storage::disk('uploads')->putFile($file, $filename);
}
You can get the file response with
public function foo(string $filename)
{
return response()->file(storage_path("app/uploads/$filename"));
}
Please read this reference about using File Storage in Laravel

Error downloading all files of a directory using Laravel s3 with ZipArchive

I am trying to download all files of a directory from digital ocean space that works like amazon s3. I have written the code below for the purpose:
$zip = new \ZipArchive();
$fileName = $exam->id . '.zip';
$files = Storage::disk('do')->files('answers/' . $id);
if ($zip->open(($fileName), \ZipArchive::CREATE) === TRUE) {
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile(Storage::disk('do')->url($value), $relativeNameInZipFile);
}
$zip->close();
}
return \response()->download(public_path($fileName));
It gives me an error saying:
The file "H:\development\code\aminulsacademy.com\public\5.zip" does not exist
The $files variable gives me the following data:
$files = Storage::disk('do')->files('answers/' . $id);
Output:
["answers/5/1_4.pdf", "answers/5/1_4_reviewed.pdf", "answers/5/3_4.pdf","answers/5/3_4_reviewed.pdf"]
Can anyone help me?

PHP foreach($images as $image) not uploading images

I am trying to upload multiple images to my server, however it is not working. It gives me status code 200 but it doesn't upload images.
This is what I do:
if($request->hasFile('post_image')){
$images = $request->post_image;
$i = 0;
foreach($images as $image){
$i++;
$filename = $post->id.'.'.$i.'jpg';
$location = '/var/www/site/html/'.$post->id;
$image->move($location, $filename);
}
}
If I am removing the foreach() then it uploads image but only one.
Why it is not uploading them? What I am doing wrong?
Edit:
Answering my own question here also. The problem was that my key "post_image" had to be "post_image[]" instead.
You forgot to add . before 'jpg' extension in your filename:
Should be like this:
$filename = $post->id.'.'.$i.'.jpg';
I am trying this way upload multiple images:
if(!empty(Input::file('image'))){
$files= Input::file('image');
$destinationPath= 'images';
$images=array();
foreach($files as $file){
$fullname= $file->getClientOriginalName();
$hashname = $fullname;
$upload_success =$file->move($destinationPath, $hashname);
$images[]=$fullname;
$has= implode(",",$images);
}
$categories = new CategoryType;
$categories->name = Input::get('name');
$categories->image_attachment = $has;
$categories->save();
}
Follow this awnser you can loop on the all uploaded files
Get all files in a foreach loop in Laravel
like $images = Input::file('post_image');
The problem was that my key "post_image" had to be "post_image[]" instead.
$file_ary = array();
$file_count = count($request->file('image') );
$a=($request->file('image'));
$finalArray=array();
for ($i=0; $i<$file_count; $i++) {
$fileName = time().$a[$i]->getClientOriginalName();
$destinationPath = $request->input('path') ;
$finalArray[$i]['image']=$destinationPath.$fileName;
$a[$i]->move($destinationPath,$fileName);
}
return json_encode($finalArray);

Deleting ".part" files from folder with PHP

I'm using the following to delete all files from the specified directory.
$files = glob('path/to/temp/*');
foreach($files as $file){
if(is_file($file))
unlink($file);
}
It removes everything other than partially uploaded files eg : myfile.mp3.part
I've tried specifying .part in the file path just to see if I can force it that way :
$files = glob('path/to/temp/*.part');
But that doesn't work either.
Am I missing something here? Is there a different method for deleting non-active partial files?
$files = scandir('/path/to/temp');
foreach($files as $key => $file) {
if ( preg_match('/.*?\.part$/', $file) ) {
unlink($file);
}
}
I'm using something likes this to delete all files in a folder.
$dir = "/path/to/temp";
$files = scandir($dir);
foreach($files as $file){
$path = $dir."/".$file;
if(is_file($path)) unlink($path);
}

Laravel Image Upload Issue

I am trying to upload an image using Laravel it is working to the point that the images are being successfully placed into the destination folder but when I try to view them within my IDE I am told they are corrupt and that netbeans cannot open them.
I am wondering if my upload process is causing this?
This is my code:
public function handleCreate(){
$book = new Book;
$book->title = Input::get('title');
$book->desc = Input::get('desc');
//Img uploading...
$destinationPath = '';
$filename = '';
if(Input::hasFile('cover')){
$file = Input::file('cover');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . "_" . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$book->cover = $filename;
$book->save();
return Redirect::route('books')->with('msg', 'test');
}
Any help is much appreciated. I know the image is not corrupt before uploading as I can view on my desktop. The issue is only after uploading it.

Categories