使用PHP和MYSQL上传文件并再次为服务器下载

时间:2016-11-22 06:05:26

标签: php html mysql

我想上传文件并将其保存到本地主机这里是我的php:

<?php
$error = "error";

$con = mysql_connect('localhost','bayash_user','u)nHf,Ac)') or die($error);
mysql_select_db('bayansh_bc',$con) or die($error);

if (isset($_POST['submit'])) {

    $doc_name = $_POST['doc_name'];

    $name = $_FILES['myfile']['name'];
    $tmp_name = $_FILES['myfile']['tmp_name'];

    if ($name && $doc_name) {

        $location = "documents/$name";
        move_uploaded_file($tmp_name, $location);
        $query = mysql_query("INSERT INTO documents (name.path) VALUES ('$doc_name','$location')");
        header('Location:index.php');
    }else

    die("Field to print");
}
?>

这是我的HTML代码:

<html>
<head>
    <title> Upload Documents</title>
</head>
<body>

    <form action="upload.php" method="post" enctype="multipart/form-data">

        <label>Document Name</label>
        <input type="text" name="doc_name">
        <input type="file" name="myfile">
        <input type="submit" name="submit" value="Upload">

    </form>
</body>
</html>

但文件上传成功,但它没有向文档表添加名称和路径。

1 个答案:

答案 0 :(得分:-1)

试试这个,

<?php
$error = "error";

$con = mysql_connect('localhost','bayash_user','u)nHf,Ac)') or die($error);
mysql_select_db('bayansh_bc',$con) or die($error);

if (isset($_POST['submit'])) {

    $doc_name = $_POST['doc_name'];

    $name = $_FILES['myfile']['name'];
    $tmp_name = $_FILES['myfile']['tmp_name'];

    if ($name && $doc_name) {

        $location = "documents/".$name;
        move_uploaded_file($tmp_name, $location);
        $query = mysql_query("INSERT INTO documents (name,path) VALUES ('$doc_name','$location')");
        header('Location:index.php');
    }else

    die("Field to print");
}
?>