Skip to content

Instantly share code, notes, and snippets.

@haric
Created May 20, 2012 18:35
Show Gist options
  • Select an option

  • Save haric/2759064 to your computer and use it in GitHub Desktop.

Select an option

Save haric/2759064 to your computer and use it in GitHub Desktop.
Upload file to db
<?php
ob_start();
ini_set ("display_errors", 1);
if (isset($_POST["submit"])) {
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$c = mysqli_connect("localhost" , "root", "", "mcdb");
$sql = "REPLACE INTO photo (name, size, type, content ) VALUES('$fileName', '$fileSize', '$fileType', '$content')";
$rc = mysqli_query($c, $sql);
mysqli_close($c);
ob_end_flush();
ini_set("display_errors", 0);
exit();
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
ob_end_flush();
ini_set("display_errors", 0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment