开发者

How can I retrieve files from folder on hard-disk and how to display uploaded file data into a textarea

I have made an 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 coding for this I am showing here

$filesize=$_FILES['file']['si开发者_运维知识库ze'];
$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 can I display this file data into a textarea(something like naukri.com resume section)? 2.How one can retrieve that resume file from folder on hard-disk? What query should I write to fetch this file from that folder? I know how to retrieve data from database but I don't know how to retrieve data from a folder in hard-disk like in the case if user wants to delete this file or he wants to download this file. How I can do this?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜