我收到了insert.php
个文件:
<?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! :(";
}
?>
我的form.php
文件:
<form action="insert.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
当我想上传图片时出现错误:
“警告:file_get_contents()[function.file-get-contents]:/ myDomain.com/insert.php中的文件名不能为空”
更新
我把它缩短为:
<?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! :(";
?>
是添加到数据库中,但不是二进制形式。
答案 0 :(得分:0)
始终添加检查以确保您确实获得了某些内容,
您可以使用is_uploaded_file试试这样:
<?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";
}
?>
答案 1 :(得分:0)
在大多数情况下,您不希望将文件存储在数据库中。你只想保存路径。
如果这是你想要的,这个脚本应该这样做。
// 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! :(";
}