-5
<?php

    $con=mysql_connect("localhost","root","");
        if(!$con)
        {
        die('Could Not Connect:'.mysql_error());
        } 

    mysql_select_db("tcs",$con);

    $upload_to = "./uploadedfiles/";
    move_uploaded_file(
        $_FILES["filename"]["tmp_name"],
        $upload_to . "/" . $_FILES["file"]["name"]
    );

    $sql="insert into employee values ('$_POST[username]','$_FILES[filename][name]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }

    echo "Employee Uploaded File"."$_FILES[file][name]";  //showing uploaded file name 

    ?>

But there are three problems:

  1. $sql="insert into employee values('$_POST[username]','$_FILES[filename][name]')";

By '$_FILES[filename][name]' this command file name is not saving in database.

And if I try $sql="insert into employee values ('username....','$_FILES["filename"]["name"]')" then a syntax error is displayed.

How do I send file name also in database? Please write or edit above code.

How do I add username id with this file so that it can be downloaded in the future?

  1. If I try the same file name from another location to store in the database (uploaded folder), then the file is still the same (only one copy after 2 times of uploading the same file name but from different locations).

  2. How to download file if user wants to download it? How server will know that this file name belongs to this user? Please tell me the code for this purpose.

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
Deepak Narwal
  • 313
  • 7
  • 23
  • and also tell me when i give comment how to update my post i think as i give any comment my comment is not checked by is there any method to update it so that it can see at upper in site – Deepak Narwal Jan 22 '10 at 12:01
  • Haven't you searched for it via Google or even here on SO? Your question is already answered: http://stackoverflow.com/questions/1312227/upload-a-file-to-a-mysql-db-with-php duplicate – Felix Kling Jan 22 '10 at 12:05
  • 2
    -1 for asking to have code you can copy and paste. That isn't what real developers do. – Greg Beech Jan 22 '10 at 13:37
  • @above i am sorry sir but in beginning is i do copy paste and then implement it usually i think i iwll not forget that logic in future..somebody says right if you do any task practically then it is better than theorticallly – Deepak Narwal Jan 22 '10 at 17:31

2 Answers2

1

Basically you store images in a folder but in the database, you store only the file names of uploaded pics. Here is how to upload the files.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

To store images in our DB, you've got to make the field BLOB instead of VARCHAR.

MoeAmine
  • 5,976
  • 2
  • 18
  • 21