如何使用php上传目标文件夹中的文件?

时间:2014-08-19 05:21:23

标签: php html mysql file-upload



我是PHP新手并且学习它!
我在localhost名称'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>

2 个答案:

答案 0 :(得分:0)

您正试图在$_FILES['file']['name']中错误地访问文件名。您必须使用$_FILES['nameoffile']['name'],其中nameoffile是您为文件输入字段指定的文件的名称。即as,

$_FILES['file1']['name']

CODE:

$target = "testupload/";
$target = $target . basename( $_FILES['file1']['name']);
if(move_uploaded_file($_FILES['file1']['tmp_name'], $target)) 
{
}

答案 1 :(得分:0)

以下代码可能对您有用。

$target = "Admin/upload/";
$target = $target . basename( $_FILES['uploaded_file']['name']); 
$pic=($_FILES['uploaded_file']['name']); 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target)) 
{ 
    //Tells you if its all ok 
    //echo "The file ". basename( $_FILES['uploaded_file']['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."; 
} 

在此之前,您必须具有文件上传路径文件夹的读写权限。