this is my Class :
<?php
namespace App\Functions;
use http\Env\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class UploadFile
{
public function UploadFile($root,Request $request)
{
if($request->file('file')!=null){
if(Storage::put($root.'/'.$request->file('file')->getClientOriginalName(), $request->file('file'))) return response()->json('true');
return response()->json('false');
}
}
}
My code uploads the file but puts the file name on a folder name
How to fix this problem?
enter image description here
I am going to assume that "puts the file name on a folder name" means the file is saving as if it were a folder. This would be because you are missing the file extension. You can use $request->file('file')->getClientOriginalOriginalExtension() to get this.
Your updated code would be:
<?php
namespace App\Functions;
use http\Env\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class UploadFile
{
public function UploadFile($root,Request $request)
{
if ($request->file('file') != null) {
if (Storage::put($root . '/' . $request->file('file')->getClientOriginalName() . '.' . $request->file('file')->getClientOriginalExtension(), $request->file('file'))) return response()->json('true');
return response()->json('false');
}
}
}
Related
I have a custom class in App/Helpers/regexValidation.php:
<?
namespace App\Helpers;
class RegexValidation
{
public static function isCVC($str)
{
$re = '/^[0-9]{3,4}$/s';
preg_match($re, $str, $matches);
return $matches;
}
}
I try to use it in controller:
<?php
namespace App\Http\Controllers;
use App\Helpers\RegexValidation;
public function detectfile(Request $request)
{
$cvc = RegexValidation::isCVC(555);
}
When I call this method from route Laravel I get:
Error: read ECONNRESET
If comment this line $cvc = RegexValidation::isCVC(555); it works.
What do I do wrong?
I think you are having a PSR4 Error,
I have a custom class in App/Helpers/regexValidation.php
Rename that file to PascalCase as RegexValidation.php
after renaming run
composer dumpautoload
And it should work.
I would like ot integer an externe class with two functions into my laravel project. This externe class serves for e-payment.
I created a new folder in \App named xxx then my php file named xxx.php.
My path is
\App\xxx\xxx.php
When i would like to call this class in XController using this code :
<?php
namespace App\Http\Controllers;
use App\xxx\xxx;
class XController extends Controller
{
public function Send(Request $request){
$function = new xxx;
};
}
A got an Error : Class 'App\xxx\xxx' not found
my xxx.php code is like that :
<?php
namespace App;
class xxx
{
public function function($data)
{
return ;
}
}
My route :
Route::get('/send', 'XController#Send');
Thank you in advance !
Correct the namespace in your class file Xxx.php
<?php
namespace App\Xxx;
class Xxx
{
public function function($data)
{
return ;
}
}
and run this command in your console
composer dump-autoload
Laravel uses PSR-4 autoloading, so you should ensure that your classes and folders are capitalised. You can read up more on PSR-4 here - https://www.php-fig.org/psr/psr-4/
In your case the folder should be Xxx instead of xxx and your file should be called Xxx instead of xxx.
At the same time you also need to update the namespace on the Xxx.php file:
<?php
namespace App\Xxx;
class Xxx
{
public function function($data)
{
return ;
}
}
Then within your controller you can update your use statement and the method.
<?php
namespace App\Http\Controllers;
use App\Xxx\Xxx;
class XController extends Controller
{
public function Send(Request $request){
$function = new Xxx;
}
}
I hope this helps.
in xxx.php file namespace should be like this :
namespace App\folderName; // App\xxx;
and then run
php artisan config:cache;
This Code save image in only one "folder-one". I want to upload the
image at the same time in two different folders, now it saving in folder-one
example
"folder-one"
and
"folder-two"
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Mail;
use Illuminate\Auth\Events\Registered;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'photo_jpeg' => 'required|image|mimes:jpeg,png,jpg|max:2048',
]);
}
protected function create(array $data)
{
$photo_jpeg= time() . '.' . $data['photo_jpeg']->getClientOriginalExtension();
$data['photo_jpeg']->move(base_path() . 'public/folder-one', $photo_jpeg);
return user::create([
'photo_jpeg' => $photo_jpeg,
]);
}
Use method copy() to make a copy of the file at the new destination.
$request->file('photo')->move($destination_path, $file_name); //original
copy($destination_path.$file_name, $new_path.$file_name); //backup
You can't run move() twice for the same file because as it name says, it moves the file, so on the second run, the original file won't exist anymore.
You must copy the file:
$uploadPath = public_path('folder-one/');
$file = $data['photo_jpeg'];
$photo_jpeg= time() . '.' .$file->getClientOriginalExtension();
$file->move($uploadPath,$photo_jpeg);
\File::copy($uploadPath.$photo_jpeg,public_path('folder-two/').$photo_jpeg);
I made an API with Laravel and it is registering the data correctly
Now I'm trying to get this to trigger registration confirmation emails, however these are not going
I'm using Mailtrap for the tests
In .env I put like this:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=291ac7fdcf52bb
MAIL_PASSWORD=610b2e5e9782d3
No Http/Mail/DataManifestMail.php tenho:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class DataManifestMail extends Mailable
{
use Queueable, SerializesModels;
protected $inputs;
public function __construct($inputs)
{
$this->inputs = $inputs;
}
public function build()
{
return $this->view('mails.data');
}
}
In views/mails/data.blade.php I only have one warning message:
<h1>Test e-mail</h1>
Then in my Http/Controller/ManifestationController.php I put it like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Manifestation;
use App\Mail\DataManifestationMail;
use Mail;
class ManifestationController extends Controller
{
private $manifestation;
public function __construct(Manifestation $manifestation)
{
$this->manifestation = $manifestation;
}
public function store(Request $request)
{
$nr = Manifestation::max('nrmanifestation');
$this->manifestation->nrmanifestation = $nr + 1;
$this->manifestation->dspass = $request->dspass;
$this->manifestation->eeemail = $request->eeemail;
$this->manifestation->address = $request->address;
$this->manifestation->name = $request->name;
$this->manifestation->latitude = $request->latitude;
$this->manifestation->longitude = $request->longitude;
$this->manifestation->save();
Mail::to('vazag#c1oramn.com')->send(new DataManifestationMail());
return response()->json([
'result' =>
$this->manifestation->nrmanifestation
]);
}
}
I have reread the documentation and my code several times and have not found any errors
What can it be?
There might be multiple reasons:
configuration is cached (use php artisan config:cache)
you used invalid mailtrap data (you should have log in laravel.log file)
your server has somehow blocked 2525 port
Also looking at your code it seems you miss passing $input parameter to constructor. You have:
public function __construct($inputs)
but you run:
->send(new DataManifestationMail());
without passing any value
I have created app/Http/helpers.php
if (!function_exists('getLocation')) {
function getLocation($request)
{
return 'test';
}
I have added files section in composer.json autoload
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/helpers.php"
]
},
Here is my controller :
namespace App\Http\Controllers;
use App\Jobs\ChangeLocale;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Log;
class HomeController extends Controller
{
public function index(Request $request)
{
$data['location'] = getLocation($request);
}
}
When I call the function in controller as getLocation($request); it is saying "Call to undefined function App\Http\Controllers\getLocation()"
This is working fine in my local , but not on remote server. What am I missing in my remote server. Tried composer install and composer dump-autoload.
UPDATE:
The helper file is not getting listed in vendor/composer/autoload_files.php
On server, you need to execute:
composer dumpautoload
because it is not found on vendor/autoload.php
Try like this,
create Helpers directory inside the app directory.
create a Example.php file inside the Helpers directory
create a alieas for this file at config/app.php file
Ex: 'Example' => App\Helpers\Example::class,
The code in the Example.php like follows,
<?php
namespace App\Helpers;
class Example
{
static function exampleMethod()
{
return "I am helper";
}
}
Using the above Example helper in Controller files like follows,
<?php
namespace App\Http\Controllers;
use App\Helpers\Example;
class HomeController extends Controller
{
public function index(Request $request)
{
return Example::exampleMethod();
}
}
Using the above Example helper in blade view files like follows,
<div>{{ Example::exampleMethod()}}</div>
This will help you to get solution.
Make Sure the structure of the Helper.php is correct...
<?php
//no namespace, no classes
//classes are not helpers
if ( !function_exists('nextStage') ) {
function nextStage($currentStage) {
//if you need to access a class, use complete namespace
return \App\Models\Stage::where('id',$currentStage)->first()->next_stage;
}
}
More help on Laracast found here