Home > Backend Development > PHP Tutorial > 在调试一个上传文件并保存在数据库里的小程序,始终出现有关问题,向这里的大神请问

在调试一个上传文件并保存在数据库里的小程序,始终出现有关问题,向这里的大神请问

WBOY
Release: 2016-06-13 11:49:58
Original
791 people have browsed it

在调试一个上传文件并保存在数据库里的小程序,始终出现问题,向这里的大神请教!
其实这个功能应该很简单,我从网上看到一段程序,自己弄下来调试调试,但出现意想不到的错误,请大神指教啊。

html前端代码:

<br /><html><br /><body><br /><form action="upload.php" method="post" enctype="multipart/form-data"><br /><label for="file">Filename:</label><br /><input type="file" name="file" id="file" /> <br /><br /><br /><input type="submit" name="submit" value="Submit" /><br /></form><br /></body><br /></html><br />
Copy after login


php后端代码:
<br /><?php <br />if ($_FILES ['file'] != "none" && $_FILES ['file'] != "") <br />{<br />	$time_limit = 60;<br />	set_time_limit ( $time_limit );<br />	<br />	$file_type = $_FILES ['file'] ['type'];<br />	$file_name = $_FILES ['file'] ['name'];<br />	$file_size = $_FILES ['file'] ['size'];<br />	$fp = fopen ( $_FILES ['file'] ['tmp_name'], "rb" );<br />	<br />	if (! $fp)<br />	{<br />		die ( "file open error" );<br />	}<br />	<br />	$file_data = file_get_contents($_FILES["file"]['tmp_name']);<br />	//$file_data = fread($fp, $file_size ) ;<br />	fclose ( $fp );<br />	 $conn = mysqli_connect ( "localhost", "root", "root" );<br />    if (! $conn)<br />    {<br />		die ( "error : mysql connect failed" );<br />    }<br />    mysqli_select_db($conn,"onlytest" );<br />    $sql = "insert into onlytest.file (contents,type,name,size) values ('$file_data','$file_type','$file_name','$file_size')";<br />    $result = mysqli_query ($conn, $sql);<br />    //$id = mysqli_insert_id ($conn);<br />    //echo $id;<br />    mysqli_close ( $conn );<br />    set_time_limit ( 30 ); <br />    if ($result)<br />    	echo "succeed to upload the files ";<br />    else<br />    	echo "failed!";<br />   <br />} <br />else <br />{<br />	echo "You haven't uploaded any files!";<br />}<br />?><br />
Copy after login


数据库为onlytest,表格名为file,几个关键词为id, contents,type,name,size,设置如下:

------解决方案--------------------
错误信息是什么?
------解决方案--------------------
你的配置最大允许上传的多大的?
------解决方案--------------------
可以打印出sql语句是什么呢?
------解决方案--------------------
过滤下吧。 把$file_data加一个mysql_escape_string函数

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template