user8883996
user8883996

Reputation: 143

How to store file path in database using php?

I have two php pages adp3.php(form page) and adp4.php(uploads content to database)

adp3.php:

<form method="POST" enctype="multipart/form-data">
 <label>SUBJECT:</label><select name="subject">
 <?php 
$query="SELECT * FROM subject WHERE SyllabusID=".$batch1." and SemID=".$sems."";
$result=mysqli_query($con, $query);
//loop
foreach($result as $subject){
?>
<option value="<?php echo $subject['SubjectID']."-".$subject['SubjectName']; ?>"><?php echo $subject['SubjectName']; ?></option>
<?php
}
?>
  </select>
   <br>
DATE: <select name="date">
    <option>MAY/JUNE</option>
    <option>NOV/DEC</option>
    </select>

    <p color="white">YEAR: <input  type="varchar" name="year"/></p></br>

    <p color="white">PAPER LINK: <input  type="file" name="file"/></p></br>
         <br>
         <button formaction="adp4.php" class="btn-login">GO</button>

adp4:

<?php
$date =$_POST['date'];
$year=$_POST['year'];
$file=$_FILES['file'];

    if($date && $year)
    { mysql_connect("localhost","root","") or die("we couldnt connect");
       mysql_select_db("dbmsproj");
        $result=mysql_query("INSERT INTO paper(SubjectID,Date,Dlink) values('$subid','$date $year','$file')");

      ?>  <p color="white"><?php echo "Paper SUCCESSFULLY ADDED TO THE DATABASE";
    }
    else
    {?>
        <p color="white"><?php echo  "ALL FIELDS NEED TO BE FILLED ";
    }
        ?>

everything is getting entered correctly in db besides file path(shows "Array" in file path field).. please help

Update: (PROBLEM SOLVED) Thank you guys for helping me. My code is finally working . you guys are the best

Upvotes: 0

Views: 3369

Answers (4)

Oshan Mendis
Oshan Mendis

Reputation: 196

Try using $_POST['file'] instead of using $_FILE since you just need to know the file path

EDIT:

As for the documentation, $_FILES[] returns an array which has all the information about the file you have uploaded. Using this array you can get the temporary name with path of the file using

$file=$_FILES['file']['temp_name'];

The above code will provide you with the relevant temporary name of the uploaded file within the server

See also : PHP Documentation

For further inspection these are the data stored within the array (As for the PHP Documentation):

$_FILES['userfile']['name']

The original name of the file on the client machine.

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

$_FILES['userfile']['size']

The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error']

The error code associated with this file upload.

Upvotes: 3

A.D.
A.D.

Reputation: 2372

check this may this can help.

$filePath = realpath($_FILES["file"]["tmp_name"]);

Upvotes: 0

hasnain
hasnain

Reputation: 137

First you check what are you inserting: by this.

print_r($_FILES["file"]);

Upvotes: 2

Pratik Vekariya
Pratik Vekariya

Reputation: 45

If you access file name than you have to do like this

$file = $_FILES["file"]["name"];

Upvotes: -1

Related Questions