The logic is that, you use the upload form in html, once you select a file and click the button upload, you must get the tmp_name of the file, the designated folder where you want your file be save and perform the move_uploaded_file() function. The code below is the sample:
1. Create an html tags and etc. and paste this code inside the body tags (save this as uploadfile.php inside you htdocs folder):
<form action="uploadfile.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" value=""/><br/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
require('connect.php'); // note that you already established your connection in MySQL and to your database
session_start();
$path = "files\\";
$fname = $_FILES["file"]["name"];
$ftmp = $_FILES["file"]["tmp_name"];
$fsize = $_FILES["file"]["size"];
$file_fullpath = $path.$fname;
$path = stripslashes($path);
$fname = stripslashes($fname);
$fsize = stripslashes($fsize);
$path = mysql_real_escape_string($path);
$fname = mysql_real_escape_string($fname);
$fsize = mysql_real_escape_string($fsize);
if($fname == ""){
echo "Please complete selection.";
}
else{
move_uploaded_file($ftmp, 'files/'.$fname);
mysql_query("INSERT INTO myfiles(filetitle, filedir, filesize) values( '$fname' , '$path' , '$fsize' )");
echo "File uploaded successfully.";
}
?>
2. Create a folder and name it as files inside your htdocs folder together with the uploadfile.php.
3. In your mysql, create a table and name it as myfiles and put the following as your fieldnames:
- filetitle varchar(25)
- filedir varchar(25)
- filesize varchar(25)
No comments:
Post a Comment