When debugging a small program that uploads files and stores them in the database, problems always occur.

Source: Internet
Author: User
When debugging a small program that uploads files and stores them in the database, problems always occur. Ask the experts here! In fact, this function should be very simple. I can see a program on the Internet and debug it myself, but there are unexpected errors. please kindly advise. Html front-end code: & lt; html & gt; & lt; body & gt; & lt; for always encounters problems when debugging a small program that uploads files and stores them in the database, ask the experts here!
In fact, this function should be very simple. I can see a program on the Internet and debug it myself, but there are unexpected errors. please kindly advise.

Html front-end code:








Php back-end code:

if ($_FILES ['file'] != "none" && $_FILES ['file'] != "") 
{
$time_limit = 60;
set_time_limit ( $time_limit );

$file_type = $_FILES ['file'] ['type'];
$file_name = $_FILES ['file'] ['name'];
$file_size = $_FILES ['file'] ['size'];
$fp = fopen ( $_FILES ['file'] ['tmp_name'], "rb" );

if (! $fp)
{
die ( "file open error" );
}

$file_data = file_get_contents($_FILES["file"]['tmp_name']);
//$file_data = fread($fp, $file_size ) ;
fclose ( $fp );
 $conn = mysqli_connect ( "localhost", "root", "root" );
    if (! $conn)
    {
die ( "error : mysql connect failed" );
    }
    mysqli_select_db($conn,"onlytest" );
    $sql = "insert into onlytest.file (contents,type,name,size) values ('$file_data','$file_type','$file_name','$file_size')";
    $result = mysqli_query ($conn, $sql);
    //$id = mysqli_insert_id ($conn);
    //echo $id;
    mysqli_close ( $conn );
    set_time_limit ( 30 ); 
    if ($result)
    echo "succeed to upload the files ";
    else
    echo "failed!";
   

else 
{
echo "You haven't uploaded any files!";
}
?>


The database is onlytest, the table name is file, and several keywords are id, contents, type, name, and size. The settings are as follows:

------ Solution --------------------
What is the error message?
------ Solution --------------------
What is the maximum size of Upload allowed for your configuration?
------ Solution --------------------
What are SQL statements that can be printed?
------ Solution --------------------
Filter it out. Add $ file_data to the mysql_escape_string function.

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.