Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i upload and download the file in php with MySQL database.



please help me...
Posted
Updated 28-Nov-17 16:24pm
Comments
Ed Nutting 17-May-12 8:26am    
Have you tried searching Google? This has been answered an uncountable number of times - a quick search will find you what you want.

Ed

<?php // display file upload form
if (!isset($_POST['submit'])) { ?>


Select file:





<?php
} else {
// check uploaded file size
if ($_FILES['data']['size'] == 0) {
die("ERROR: Zero byte file upload");
}
// check if file type is allowed (optional)
$allowedFileTypes = array("image/gif", "image/jpeg", "image/pjpeg");
if (!in_array($_FILES['data']['type'], $allowedFileTypes)) {
die("ERROR: File type not permitted");
} // check if this is a valid upload
if (!is_uploaded_file($_FILES['data']['tmp_name'])) {
die("ERROR: Not a valid file upload"); } // set the name of the target directory
$uploadDir = "./uploads/"; // copy the uploaded file to the directory
move_uploaded_file($_FILES['data']['tmp_name'], $uploadDir . $_FILES['data']['name']) or die("Cannot copy uploaded file"); // display success message
echo "File successfully uploaded to " . $uploadDir .$_FILES['data']['name']; } ?>
 
Share this answer
 
If you want to get something from your MySQL database, you will have to connect to it and then query it:
PHP
$connectionString = mysql_connect('host', 'MySQL username', 'MySQL Password');//Connect to the MySQL database
$selectDatabase = mysql_selectdb('MySQL Database Name', $connectionString);//Connect to your database

//To get something to the database
$tableGetQuery = mysql_query("SELECT * FROM `table` WHERE `field` = 'value'");//Query the database table
$tableGetArray = mysql_fetch_array($tableGetQuery);//Split the table into individual elements
$tableFieldValue = $tableGetArray['field_name'];//Get the value of the table cell
//To add to the table
$tableAddQuery = mysql_query = "INSERT INTO `table` (`field_name`, `other_field_name`) VALUES ('value1', 'value2')";


But if you want to upload and download files just use normal HTML file upload element and PHP code:

PHP
//To upload

if($_POST['submit'])//Form submit - where 'file' is the file upload HTML element
{
	if($_FILES["file"]["error"] > 0)//Check if there is an error
	{
		$msg = "Error: ".$_FILES["file"]["error"];
	}
	else
	{
	    if(file_exists("upload/".$_FILES["file"]["name"]))//Checks if file exists
            {
		$msg = "The file already exists!";
            }
	    else
	    {
		move_uploaded_file($_FILES["file"]["tmp_name"], "upload/". $_FILES["file"]["name"]);//Moves the uploaded file from the temporary directory ('tmp_name') to the 'upload/' directory
	    }
	}
}


And to download the file just navigate the the location of the file
'www.yoursite.com/upload/uploaded_file.txt'

If you wanted to store the uploaded file in a MySQL data base, in the file upload code, just add the MySQL connection settings and place the 'INSERT INTO' query either before of after the moving of the file from the temporary directory. Because that is what I have done.

Hope it helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900