Forum Moderators: coopster

Message Too Old, No Replies

Uploading images with form using php

How to uploading images to mysql database using form with php

         

okonjiaustin

3:15 am on Feb 12, 2008 (gmt 0)

10+ Year Member



I have been using the code below to insert data into a recruitment table successfully, but now I need to allow the applicants upload their passport photographs so that when the row is displayed the photograph will be displayed alongside the other row data. I will be glad if someone helps me with the code to achieve these.
This is my insert code:

<?php
$surname = $_POST['surname'];
$firstname = $_POST['firstname'];
$othernames = $_POST['othernames'];
$dateofbirth = $_POST['dateofbirth'];
$placeofbirth = $_POST['placeofbirth'];

// process form
$link = mysql_connect("localhost", "root","pass");
mysql_select_db("base", $link);
$sql = "INSERT INTO `recruitment` (`surname`,`firstname`,`othernames`,`dateofbirth`,`placeofbirth`)
VALUES ('$surname', '$firstname', '$othernames', '$dateofbirth', '$placeofbirth');";
$result = mysql_query($sql);

// print a success message
echo " Thank you $firstname $surname, for submitting your data<br>";

?>

menace_sa

6:17 am on Feb 12, 2008 (gmt 0)

10+ Year Member



First thing add enctype="multipart/form-data" to your form tag.

On your form add a input box like <input name="passport" type="file" id="passport" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:9px">

No , when submit is pressed in your php code add
$passport = $_FILES['passport']['name']; (This also serves as the check if they have chosen to upload a file)

$filepath = "passportsfile/";

and finally

copy ($_FILES['passport']['tmp_name'], "" . $filepath . "".$_FILES['passport']['name']);

Also make sure the directory you choose have the appropriate rights

Thats it

okonjiaustin

6:24 am on Feb 12, 2008 (gmt 0)

10+ Year Member



Hello Menace_sa,

Does it mean that the following must be a file path in the server: $filepath = "passportsfile/"; and copy ($_FILES['passport']['tmp_name'], "" . $filepath . "".$_FILES['passport']['name']); like C:/recruitment/passport

Austin

menace_sa

6:58 am on Feb 12, 2008 (gmt 0)

10+ Year Member



Its the folder path yes, say your website is in c:\website you will have a passports folder in there ie. c:\website\passport\ , so to access this from within your website folder in the php your $filepath will be "passport/"

okonjiaustin

5:42 am on Feb 13, 2008 (gmt 0)

10+ Year Member



Thanks I will try it.

okonjiaustin

5:16 am on Feb 14, 2008 (gmt 0)

10+ Year Member



Thanks I have tried it and worked.

Now I need to display the row data and the image together.

Can you please suggest a code based on the previous details I submitted.

Thanks

Austin