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 creaza un text ingrosat?
<q> <strong> <em>
<p>Adresa: <strong>http://CoursesWeb.net/</strong> - Tutoriale.</p>
Care din aceste coduri CSS afiseaza textul ingrosat?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
Ce functie JavaScript apeleaza o alta functie, in mod repetat, la un anumit interval de timp?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Clic pe variabila definita corect in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Care din urmatoarele forme ale verbului "travel" (a calatori) e la timpul Prezent Perfect?
have traveled shall travel had traveled
We have traveled in space.
- Noi am calatorit (deja) in spatiu.
Care din urmatoarele forme ale verbului "pintar" (a picta) se foloseste la Trecutul Imperfect?
ir a pintar pintaba pintó
La niña pintaba un arco iris.
- Fetita picta un curcubeu.
Laravel - Upload Fisiere

Last accessed pages

  1. Gramatica limbii engleze - Prezentare Generala (89256)
  2. Trecut perfect si Trecutul perfect continuu - Present perfect and Continuous (20714)
  3. Gradele de comparatie: comparative, superlative (14880)
  4. Confusable words - Cuvinte confundabile - Test Engleza (1249)
  5. Confusable words - Cuvinte confundabile (2553)

Popular pages this month

  1. Gramatica limbii engleze - Prezentare Generala (3240)
  2. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (1498)
  3. Creare si editare pagini HTML (1243)
  4. Curs HTML gratuit Tutoriale HTML5 (1093)
  5. Prezentul simplu si continuu - Present Tense Simple and Continuous (956)