search

Home  >  Q&A  >  body text

How to import Excel files into the database through code? Below is an example I found online, but I can’t import it?

The example in this article describes how PHP implements uploading files and storing them in the database. Share it with everyone for your reference. The details are as follows:

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 out

$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);

//Output the corresponding file header first, And restore the original file name

header("Content-type:$type");

header("Content-Disposition: attachment; filename=$name");

echo $data;

?>



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 out

$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 If there is no specified record, an error will be reported

$num=mysql_num_rows($result);

if($num<1) die("error: no this recorder");

//The following two sentences of program can also be written like this

//$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 "<hr>Uploaded file information:";

echo "<br>The file's name - $name";

echo "< ;br>The file's size - $size";

echo "<br><a href=show_add.php?id=$id>Attachment</a>";

?>



submit.php file is as follows:

<?php

if(is_uploaded_file($_FILES['myfile'][ 'tmp_name'])) {

//Now you have the uploaded file

$myfile=$_FILES["myfile"];

//Set the timeout limit, The default time is 30 seconds. When set to 0, there is no time limit.

$time_limit=60;

set_time_limit($time_limit); //

Read 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 to the database and save the file to In 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 the default timeout setting

echo "Upload successful--- ";

echo "<a href='show_info.php?id=$id'>Display uploaded file information </a>";

}

else {

echo "You did not upload any files";

}

?>



upload.php file is as follows:

<html>

<head>

<title> ;File upload form</title>

</head>

<body>

<table>

<form enctype ='multipart/form-data' name='myform' action='submit.php'

method='post'>

<INPUT TYPE = "hidden" NAME = " MAX_FILE_SIZE" VALUE ="1000000">

<tr><td>Select upload file</td><td>

<input name='myfile' type ='file'></td></tr>

<tr><td colspan='2'><input name='submit' value='upload' type ='submit'></td></tr>

</table>

</body>

</html>



琅2572 days ago964

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-12-09 20:24:57

    When I see this bunch of code, I really don’t want to write it and read it

    reply
    0
  • Cancelreply