hi,
can someone tell how to upload a file in php.i want to know the difference betweeen uploading a file and storing it in a folder and uploading a file and storing in a database.i want to know which method is good

Recommended Answers

All 13 Replies

the difference between them is when you store the data in the database it will slow the script and have a hard time fetching the data from the database.Best way is to store the data in a folder and save the path in the database.

There are may tutorials there on how to do them.

can u please suggest one for uploading word document

As Ryan_vietnow has already suggested, uploading a file to the database is not a good idea. Upload it to the filesystem and save the path in the database. Check this link. Uploading a file is pretty simple.

ya tat really helped me

<html>
<head>


</head>
<body>
<?php
$member=$_POST;


$title=$_POST;


$keywords=$_REQUEST;
$industype=$_REQUEST;
$careerlevel=$_REQUEST;
$txtresume=$_REQUEST;
$cgmember=$_REQUEST;


$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES);
$_FILES;
$target_path = "uploads/";


$target_path = $target_path . basename( $_FILES);
if (isset($_REQUEST))
{
mysql_query("INSERT INTO post_resume(split, res_title, keywords, ind_type, career, resume, cgmember)
values('$member','$title','$keywords','$industype','$careerlevel','$target_path','$cgmember')") or die("ERROR:".mysql_error());


echo "Record Added";
}


if(move_uploaded_file($_FILES, $target_path)) {
echo "The file ".  basename( $_FILES).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}


mysql_close($link);



?>
</body>
</html>

hi this worked.when i used update it is working.when i tried to insert the target in my table.it is not working

What do you mean by not working ? are you getting any error ?

no error.it is just displaying the else..........There was an error uploading the file, please try again!.
record is getting added but the file pathis not getting added in the db.

my html pg

<html>
<head>
</head>
<body>
<form method="post" action="resumeinsert.php" >
<div>
<table>
<tr>
<td><input type="radio" name="member" value="Split Candidate">Split Candidate</td>
</tr>

<tr>
<td>
Resume title </td><td>
<input name="title" type="text" /></td>
</tr><tr>
<td>
Salary</td><td>
<input name="keywords" type="text"/></td>
</tr>
<tr>
<td>

Industry Type *</td><td>

<select name="industype">
<option value="Accounting/Auditing" selected>Accounting/Auditing</option>

</select>
</td>
</tr>
<tr>
<td>
Career Level</td><td>

<select name="careerlevel">
<option name="Student(High Level)" selected>Student(High Level)</option>
</select>
</td>
</tr>
<tr>
<td>
<strong>
Resume Description
</strong></td>
</tr>
<tr>
<td>
&nbsp;
<textarea name="txtresume" style="width: 672px; height: 214px"></textarea>
</td>
</tr>
<tr>
<td><input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /></td></tr>
<tr>
<td colspan="2">

<input name="cgmember" type="radio" value="I agree " />
I agree

</td>
</tr>
<tr>
<td>
<br />
<br />
</td>
<td>

<input name="submit" type="submit" value="submit"></td>
</tr>
</table>

</div>
</form>
</body>
</html>

<form method="post" action="resumeinsert.php" >

There is no enctype in your form. You need enctype when you are using input type=file.

thanks.the file is getting uploaded.path is not getting stored

Print out what's in $target_path. while adding $target_path to the table, instead of inserting $target_path, do this. $target_path=addslashes($target_path); and then insert.

<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$target_path = "uploads/".$title;
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES);
$_FILES;
$target_path = "uploads/";


$target_path = $target_path . basename( $_FILES);
echo $target_path;
$target_path=addslashes($target_path);
echo $target_path;
if (isset($_REQUEST))
{
mysql_query("INSERT INTO post_resume(split, res_title, keywords, ind_type, career, resume, cgmember)
values('$member','$title','$keywords','$industype','$careerlevel','$target_path','$cgmember')") or die("ERROR:".mysql_error());
echo "Record Added";


}
if(move_uploaded_file($_FILES, $target_path)) {
echo "The file ".  basename( $_FILES).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
mysql_close($link);


?>

data is not getting inserted.

What data ? Print out your query. What does it print ?

The code below is the full working code

<html>
<head>
</head>
<body>
<form name="upload" method="post" action="resumeinsert.php" ENCTYPE="multipart/form-data" >
<div>
<table>
<tr>
<td><input type="radio" name="member" value="Split Candidate">Split Candidate</td>
</tr>

<tr>
<td>
Resume title </td><td>
<input name="title" type="text" /></td>
</tr><tr>
<td>
Salary</td><td>
<input name="keywords" type="text"/></td>
</tr>
<tr>
<td>

Industry Type *</td><td>

<select name="industype">
<option value="Accounting/Auditing" selected>Accounting/Auditing</option>

</select>
</td>
</tr>
<tr>
<td>
Career Level</td><td>

<select name="careerlevel">
<option name="Student(High Level)" selected>Student(High Level)</option>
</select>
</td>
</tr>
<tr>
<td>
<strong>
Resume Description
</strong></td>
</tr>
<tr>
<td>
&nbsp;
<textarea name="txtresume" style="width: 672px; height: 214px"></textarea>
</td>
</tr>
<tr>
<td>
Choose a file to upload: <input name="uploadedfile" type="file" /><br /></td></tr>
<tr>
<td colspan="2">

<input name="cgmember" type="radio" value="I agree " />
I agree

</td>
</tr>
<tr>
<td>
<br />
<br />
</td>
<td>

<input name="submit" type="submit" value="submit"></td>
</tr>
</table>

</div>
</form>
</body>
</html>
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$target_path = "uploads/".$title;
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo $target_path;
$target_path=addslashes($target_path);
echo $target_path;
if (isset($_REQUEST['submit']))
{
mysql_query("INSERT INTO post_resume(split, res_title, keywords, ind_type, career, resume, cgmember)
values('$member','$title','$keywords','$industype','$careerlevel','$target_path','$cgmember')") or die("ERROR:".mysql_error());
echo "Record Added";

}
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
mysql_close($link);

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.