将图像上传到数据库(获取错误通知:未定义的索引:图像)

时间:2014-03-28 04:32:39

标签: php

Html文件:

<form action="cregister.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="wizForm">
        <input type="file" name="image" id="image" value="uploads/default.png" />
        <button type="button" id="simple-post" class="btn btn-success submitBtn">Submit</button>
</form>

操作文件:

$filename = $_FILES['image']['name'];
$path = "uploads";
move_uploaded_file($_FILES['image']['tmp_name'], $path."/".$filename);
$photo = $path."/".$filename;


$insert = "INSERT INTO user (photo)VALUES ('$photo')";
$sth = $db->query($insert);

获取错误

Notice: Undefined index: image

2 个答案:

答案 0 :(得分:2)

该文件不会以这种方式取值value="uploads/default.png"

<input type="file" name="image" id="image" value="uploads/default.png" />

您需要选择文件并提交表单

print_r($_FILES)在行动档案中。

答案 1 :(得分:0)

试试这个

HTML

<html>
  <form action="cregister.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="wizForm">
        <input type="file" name="image" id="image" value="uploads/default.png" />
        <input type="submit" id="simple-post" class="btn btn-success submitBtn" value="Submit"/>
</form>  
</html>

PHP

<?php
    if(!empty($_FILES)){
        print_r($_FILES);
        $filename = $_FILES['image']['name'];
        $path = "uploads";
        move_uploaded_file($_FILES['image']['tmp_name'], $path."/".$filename);
        $photo = $path."/".$filename;
    }
?>