【发布时间】:2014-10-12 02:57:56
【问题描述】:
我是 PHP 新手,正在学习它!
我在本地主机名称“submitpaper”上创建了一个简单的数据库
然后我创建了一个表名“upload_file”,其中包含两个字段(file1、file2)都是(VARCHAR 255)
我在将文件保存到目标文件夹“testupload”时遇到问题
请检查我的 PHP 脚本和 HTML
PHP 脚本
<?php
//This is the directory where files will be saved
$target = "testupload/";
$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$file1=($_FILES['file1']['name']);
$file2=($_FILES['file2']['name']);
// Connects to your Database
mysql_connect("localhost", "root", "")
//Writes the information to the database
mysql_query("INSERT INTO `upload_file` VALUES ('$file1', '$file2')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). "has been uploaded, and your information has been added to the directory"; }
else {
//Gives and error if its not echo "Sorry, there was a problem uploading your file."; }
?>
HTML 文件代码
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
File1:<input type="file" name="file1" id="file1"><br>
File2:<input type="file" name="file2" id="file2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
【问题讨论】:
-
目标文件夹是否已经创建?如果不是,您需要先手动或通过 php 脚本创建它。
-
你遇到了什么错误?
-
我已经创建了 PHP 文件所在的目标文件夹!单击“提交”按钮后不显示仅显示 PHP 脚本的错误
-
如果正在显示 PHP 脚本,那么您很可能没有在服务器上运行该脚本。您需要一个服务器(如果是 windows 则安装 wamp,如果是 linux 则安装灯)来运行 php 脚本。
标签: php html mysql file-upload