我想上传文件并将其保存到本地主机这里是我的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>
但文件上传成功,但它没有向文档表添加名称和路径。
答案 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");
}
?>