File and form operation and database operation involved in uploading PHP files

Source: Internet
Author: User
Tags file info mysql connect php and mysql php file upload
This article mainly introduces the PHP file upload involved in the file and form operation and database operations, interested in the friend's reference, I hope to be helpful to everyone.

This article describes the PHP implementation of the upload file and into the database method, as follows:

The show_add.php file is as follows:

<?php       if (!isset ($_request[' id ')) or $_request[' id ']== "") Die ("Error:id none");  $id = $_request[' id '];  Locate the record, read the  $conn =mysql_connect ("localhost", "root", "admin");  if (! $conn) Die ("Error:mysql connect Failed");  mysql_select_db ("Nokiapaymentplat", $conn);  $sql = "SELECT * from receive where id= $id";  $result = mysql_query ($sql, $conn);  if (! $result) die ("Error:mysql query");  $num =mysql_num_rows ($result);  if ($num <1) die ("Error:no this recorder");  $data = mysql_result ($result, 0, "File_data");  $type = mysql_result ($result, 0, "File_type");  $name = mysql_result ($result, 0, "file_name");  Mysql_close ($conn);  First output the corresponding file header, and restore the original file name  header ("Content-type: $type");  Header ("content-disposition:attachment; Filename= $name ");  Echo $data;? >

The show_info.php file is as follows:

<?php    if (!isset ($_request[' id ')) or $_request[' id ']== "") Die ("Error:id none");  $id = $_request[' id '];  Locate the record, read the  $conn =mysql_connect ("localhost", "root", "admin");  if (! $conn) Die ("Error:mysql connect Failed");  mysql_select_db ("Nokiapaymentplat", $conn);  $sql = "Select File_name, file_size from receive where id= $id";  $result = mysql_query ($sql, $conn);  if (! $result) die ("Error:mysql query");  If there is no record specified, the error  $num =mysql_num_rows ($result);  if ($num <1) die ("Error:no this recorder");  The following two sentences can also be written in this way  //$row =mysql_fetch_object ($result);  $name = $row->name;  $size = $row->size;  $name = mysql_result ($result, 0, "file_name");  $size = mysql_result ($result, 0, "file_size");  Mysql_close ($conn);  echo "

The submit.php file is as follows:

<?php if (is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {//have uploaded files $myfile =$_files["MyFile"];         Set timeout limit time, default time is 30 seconds, set to 0 for unlimited $time _limit=60; Set_time_limit ($time _limit);    Read the contents of the file into the string $fp =fopen ($myfile [' Tmp_name '], "RB");    if (! $fp) Die ("File open error");    $file _data = addslashes (Fread ($fp, FileSize ($myfile [' tmp_name ']));    Fclose ($FP);     Unlink ($myfile [' tmp_name ']);    File format, name, size $file _type= $myfile ["type"];    $file _name= $myfile ["name"];    $file _size= $myfile ["size"];    Die ($file _type);    Connect the database, save the file to the database $conn =mysql_connect ("localhost", "root", "admin");    if (! $conn) Die ("Error:mysql connect Failed");    mysql_select_db ("Nokiapaymentplat", $conn); $sql = "INSERT into receive (file_data,file_type,file_name,file_size) VALUES (' $file _data ', ' $file _type ', ' $file _name ')    , $file _size) ";    $result =mysql_query ($sql, $conn);    The following sentence takes out the ID of the INSERT statement just now $id =mysql_insert_id ();    Mysql_close ($conn); Set_time_limit (30);   Restore default timeout settings  echo "Upload successful---";  echo "<a href= ' show_info.php?id= $id ' > show upload file info </a>";    } else {echo "You did not upload any files"; }?>

The upload.php file is as follows:


Summary: the above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Php+ajax real-time input automatic search matching method

Based on jquery, PHP and MySQL achieve a rating function

Application of PHP variables, arrays, regular expressions and templates

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.