Curs Php-mysql

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
Ce tag se foloseste pentru a include fisier extern CSS in pagina web?
<body> <script> <link>
<link href="/templ/style.css" rel="stylesheet" type="text/css" />
Ce proprietate CSS seteaza marimea textului?
font-weight text-decoration font-size
h2 {
  font-size: 1em;
}
Indicati proprietate JavaScript care poate sa adauge cod HTML intr-un element.
text value innerHTML
document.getElementById("someID").innerHTML = "Continut HTML";
Clic pe functia care returneaza numarul de caractere dintr-un sir in PHP.
count() strlen() stristr()
$str = "http://CoursesWeb.net/";
$nr_chr = strlen($str);
echo $nr_chr;       // 22
Care este forma de feminin a adjectivului "white" (alb)?
white whitea whites
(In limba engleza adjectivele au aceeasi forma pt. ambele genuri)
The snow is white. - Zapada e alba.
Care este forma de feminin a adjectivului "blanco" (alb)?
blanc blance blanca
La nieve es blanca.
- Zapada e alba.
Laravel - Upload Fisiere

Last accessed pages

  1. PHP OOP - Clase, Obiecte, constructor (3274)
  2. Verbe modale - Modal verbs - CAN, COULD, MAY, MIGHT, MUST (40170)
  3. Demonstrative (865)
  4. Trecut perfect continuu - Exercitii si teste incepatori (3126)
  5. Obiectul window (909)

Popular pages this month

  1. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (4964)
  2. Curs HTML gratuit Tutoriale HTML5 (4023)
  3. Prezentul simplu si continuu - Present Tense Simple and Continuous (3338)
  4. Curs si Tutoriale JavaScript (3330)
  5. Curs PHP MySQL, Tutoriale si Scripturi PHP (3197)