Results 1 to 4 of 4

Thread: TMP File to MySQL?

  1. #1
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TMP File to MySQL?

    Basically, I've learned PHP, XML, HTML, CSS, Javascript etc however I never got down to learning MySQL and kind of just forgot about it. However a few days ago I got a client asking if I could make them a 'Video Host' so certain people could upload videos. (Much like YouTube.) She wanted Search etc and sadly, that needs MySQL. I had to reject her simply because I was to lazy to learn it. Now I'm learning it I'm completely stumped!

    I decided to throw myself in the deep end and try to something like what she requested, so far I've achieved being able to log information in a database and put the file (movie.mp4) in a directory. However now I'd like to store the files in the directory too so I don't use a unneeded amount of server space. The code I currently have to upload the file and and store the information in the database (and the file in its directory) is as follows.

    PHP Code:
    if(isset($_POST['submit']))
    {
        
    $name $_FILES['file']['name'];
        
    $temp $_FILES['file']['tmp_name'];
        
        
    move_uploaded_file($temp,"uploaded/".$name);
        
    $url "uploaded/$name";
        
    mysql_query("INSERT INTO `videos` VALUE ('','$name','$url')");

    I know it's not the nicest looking code, but for now I'd rather get it working then mess with how it looks. Anyway.

    I was wondering if they're was anyway to move the TEMP file into the database? much like how you do it with the NAME of the file. Any help with this is much appreciated as I've been stuck at this since Yesterday.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Typically, storing files inside databases is generally advised against. I could go through a few reasons, but you might as well just check out the quesion here (you'll find reasons why not to store files inside databases, but also solutions if you decide to dig yourself into that whole).

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    342
    Thanks
    5
    Thanked 33 Times in 31 Posts

    Default

    I have something that allows you to upload pictures (assuming you could do the same thing with videos)

    index.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Untitled Document</title>
    </
    head>

    <
    body>

    <
    form action="upload_rename_ac.php" method="post"
    enctype="multipart/form-data">
    <
    label for="file">Filename:</label>
    <
    input type="file" name="file" id="file"><br>
    <
    input type="submit" name="submit" value="Submit">
    </
    form
    ></body>
    </
    html
    upload_rename_ac.php
    PHP Code:
    <?php
    $random_digit_one
    =rand(0,99999999);
    $random_digit_two=rand(0,99999999);
    $random_digit_fin=rand(0,99999999);
    $allowedExts = array("gif""jpeg""jpg""png"); //Change this to video files
    $temp explode("."$_FILES["file"]["name"]);
    $extension end($temp);
    if (((
    $_FILES["file"]["type"] == "image/gif"/*Change file type*/
    || ($_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/jpg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg")
    || (
    $_FILES["file"]["type"] == "image/x-png")
    || (
    $_FILES["file"]["type"] == "image/png"))
    && (
    $_FILES["file"]["size"] < 20000000/* change size */
    && in_array($extension$allowedExts))
      {
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["file"]["error"] . "<br>";
        }
      else
        {
        echo 
    "Upload: " $_FILES["file"]["name"] . "<br>";
        echo 
    "Type: " $_FILES["file"]["type"] . "<br>";
        echo 
    "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo 
    "Temp file: " $_FILES["file"]["tmp_name"] . "<br>";

        if (
    file_exists("upload\\" $_FILES["file"]["name"]))
          {
          echo 
    $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["file"]["tmp_name"],"upload\\" $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"]);
          echo 
    "Stored in: " "upload\\" $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"];
         
    // echo '<div><image src="upload\\' . $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"].'" width="50%" /></div>';
          
    echo '<a href="index.php">Return</a>';
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>
    This shall uploading it. If you wanna add something to match the name with the video type and just throw the video name with the video uploaded i think that shall work. However, its late for me so i cant really check right now.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Often this means your /tmp partition has run out of space and the file can't be created, or for whatever reason the mysqld process cannot write to that directory because of permission problems. Sometimes this is the case when selinux rains on your parade.

    Any operation that requites a "temp file" will go into the /tmp directory by default. The name you're seeing is just some internal random name.

Similar Threads

  1. Help with file using PHP and MYSQL
    By Rob (SA) in forum PHP
    Replies: 0
    Last Post: 04-14-2012, 06:02 AM
  2. Load mysql from .dat file
    By umbongo in forum MySQL and other databases
    Replies: 4
    Last Post: 01-17-2012, 01:55 AM
  3. Contacts from mysql into a pdf file
    By ryokale in forum PHP
    Replies: 0
    Last Post: 12-20-2008, 07:47 PM
  4. import cvs file to mysql
    By ddun2301 in forum MySQL and other databases
    Replies: 1
    Last Post: 11-28-2008, 06:46 PM
  5. MySQL data to XML file
    By pavmoxo in forum PHP
    Replies: 5
    Last Post: 03-04-2007, 01:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •