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');
Related
$sql = DB::table('laravel_products')
->insert(array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
));
$lastInsertedID = $sql->lastInsertId();
When I try to insert its responses:
"Call to a member function lastInsertId() on bool"
I used insertGetId but its cant save multiple rows of pictures on mysql.
If you want to get the last inserted ID like that you can call that method on the PDO instance directly:
$id = DB::getPdo()->lastInsertId();
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
from : https://laravel.com/docs/5.8/queries#inserts
$data = new LaravelProducts(); //LaravelProducts is your Model Name
$data->name= $name; //here 'name' is your column name
$data->price= $price; //here 'price' is your column name
$data->qty= $qty; //here 'qty' is your column name
$data->description= $description; //here 'description' is your column name
..........
..........
$data->image= $image; //here 'image' is your column name
$data->save();
$lastInsertedId = $data->id;
You don't have to write a new query to collect last inserted id from database.
$laravel_product = DB::table('laravel_products')
->insertGetId( array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
)
);
foreach ($filenamesToSave as $filename) {
DB::insert('INSERT INTO laravel_products_images ( product_id, filename ) VALUES ( ?, ? )',[$laravel_product->id, $filename]);
return view('createproduct');
} // Foreach Closing
// Echo your inserted ID Like Below
echo $laravel_product->id;
It should be 100% working for you.
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.'"'
));
}
I have angular app as frontend and laravel 5.2 as api backend. My angular app send parameter to my laravel controller this:
{
name: "My Name",
email: "example#email.com",
subject: "Hello",
message: "This My Message",
attachment: {
base64: /9j/4AAQSkZJRgABAQEASABIAAD/2wCEAAMCAgMC.....
filetype: "application/rar",
filename: "example.rar",
filesize: 198141,…
}
}
And in my laravel controller i have code like this
$data = [
'name' => $request->input('name'),
'email' => $request->input('email'),
'subject' => $request->input('subject'),
'message' => $request->input('message'),
'attachment' => $request->file('attachment'),
'store' => 'Store Name',
];
Mail::send('emails.call-us', ['data' => $data],
function ($m) use ($data) {
$m->from($data['email'], $data['name'] . ' - ' . $data['store']);
$m->attach($data['attachment'], [as => 'example.rar', ['mime' => 'application/rar']);
$m->to(env('EMAIL_CONTACT_US'));
$m->subject($data['subject']);
});
What is the recommended way of send file as attachment email from base64 format? Thanks
Ok i solved my problem and it's easy ...
Just changed attach to attachData
$data = [
'name' => $request->input('name'),
'email' => $request->input('email'),
'subject' => $request->input('subject'),
'message' => $request->input('message'),
'attachment' => $vAttachment,
'store' => $vStore->name,
];
Mail::send('emails.call-us', ['data' => $data],
function ($m) use ($data) {
$m->from($data['email'], $data['name'] . ' - ' . $data['store']);
$m->attachData(base64_decode($data['attachment']['base64']), $data['attachment']['filename'], ['mime' => $data['attachment']['filetype']]);
$m->to(env('EMAIL_CONTACT_US'));
$m->subject($data['subject']);
Silly me :)
I'm trying to POST multipart and json data with Guzzle to build my apps with Phonegap Build API. I've tried many adjustment but still got error results. Here's the latest function I'm using:
public function testBuild(Request $request)
{
$zip_path = storage_path('zip/testing.zip');
$upload = $this->client->request('POST', 'apps',
['json' =>
['data' => array(
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
)],
'multipart' =>
['name' => 'file',
'contents' => fopen($zip_path, 'r')
]
]);
$result = $upload->getBody();
return $result;
}
This is my the correct curl format that has success result from the API, but with file I have in my desktop:
curl -F file=#/Users/dedenbangkit/Desktop/testing.zip
-u email#email.com
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
https://build.phonegap.com/api/v1/apps
As mentioned before, you cannot use multipart and json together.
In your curl example it's just a multipart form, so use the same in Guzzle:
$this->client->request('POST', 'apps', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($zip_path, 'r'),
],
[
'name' => 'data',
'contents' => json_encode(
[
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
]
),
]
]
]);
How make post request with GuzzleHttp( version 6.0 ). I am trying do the following and getting error
i'm get the image value
Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1.53mb.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1607671
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => C:\wamp\tmp\php32BB.tmp
[fileName:SplFileInfo:private] => php32BB.tmp
)
here i'm using guzzlehttp using to upload the image
$response = $this->client->request('POST', url('/update_profile'), [
'multipart' => [
[
'name' => 'foo',
'contents' => fopen('C:\wamp\tmp\php32BB.tmp', 'r'),//this path is image save temperary path
]
]
]);
Now i get the error
fopen(C:\wamp\tmp\php17BC.tmp): failed to open stream: No such file or directory.
How to use the contents in multipart
i solved this issue
$image_path = $post_array['image']->getPathname();
$image_mime = $post_array['image']->getmimeType();
$image_org = $post_array['image']->getClientOriginalName();
$response = $this->client->post(url('/update_profile'), [
'multipart' => [
[
'name' => 'image',
'filename' => $image_org,
'Mime-Type'=> $image_mime,
'contents' => fopen( $image_path, 'r' ),
],
]
]);