M.Stark
M.Stark

Reputation: 37

PHP Image upload to server, and save path to the PostgreSQL database

I'd like to modify my form to upload files to the server, with the full path of the uploaded file saved to the database.

Code:

//EDYCJA AGENTA

    function edycja_agenta($id)

    {

         $q = "SELECT id, ostatnie_logowanie, profil_id, adres_ip, status, lokalizacja, adres_mac, wersja_programu FROM agenty WHERE id=$id";

        $wynik =  pg_query($q) or die("Zapytanie niepoprawne:".$q);

        $ilosc=pg_numrows($wynik);



         $i=0;

while ($i < $ilosc) {

        $id=pg_result($wynik,$i,"id");

        $lokalizacja=pg_result($wynik,$i,"lokalizacja");

        $ostatnie_logowanie=pg_result($wynik,$i,"ostatnie_logowanie");

        $profil_id=pg_result($wynik,$i,"profil_id");

        $adres_ip=pg_result($wynik,$i,"adres_ip");

        $status=pg_result($wynik,$i,"status");

        $adres_mac=pg_result($wynik,$i,"adres_mac");

        $wersja_programu=pg_result($wynik,$i,"wersja_programu");  

        $i++;

  }

    echo'



<form  action="agent.php?id='.$id.'&zapisz" method="post" name="formularz">







    <div class="control-group">
    <label class="control-label">Lokalizacja</label>
    <div class="controls">
      <input type="text" name="lokalizacja" required value="'.$lokalizacja.'" />
      <p class="help-block"></p>
    </div>
  </div>



    <label class="control-label" >Profil</label>

    <div class="controls">

            <select class="formularz_select" name="profil_nazwa" id="lista">';

            echo pobierz_profile($profil_id);

            echo ' </select>

    </div>







 <button type="submit" class="btn btn-primary">Zapisz dane</button>









</form>



';

    }



//KONIEC EDYCJA AGENTA





  //ZAPIS EDYCJA AGENTA

    function zapisz($id)

    {





         if (isset($_POST['profil_nazwa']) ) 

        $profil_nazwa= $_POST['profil_nazwa'];

        if (isset($_POST['lokalizacja']) ) 

        $lokalizacja= $_POST['lokalizacja'];





if( isset($_POST['lokalizacja'])  AND isset($_POST['profil_nazwa'])) {  

         $zapytanie      =  "SELECT id FROM profile WHERE nazwa='$profil_nazwa'";

         $odpowiedz     =  pg_query($zapytanie) or die("Zapytanie niepoprawne:".$zapytanie);

         $id_profilu    =  pg_result($odpowiedz,0,"id");





            $q = "UPDATE agenty SET profil_id='$id_profilu', lokalizacja='$lokalizacja', status='1'  WHERE id=$id";

            $wynik =  pg_query($q) or die("Zapytanie niepoprawne 1:".$q);

           if($wynik) {  $GLOBALS[komunikat_edycja_agenta]="<div id=\"myAlert\" class=\"alert alert-success fade\"><a class=\"close\" data-dismiss=\"alert\">×</a>Edycja zakończona sukcesem</div>";

           } else {       $GLOBALS[komunikat_edycja_agenta]="<div id=\"myAlert\" class=\"alert alert-error fade\"><a class=\"close\" data-dismiss=\"alert\">×</a>Edycja zakończona błędem</div>";     

    }





}



  }



//KONIEC ZAPISU EDYCJA AGENTA  

I know that I need to use an input type file, but I have no clue on how to save the path to the database and how to specify the folder for the uploaded file.

Upvotes: 0

Views: 1575

Answers (1)

Raymond Cheng
Raymond Cheng

Reputation: 2505

there have some keys for upload file:

  1. <input type='file' name='file_name' /> must have this kind of input;
  2. <form encrypt='multipart/form-data'> form must have encrypt property
  3. get uploaded file you need use $_FILES in php server side,in this case,you could print $_FILES['file_name'] to see details,remember $_FILES array key must eq to input's name value("file_name")
  4. to store file you need use move_uploaded_file($temp_name,$save_path),$save_path must writable

to see an example,please go to here:http://www.w3schools.com/PHP/php_file_upload.asp

Upvotes: 3

Related Questions