Farhad paikan Farhad paikan - 15 days ago 4
HTML Question

upload a file and download it again for server with PHP and MYSQL

i want to upload a file and save it to local host here is my php:

<?php
$error = "error";

$con = mysql_connect('localhost','bayash_user','u)nHf,Ac)') or die($error);
mysql_select_db('bayansh_bc',$con) or die($error);

if (isset($_POST['submit'])) {

$doc_name = $_POST['doc_name'];

$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name && $doc_name) {

$location = "documents/$name";
move_uploaded_file($tmp_name, $location);
$query = mysql_query("INSERT INTO documents (name.path) VALUES ('$doc_name','$location')");
header('Location:index.php');
}else

die("Field to print");
}
?>


and here is my html code:

<html>
<head>
<title> Upload Documents</title>
</head>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">

<label>Document Name</label>
<input type="text" name="doc_name">
<input type="file" name="myfile">
<input type="submit" name="submit" value="Upload">

</form>
</body>
</html>


But the file upload successfully but it doesnt add to document table the name and path.

Answer

try this,

<?php
$error = "error";

$con = mysql_connect('localhost','bayash_user','u)nHf,Ac)') or die($error);
mysql_select_db('bayansh_bc',$con) or die($error);

if (isset($_POST['submit'])) {

    $doc_name = $_POST['doc_name'];

    $name = $_FILES['myfile']['name'];
    $tmp_name = $_FILES['myfile']['tmp_name'];

    if ($name && $doc_name) {

        $location = "documents/".$name;
        move_uploaded_file($tmp_name, $location);
        $query = mysql_query("INSERT INTO documents (name,path) VALUES ('$doc_name','$location')");
        header('Location:index.php');
    }else

    die("Field to print");
}
?>