Deepak Narwal Deepak Narwal - 20 days ago 6
PHP Question

Three problems in uploading a file into database and then how to download it back

<?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.


Answer

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.