我想上傳一個文件並將其保存到這裏本地主機服務器是我的PHP:上傳文件並重新下載了PHP與MySQL
<?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>
但文件上傳成功,但它並沒有添加到文件表的名稱和路徑。
** WARNING **:如果你剛開始學習PHP,請不要使用['mysql_query'](http://php.net/manual/en /function.mysql-query.php)接口。這是非常可怕和危險的,它在PHP 7中被刪除了。[PDO的替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介紹了最佳實踐。你的用戶參數是**不是** [正確轉義](http://bobby-tables.com/php),並且有[SQL注入漏洞](http://bobby-tables.com/)可以被利用。 – tadman
**警告**:不要接受用戶提供的文件名,因爲這些文件可能是敵對的,可能會覆蓋代碼的一部分或更糟。最好使用某種隨機生成的標識符,如UUID,以避免以這種方式暴露自己。 – tadman
@tadman你可以爲我編輯嗎? :) –