How to save a url dynamically into a database.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    How to save a url dynamically into a database.

    Hello all..

    Can someone recommend a tutorial on how to go about uploading a file (Image) and saving the url to the database? It would be great if the script could name the file also.

    There should be no user intervention in saving the url to the database and should happen by way of the script. I will have a directory on the filesystem for the images. So basically, a script that would allow me to upload an image then place that image into an upload directory; take the name of that url and save it into the db.

    I have searched the net and can only come up with hundreds of tutorials on how to stick an image into a db.. That isn't what I'm looking for. I want to put the url into the db then draw it out into a img tag to populate a page.

    Thanks!

    Frank
  • fjm
    Contributor
    • May 2007
    • 348

    #2
    Hello again.. I was able to find a good tutorial on what I was looking for so I don't need help per say with the image upload anymore but I could sure use a little clarification on OOP.

    Can someone who really understands OOP tell me if I can take a piece of code and put it into a class instead of sticking this code into my html?

    Here a piece of that script:
    Code:
    <?php
    error_reporting(E_ALL);
    if(isset($_GET['id']))
    {
    include 'library/config.php';
    include 'library/opendb.php';
    $id	 = $_GET['id'];
    $query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $filePath) = mysql_fetch_array($result);
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
     
    readfile($filePath);
    include 'library/closedb.php'; 
    exit;
    }
    ?>
    Can I use this piece of code in a constructor and call it from the class I make?

    I am trying to learn OOP and while the concept is brutally easy, putting it into practice is a different story for me. :)

    Thanks guys!

    Frank

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Frank.

      The basic idea behind OOP is to stop thinking in terms of tasks, and to start thinking in terms of objects.

      For example, when uploading a file to the database, you'll be working with:
      • An upload (which is also a file)
      • A database connection


      To implement this, you're going to want to create three classes:

      [code=php]
      class File {
      protected $myPath;

      public function __construct($pa th) {
      $this->myPath = $path;
      }

      public function __toString() {
      return $this->myPath;
      }
      }

      class Upload extends File {
      protected $myMeta;

      public function __construct($in dex) {
      $this->myMeta = $_FILES[$index];
      parent::__const ruct($this->myMeta['tmp_name']);
      }
      }
      [/code]

      For info on how to set up the Database class, check out this article.

      This is a very basic example. You'll need to create member functions that process your uploads, since otherwise PHP will delete them.

      For more information, check out this page.

      Ask questions.

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        This may also be of interest.

        Understanding Object-Oriented Programming

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #5
          Thank you guys! I will read the links you provided..


          Frank

          Comment

          • fjm
            Contributor
            • May 2007
            • 348

            #6
            Originally posted by volectricity
            This may also be of interest.

            Understanding Object-Oriented Programming
            Hey volectricity,

            Nice site you got there! I bet the visually impared love it. :)

            Seriously though. You did a great job on that! Also, thank you for a terriffic OOP article.. I am on my way now!!

            Thanks again,
            Frank

            Comment

            • pbradford
              New Member
              • Jun 2010
              • 1

              #7
              Originally posted by fjm
              Hello again.. I was able to find a good tutorial on what I was looking for so I don't need help per say with the image upload anymore but I could sure use a little clarification on OOP.

              Can someone who really understands OOP tell me if I can take a piece of code and put it into a class instead of sticking this code into my html?

              Here a piece of that script:
              [PHP] <?php
              error_reporting (E_ALL);
              if(isset($_GET['id']))
              {
              include 'library/config.php';
              include 'library/opendb.php';
              $id = $_GET['id'];
              $query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
              $result = mysql_query($qu ery) or die('Error, query failed');
              list($name, $type, $size, $filePath) = mysql_fetch_arr ay($result);
              header("Content-Disposition: attachment; filename=$name" );
              header("Content-length: $size");
              header("Content-type: $type");

              readfile($fileP ath);
              include 'library/closedb.php';
              exit;
              }
              ?> [/PHP]

              Can I use this piece of code in a constructor and call it from the class I make?

              I am trying to learn OOP and while the concept is brutally easy, putting it into practice is a different story for me. :)

              Thanks guys!

              Frank
              Frank,
              Where was that tutorial you mentioned. I am trying to accomplish the same thing in a current program I am developing but having a devil of a time trying to get my brain wrapped around it.

              PB

              Comment

              Working...