PHP Script Database features detailed (2)

Source: Internet
Author: User
Tags exit file size file upload header insert connect php script query
Script | data | database | Detailed (author: Wang Kaibo)





use PHP to save files to a database

The
database is the center of data organization and storage. It can also be a variety of data, including programs, files, reports, and even audio and video data. Because through the browser, individual users can only fill in a small number of personal resumes. Therefore, we demonstrate the user's personal resume upload function. Other types of data can be manipulated by imitating this example.





First is the information gathering page. Let the user select the file to upload. The HTML code for this page is as follows:





〈!--begin of post.htm--〉





〈p〉〈/p〉





〈form method= "POST" action= "insert.php" enctype= "Multipart/form-data" 〉





〈p〉〈b〉 Personal Resume Submission 〈/b〉〈/p〉





〈p〉 Name: 〈br〉





〈input type= "text" name= "name" size= "20" 〉〈/p〉





〈p〉 Profile: 〈br〉





〈textarea rows= "2" name= "Intro" cols= "20" 〉〈/textarea〉〈/p〉





〈p〉 Resume File: 〈br〉





〈input type= "File" Name= "Resufile" 〉〈/p〉





〈p〉〈input type= "Submit" value= "submitted" name= "B1" 〉〈/p〉





〈/form〉





〈!-end of post.htm--〉





Note that the Enctype keyword must not be saved, otherwise the file cannot be uploaded correctly.





here, we redesign the code that inserts records into the database:





  〈?





//begin of File insert.php





if ($ResuFile!= "None")





//Make sure the user has selected the file





  {





$Size = filesize ($ResuFile);





//Determine file size





$mFileData = addslashes (Fread (fopen ($ResuFile, "R"), $Size));





//Read the file and process the content





unlink ($ResuFile);





//Delete upload temp file





  }





$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!"). The database server may not be started, or the username password is incorrect! ");





$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("Select Database error, possibly the database you specified does not exist!") ");





$query = "INSERT into Resume (name,intro,resufile) VALUES (' $Name ', ' $Intro ', ' $mFileData ')";





$result = @mysql_query ("$query", $LinkID); Execute the query, insert the file into the database





if (! $result)





echo "Data insertion failed!" ";





Else





echo "File upload succeeded!";





@mysql_close ($LinkID);





//end of File insert.php





  ?〉





has the above foundation, writes the data from the database to read the program should be very simple. What you need to be aware of is how the file is sent to the customer. The server must send a header message to the browser stating that the data that will be sent is a Word document. If the user's computer is equipped with MSWord, the browser automatically calls word for the document display.





we can set up a hyperlink to download this Word file:





  〈?





//begin of File show.php





$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!"). The database server may not be started, or the username password is incorrect! ");





$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("Select Database error, possibly the database you specified does not exist!") ");





$query = "INSERT into Resume (name,intro,resufile) VALUES (' $Name ', ' $Intro ', ' $mFileData ')";





$result = @mysql_query ("$query", $LinkID);





//Execute query, insert file into database





$query = "Select Id,name,intro from Resume";





//Generate SQL statements





$result = mysql_query ($query, $LinkID); execution, the result set is saved to the variable $result





$num = mysql_num_rows ($result); Gets the number of record rows returned by the query





if ($num = 0)





  {





echo "did not find any records";





exit ();





  }





while ($row =mysql_fetch_array ($result))//Take the next line of data from the result set to the array $row





  {





echo $row ["ID"]. " ". $row [" Name]. " ". $row [" Intro "]." ";





echo "〈a href= \ download.php?id=". $row ["ID"]. " \ "View Word document 〈/a〉〈br〉";





}





//end of File show.php





  ?〉





access to file show.php, the user sees a list of personal brief information. Click "View Word document" To see the detailed resume of the corresponding member.





Word documents are displayed in the following file:





  〈?





//Begin of File download.php





$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!"). The database server may not be started, or the username password is incorrect! ");





$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("Select Database error, possibly the database you specified does not exist!") ");





$query = "Select Resufile from Resume where id= $ID";





//$ID The variable passed for the call





$result = @mysql_query ("$query", $LinkID);





//Execute query to read file contents from database





if (mysql_num_rows ($result) 〈1)





  {





echo "did not find the appropriate file!" ";





exit ();





  }





$row = mysql_fetch_array ($result);





$mFileData = $row ["Resufile"];





//Read the content of your CV (data in Word file format)





header ("Content-type:application/msword");





//Send header information stating that the data to be sent is a Word document





Echo $mFileData;





//Send document Data





//end of File download.php





  ?〉





so far, we have achieved the personal resume submission, database storage, information browsing and other functions, basically completed the "Talent Information exchange" framework function.





need to explain that file upload and database storage through PHP is a more prominent technical problem. Many of the sites on PHP continue to have such problems. These operations are more dependent on the platform and environment settings. Different platform configurations can cause the operation to fail.  This article is attached with the operating platform of the above program, compiling parameters for reference.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.