954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

upload a doc file in php

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

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 

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.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

can u please suggest one for uploading word document

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 

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.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

ya tat really helped me

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 


<?php
$member=$_POST['member'];

$title=$_POST['title'];

$keywords=$_REQUEST['keywords'];
$industype=$_REQUEST['industype'];
$careerlevel=$_REQUEST['careerlevel'];
$txtresume=$_REQUEST['txtresume'];
$cgmember=$_REQUEST['cgmember'];

$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['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
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);


?>

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 

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

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 




Split Candidate

Resume title


Salary



Industry Type *

Accounting/Auditing

Career Level

Student(High Level)


Resume Description


 


Choose a file to upload:



I agree





kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 


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

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 

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.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

<?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);

?>
data is not getting inserted.

kings
Junior Poster
107 posts since Nov 2007
Reputation Points: 3
Solved Threads: 2
 

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

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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" /></td></tr>
<tr>
<td colspan="2">

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

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


</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);

?>
mohamedsafiq
Newbie Poster
13 posts since Jan 2010
Reputation Points: 8
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You