php 报名表-上传图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17111518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Registration form - upload image
提问by user2462645
I'm working on a registration form for a website, I'm trying to upload a photo to the server and pass the file path on to the database.
我正在处理网站的注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库。
This is probably just a simple issue, as i'm quite the beginner to php and mysql.
这可能只是一个简单的问题,因为我是 php 和 mysql 的初学者。
Here's my standard form, which we'll call register.php. I cut out all other input than the image.
这是我的标准表单,我们将其称为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>
This is the execution file, which we'll call code_exec.php
这是执行文件,我们将其称为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);
?>
How do I find the path of the image file?
如何找到图像文件的路径?
回答by Brijesh Dubey
In code
在代码中
Before
前
<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>
After changes
更改后
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post" enctype="multipart/form-data">
<input type="file" id="inputImage" Name="photo">
<button class="btn btn-large btn-success" type="submit">Register</button>
</form>
You just add
enctype="multipart/form-data"
in your html form tag
您只需添加
enctype="multipart/form-data"
html 表单标签
Description
描述
1.When you upload any types of files from form you must write enctypeattribute in form element.
1.当您从表单上传任何类型的文件时,您必须在表单元素中写入enctype属性。
Thank you
谢谢
回答by ajay ramgarhia
When to upload photo/media from form, you need to add <form method="" action="" enctype = "multipart/form-data">
attribute to the form. Without that uploading of media will not work.
当从表单上传照片/媒体时,您需要向<form method="" action="" enctype = "multipart/form-data">
表单添加属性。没有该媒体上传将无法正常工作。
Also make sure to use post
method only for media uploading
还要确保使用post
仅用于媒体上传的方法
回答by Konrad Borowski
It's easy, just add $target
to your SQL query.
这很简单,只需添加$target
到您的 SQL 查询中即可。
// Escape the string, so you wouldn't be affected by SQL injection.
// Move to MySQLi or PDO, and follow guide http://bobby-tables.com/php.html
// to have your queries automatically escaped, as it's easy to forget about
// using mysql_real_escape_string() (also, it's annoying to type it).
$target = mysql_real_escape_string($target);
mysql_query("INSERT INTO member(..., target) VALUES (..., '$target')");
回答by Saqib Malik
just use the layout i have provided:
只需使用我提供的布局:
for html use this keep in mind you can always improve it:
对于 html 使用这一点请记住,您可以随时改进它:
<form action="nameofthepage.php" method="post" enctype="multipart/form-data">
<table align="center" width="750">
<tr align="center">
<td colspan="6"><h2>Create an Account</h2></td>
</tr>
<tr>
<td align="right">Customer Name:</td>
<td><input type="text" name="c_name" required/></td>
</tr>
<tr>
<td align="right">Customer Email:</td>
<td><input type="text" name="c_email" required/></td>
</tr>
<tr>
<td align="right">Customer Password:</td>
<td><input type="password" name="c_pass" required/></td>
</tr>
<tr>
<td align="right">Customer Image:</td>
<td><input type="file" name="c_image" required/></td>
</tr>
<tr>
<td align="right">Customer Country:</td>
<td>
<select name="c_country">
<option>Select a Country</option>
<option>Afghanistan</option>
<option>India</option>
<option>Japan</option>
<option>Pakistan</option>
<option>Israel</option>
<option>Nepal</option>
<option>United Arab Emirates</option>
<option>United States</option>
<option>United Kingdom</option>
</select>
</td>
</tr>
<tr>
<td align="right">Customer City:</td>
<td><input type="text" name="c_city" required/></td>
</tr>
<tr>
<td align="right">Customer Contact:</td>
<td><input type="text" name="c_contact" required/></td>
</tr>
<tr>
<td align="right">Customer Address</td>
<td><input type="text" name="c_address" required/></td>
</tr>
<tr align="center">
<td colspan="6"><input type="submit" name="register" value="Create Account" /></td>
</tr>
</table>
</form>
and for the php use:
对于 php 使用:
<?php
if(isset($_POST['register'])){
$ip = getIp();
$c_name = $_POST['c_name'];
$c_email = $_POST['c_email'];
$c_pass = $_POST['c_pass'];
$c_image = $_FILES['c_image']['name'];
$c_image_tmp = $_FILES['c_image']['tmp_name'];
$c_country = $_POST['c_country'];
$c_city = $_POST['c_city'];
$c_contact = $_POST['c_contact'];
$c_address = $_POST['c_address'];
move_uploaded_file($c_image_tmp,"customer/customer_images/$c_image");
$insert_c = "insert into customers (customer_ip,customer_name,customer_email,customer_pass,customer_country,customer_city,customer_contact,customer_address,customer_image) values ('$ip','$c_name','$c_email','$c_pass','$c_country','$c_city','$c_contact','$c_address','$c_image')";
$run_c = mysqli_query($con, $insert_c);
$sel_cart = "select * from cart where ip_add='$ip'";
$run_cart = mysqli_query($con, $sel_cart);
$check_cart = mysqli_num_rows($run_cart);
if($check_cart==0){
$_SESSION['customer_email']=$c_email;
echo "<script>alert('Account has been created successfully, Thanks!')</script>";
echo "<script>window.open('customer/my_account.php','_self')</script>";
}
else {
$_SESSION['customer_email']=$c_email;
echo "<script>alert('Account has been created successfully, Thanks!')</script>";
echo "<script>window.open('checkout.php','_self')</script>";
}
}
?>
BUT DON'T FORGET CHANGING THE NAMES OF THE TABLES AND VALUES HOPE IT HEPLED!
但不要忘记更改表和值的名称希望它有所帮助!
回答by AGM Tazim
When you used move_uploaded_file($_FILES['photo']['tmp_name'], $target)
, the image moved to your $target
path. So the path for image is the $target
. Now just use $target
instead of $pic
on the query function you used. Like-
当您使用 时move_uploaded_file($_FILES['photo']['tmp_name'], $target)
,图像移动到您的$target
路径。所以图像的路径是$target
. 现在只需使用$target
而不是$pic
您使用的查询功能。喜欢-
$target = mysql_real_escape_string($target); //escape string for sql injection
mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password, skype, email, photo)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$target', '$username', '$password', '$skype', '$email', '$pic')");