Fisierele trimise pentru upload pot fi preluate cu metoda file() sau folosind proprietati dinamice ($request->field_name).
$file = $request->file('photo');
//Or:
$file = $request->photo;

Verfiare daca e fisier pentru upload

Puteti verifica daca un fisier este trimis spre incarcare folosind metoda hasFile().
In afara de a verifica daca fisierul este prezent, puteti sa verificati daca fisierul a fost trimis cu succes, folosind metoda isValid()
if($request->hasFile('photo') && $request->file('photo')->isValid('photo')){
  //
}

Instanta clasa UploadedFile

Metoda file() returneaza o instanta a clasei Illuminate\Http\UploadedFile (care extinde clasa PHP SplFileInfo) si furnizeaza o varietate de metode pentru a inspecta si interactiona cu fisierul.
Pentru mai multe informatii despre aceste metode, vedeti: Metode clasa UploadedFile

- Exemplu, script Laravel simplu pentru incarcare imagini, cu setari pentru marimea maxima permisa a fisierului si validarea extensiei.

1. Creati un director "uploads" in directorul "public/".
2. Adaugati urmatorul cod intr-un fisier "UploadController.php" (in directorul app/Http/Controllers/):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class UploadController extends Controller {
  protected $exts =['bmp', 'gif', 'jpe', 'jpeg', 'jpg', 'png']; //allowed file exttensions
  protected $max_size =0.5; //allowed maximum file size in MB
  protected $dir ='uploads'; //folder for uploaded files (in public/ directory)
  protected $err =[]; //store errors

  //Responds to post requests to /upload/photo
  //receives the $request instance
  //return string
  public function uploadPhoto(Request $request){
    //if file is sent
    if($request->hasFile('photo')){
      $file = $request->file('photo');

      //if file successfully received
      if($file->isValid()){
        //validate the file
        $ext = $file->guessExtension();
        $size = $file->getClientSize();
        if(!in_array($ext, $this->exts)) $this->err[] ='File extension: '. $ext .'<br>Allowed file exttensions: '. implode(', ', $this->exts);
        if($size >$this->max_size*1024*1024) $this->err[] ='File extension: '. $size .'<br>Allowed maximum file size: '. $this->max_size .' MB';

        //if no error, move the file
        if(count($this->err)==0){
          $fname = $file->getClientOriginalName();
          $fmoved = $file->move(base_path() .'/public/'. $this->dir, $fname);

          //if successfully moved in uploads directory
          if($fmoved){
            return 'File successfully uploaded:<br>'. $this->dir .'/'. $fname .'<br><img src="/'. $this->dir .'/'. $fname .'" alt="Photo" />';
          }
          else $this->err[] ='Unable to move the file to: '. $this->dir .'/'. $fname;
        }
      }
      else $this->err[] ='The file not received';
    }
    else $this->err[] ='No photo file';

    if(count($this->err) >0) return implode('<br>', $this->err);
  }
}
3. Adaugati urmatorul cod intr-un fisier "upload_form.blade.php" (in directorul "resources/views/" ):
<h4>Upload Photo<h4>
<form method="post" action="/upload/photo" enctype="multipart/form-data">
{{ csrf_field() }}
File: <input type='file' name='photo'/><br>
<input type='submit' value='Upload'/>
</form>
4. Adaugati acest cod in fisierul "routes/web.php":
//shows the upload form
Route::name('upload')->get('/upload', function(){
  return view('upload_form');
});

//when the upload form is submited
Route::post('/upload/photo', 'UploadController@uploadPhoto');
5. Accesati urmatoarea adresa URL si selectati un fisier pentru incarcare:
//localhost:8000/upload
- Va afisa o pagina ca in aceasta imagine:
laravel upload form
6. Apasati pe butonul Upload, va deschide o pagina cu imaginea incarcata, cum se vede in imaginea urmatoare:
laravel uploaded file

Un alt exemplu de incarcare imagine este la aceasta pagina: //marplo.net/php-mysql/laravel-validare#anc_pex
- Formularul de incarcare contine campuri pt. Titlu si Descriere, in plus foloseste modelul avansat de validare Laravel.

O alta metoda de a salva fisierele incarcate pe server este sa folositi functia store('folder') sau storeAs('folder', 'file_name.ext').
Aceste metode stocheaza fisierele intr-un director specificat (folder) in directorul "storage/app".
Metoda store() va genera automat un ID unic pentru numele fisierului.
Metoda storeAs() primeste un al doilea argument pentru numele fisierului.
- Exemplu:
//store the uploaded 'photo' file in storage/app/uploads/

//with a unique generated name
$path = $request->photo->store('uploads');
echo $path; // uploads/wAwyYljmEkjHaucksuHM2luPiyUMNApmKrt1L3LK.png

//with the original file name
$fname = $request->photo->getClientOriginalName();
$path = $request->photo->storeAs('uploads', $fname);
echo $path; // uploads/image.png

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Care tag adauga rand nou in paragraf?
<b> <br> <p>
Prima linie ...<br>
Alta linie...
Ce proprietate CSS seteaza spatiu dintre litere?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
Ce functie obtine accesul la un element HTML cu un anumit ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Clic pe instructiunea "echo" utilizata corect.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Adresa URL: http://CoursesWeb.net";
Care din urmatoarele expresii cu "paint" (a picta) se foloseste pentru forma Negativa?
will paint not paint have painted
She does not paint that landscape.
- Ea nu picteaza acel peisaj.
Care din urmatoarele expresii cu "cantar" (a canta) se foloseste pentru propozitie Negativa?
ir a cantar cantaba no cantar
Ella no canta esa canciĆ³n.
- Ea nu canta acel cantec.
Laravel - Upload Fisiere

Last accessed pages

  1. Backgammon (7920)
  2. Trecut perfect continuu - Exercitii si teste incepatori (638)
  3. Flash Chess 3 (3421)
  4. Oriental Marble Blast (749)
  5. Zuma Deluxe (9123)

Popular pages this month

  1. Bubbles3 (5882)
  2. Gramatica limbii engleze - Prezentare Generala (5174)
  3. Butterfly Kyodai (3430)
  4. Jocuri Mahjong (2372)
  5. Cursuri si Tutoriale: Spaniola, Engleza, HTML, CSS, Php-Mysql, JavaScript, Ajax (2220)