1

So I have searched some ways to download a file to mysql through a php form. This is what I found:

www.w3schools.com/php/php_file_upload.asp

but this is for uploading to a different page instead of mysql. Now I changed the code to this at the moment:

    <form 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>

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");


if(isset($_POST['submit'])) {
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if($_FILES["file"]["size"] < 20000)
  {
  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/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  }
?> 

I have no idea how to just upload it to mysql. I did google it but couldn't find anything which helped me. My database looks like this:

Can anyone explain to me how to upload my file to my database table?

5
  • 3
    may help bytes.com/topic/php/insights/… Commented Oct 14, 2013 at 11:26
  • @HaimEvgi Thank you. I will have a look at it. Though I'm still open for other answers. Commented Oct 14, 2013 at 11:28
  • look on this example the issue is read the file to variable via file_get_contents or on other method and insert the binary data to blob field Commented Oct 14, 2013 at 11:30
  • @HaimEvgi ok I'm trying it right now. Commented Oct 14, 2013 at 11:32
  • @HaimEvgi Is there any way to, for example, show the image when I uploaded a image at the part where I list all the files? Commented Oct 14, 2013 at 11:40

1 Answer 1

1

However if you want store big data in table, you need create separate table only for file data because queries to table with big blob data very slow. For example crate table 'files' with columns id(int) and data(blob) And make query to main table with params and sorting and then give only data from table 'files' by id (to main table you may add column file_id for example) Filename:

<?php
// Allowed extension for upload
$allowedExts = array("gif", "jpeg", "jpg", "png");

$link = mysqli_connect("myhost", "myuser", "mypassw", "mybd") or die("Error " . mysqli_error($link));

if (isset($_POST['submit'])) {
    $filename = $_FILES["file"]["name"];
    // Get extension with http://www.php.net/manual/en/function.pathinfo.php
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    // Check file size in bytes and for allowed extensions
    if ($_FILES["file"]["size"] < 20000 && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            // Get file size
            $size = $_FILES["file"]["size"];
            // Read file to var
            $file_data = file_get_contents($_FILES["file"]["tmp_name"]);
            // Prepare sql
            $prepare = mysqli_prepare($link, 'INSERT INTO yourtable(`name`,`type`,`size`,`content`) VALUES(?,?,?,?)');
            // bind variables for replace ? in query with types
            // s - string, i - integer, b- blob (for file data), d - for double
            mysqli_stmt_bind_param($prepare, 'ssib', $filename, $extension, $size, $file_data);
            // execute query
            mysqli_stmt_execute($prepare);
        }
    } else {
        echo "Invalid file";
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

you can improve your answer with a little description of you done :)
This is wrong: $file_data = file_get_contents($_FILES["file"]["name"]); YOu should read "tmp_name" and not "name"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.