Reputation: 83
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.
Here's my standard form, which we'll call register.php. I cut out all other input than the image.
<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
<?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?
Upvotes: 4
Views: 36070
Reputation: 118
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.
Also make sure to use post
method only for media uploading
Upvotes: 0
Reputation: 118
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
Description
1.When you upload any types of files from form you must write enctype attribute in form element.
Thank you
Upvotes: 1
Reputation: 2217
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-
$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')");
Upvotes: 0
Reputation: 98
just use the layout i have provided:
for html use this keep in mind you can always improve it:
<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
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!
Upvotes: 0
Reputation: 11879
It's easy, just add $target
to your SQL query.
// 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')");
Upvotes: 0