Upload a photo and saving the path in the bank

Discussion in 'Design & Development' started by yannick, Mar 27, 2012.

  1. yannick

    yannick
    Expand Collapse
    Junior Member

    Joined:
    Mar 1, 2012
    Messages:
    11
    Likes Received:
    5
    As I've seen enough people with questions about how to save the image in a database, I decided to post a simple example
    1º create a file index.html
    HTML:
    <html>
    <head>
    <title>Upload</title>
    </head>
    <body bgcolor = "#FFFFFF" text = "#000000">
    <form name = "form1" method = "post" action = "upload.php" enctype = "multipart/form-data">
      <input type = "file" name = "arquivo">
      <input type = "submit" name = "Submit" value = "Enviar">
      </form>
      </body>
      </html>
    2º create a file upload.php

    PHP:
    <?php
     
    //if there is a file
    if(isset($_FILES["arquivo"])){
     
    $arquivo $_FILES["arquivo"];
     
    //directory of files
    $pasta_dir "arquivos/"
     
    //if not exist it creates a folder
    if(!file_exists($pasta_dir)){
    mkdir($pasta_dir);
    }
     
    $arquivo_nome $pasta_dir $arquivo["name"];
     
    // Upload image
    move_uploaded_file($arquivo["tmp_name"], $arquivo_nome);
     
    //connect to the database
    $cn mysql_connect("localhost""root""");
    mysql_select_db("banco");
     
    //Here the path saved in the bank photo
     
    <font face="verdana">mysql_query("INSERT INTO tabela VALUES ('', '$arquivo_nome')");mysql_close($cn); </font>
     
    }
     
    ?>
    3º list the files
    PHP:
    <?php
     
     
    //connect to the database
     
    <font face="verdana">
    $cn mysql_connect("localhost""root""");mysql_select_db("banco");
    </
    font>
     
    //select the table
     
    <font face="verdana">$sql "Select * from tabela";$query mysql_query($sql);while($row mysql_fetch_array($query)){$fotos $row["fotos"];echo "<img src=\"$fotos\">";} </font>
     
    ?>
     
  2. yannick

    yannick
    Expand Collapse
    Junior Member

    Joined:
    Mar 1, 2012
    Messages:
    11
    Likes Received:
    5
    For the nest post will be an upload more advanced
     

Share This Page