tags:

views:

33

answers:

2

I have made a application form in which i am asking for username,password,email id and user's resume.Now after uploading resume i am storing it into hard disk into htdocs/uploadedfiles/..in a format something like this username_filename.In database i am storing file name,file size,file type.Some coading for this i am showing here

$filesize=$_FILES['file']['size'];
$filename=$_FILES['file']['name'];
$filetype=$_FILES['file']['type'];
$temp_name=$_FILES['file']['tmp_name'];   //temporary name of uploaded file
$pwd_hash = hash('sha1',$_POST['password']);        



$target_path = "uploadedfiles/";
$target_path = $target_path.$_POST['username']."_".basename( $_FILES['file']['name']); 

move_uploaded_file($_FILES['file']['tmp_name'], $target_path) ;   

$sql="insert into employee values    ('NULL','{$_POST[username]}','{$pwd_hash}','{$filename}','{$filetype}','$filesize',NOW())";

Now i have two questions 1.NOw how i can display this file data into a textarea(something like naukri.com resume section) 2.How one can retrive that resume file from folder on hard-disk.What query should i write to fetch this file from that folder.I know how to retrive data from database but i dont know how to retrive data from a folder in hard-disk like in the case if user want to delete this file or he wnat to download this file.How i can do this

A: 

Assuming you gotten the path to the file from the db:

$content = file_get_contents($path_to_file); will load the entire contents of the file into a string $content. You could then do:

<textarea>
<?php echo $content; ?>
</textarea>

to display it.

To delete a file you would use unlink($path_to_file); either directly before or after deleting the necessary record from the DB (or setting the file path column to null).

Now as far as the path you store in the DB you DONT want to use a relative path. Always use an absolute path. so instead of:

$target_path = "uploadedfiles/";
$target_path = $target_path.$_POST['username']."_".basename( $_FILES['file']['name']);

you would do:

$target_path = "uploadedfiles/";
$target_path = realpath($target_path.$_POST['username']."_".basename( $_FILES['file']['name']));

Youll also probably want to remove anything odd (spaces, special characters, etc.) from the filename when it comes in.

prodigitalson
i get the point of displaying data into textarea but not getting point how to retrive file from folder on hard disk..and about what realpath u are taking about sir..should i not attch username with the file name.plz tell little bit about this also
Deepak Narwal
see the docs on `realpath`: http://php.net/manual/en/function.realpath.php Also what dont you get about retrieval of the file? You have to understand that to ehoc its contents in a `textarea` - which you said you understood :-)
prodigitalson
A: 

I am sorry i should not give answer of question myself..But it is necessary for solution..

$row=mysql_fetch_array($result);
$a='uploadedfiles/deepak.narwal_Deepak_resume revised'; //this is file name which i want to open.
$content = file_get_contents($a);

This is what i used for displaying data stored in this file

Resume
<textarea rows="50" cols="70" name="feedback"><?php echo $content; ?></textarea>

But when i execute the file following error is shown Warning: file_get_contents(uploadedfiles/deepak.narwal_Deepak_resume revised) [function.file-get-contents]: failed to open stream: No such file or directory in F:\Study Material\Linux\xampp\htdocs\test.php on line 26.But this file is in that directory i checked manully

i also tried like this
 $row=mysql_fetch_array($result);
    $a=row['File Path']; //this path is stored in database.file is there sure
    $content = file_get_contents($a);
Deepak Narwal
You should move this as an edit to your inital question not as an answer. Or alternatively create an entirely new question.What is the FULL path to the file on your system (for example `F:\Study Material\Linux\xampp\htdocs\uploadedfiles\filename.txt`)? that is the path you should use with `file_get_contents` Assuming youre trying to read the file from the same realative location you were in when you stored it this shouldnt be necessary. ITs jsut a better practice. Also you should not use space in file or dirnames - if you ever go to deploy to a non- windows server you can have some issues.
prodigitalson