【发布时间】:2013-06-11 06:45:50
【问题描述】:
我正在处理网站的注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库。
这可能只是一个简单的问题,因为我是 php 和 mysql 的初学者。
这是我的标准格式,我们称之为register.php。我剪掉了除图像之外的所有其他输入。
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<input type="file" id="inputImage" Name="photo">
<button class="btn btn-large btn-success" type="submit">Register</button>
</form>
这是执行文件,我们称之为code_exec.php
<?php
session_start();
include('connection.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mname = $_POST['mname'];
$address = $_POST['address'];
$contact = $_POST['contact'];
$pic = $_POST['pic'];
$username = $_POST['username'];
$password = $_POST['password'];
$skype = $_POST['skype'];
$email = $_POST['email'];
//This is the directory where images will be saved
$target = "upload/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$pic = ($_FILES['photo']['name']);
//Writes the photo to the server
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password, skype, email, photo)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password', '$skype', '$email', '$pic')");
header("location: index.php?remarks=success");
mysql_close($con);
?>
如何找到图片文件的路径?
【问题讨论】:
-
不要直接插入
$_POST数据!这是一个主要的安全问题en.wikipedia.org/wiki/SQL_injection。请参阅:xkcd.com/327 和 stackoverflow.com/questions/332365/… -
Please, don't use
mysql_*functions in new code。它们不再维护,是officially deprecated,可以是dangerous in live code。看到red box?了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
Please, indent code correctly。没有缩进,代码更难理解。
标签: php html mysql forms registration