Please help on how to design web page form which enables uploading files using php and msql
Firstly, Create your Database in PHPMYADMIN under create Database give it name then go to the top of the page click on SQL copy and paste this code:
CREATE TABLE `file` ( `id` int(11) NOT NULL AUTO_INCREMENT, `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `uploaded_on` datetime NOT NULL, `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and then tab go button that is all, then go back to your Text Editor create new file connection.php then copy this code:
copy below code and paste it inside connection file connection.php <?php // Database configuration $dbHost = "localhost"; // your host $dbUsername = "root"; // your database username $dbPassword = "root"; // your database password if any, by defaut the password is empty revome the password $dbName = "database_name"; // copy the database name from php my admin or you type theasame name // Create database connection $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); //this are the variables of the configuration // Check connection status if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } ?>
There is many way of creating connection in PHP and MYSQL
create another file index.php then copy and paste this code:
<form action="upload_file.php" method="post" enctype="multipart/form-data"> Select File to Upload: <input type="file" name="file"> <input type="submit" name="submit" value="Upload File"> </form>
finally create another new file upload_file.php then copy and paste this code:
<?php // Include the database configuration file include 'connection.php'; $statusMsg = ''; // File upload path $targetDir = "uploads/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf', 'doc'); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } // Display status message echo $statusMsg; ?>
Remember to create uploads folder in your root folder.
please sent your email then I will send you the comple project for File Upload.