I got my insert.php
file:
<?php
// Above is code for connection //
echo "succesful connection";
$image = addslashes(file_get_contents($_FILE['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `myTable` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) {
echo "Something went wrong! :(";
}
?>
my form.php
file:
<form action="insert.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
When I want to upload image i got an error:
"Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /myDomain.com/insert.php"
Update:
I shortened it to:
<?php
$content = base64_encode(file_get_contents($_FILES['image']['tmp_name']));
$sql = "INSERT INTO `myTable` (`id`, `image`) VALUES ('1', '{$content}')";
if (!mysql_query($sql)) {
echo "Something went wrong! :(";
?>
Is added to the database, but not in binary form.
Always add check to make sure you are actually getting something,
You can make use of is_uploaded_file Try it like this:
<?php
if(is_uploaded_file($_FILES['image']['tmp_name']){
echo "succesful connection, we have an image!";
//get the image name, no need for slashes
$image_name = basename( $_FILES['image']['name']);
//perform the the INSERT HERE
//........
}else{
echo "Nothing was uploaded";
}
?>
In most cases you don't want to really store the file in the database. You just want to save the path.
If that is what you want, this script should do it.
// Get destination
$destination = 'path/to/folder' . $_FILES['image']['name'];
// Upload file
move_uploaded_file($_FILES['image']['tmp_name'], $destination);
// Get name
$image_name = addslashes($_FILES['image']['name']);
// Query
$sql = "INSERT INTO `myTable` (`id`, `image`, `image_name`) VALUES ('1', '{$destination}', '{$image_name}')";
if (!mysql_query($sql)) {
echo "Something went wrong! :(";
}