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

cgh8pdjw  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(132)

我是新的PHP和学习它!
我在本地主机名“submitpaper”上创建了一个简单的数据库
然后,我创建了一个名为'upload_file'的表,它有两个字段(file 1、file 2),它们都是(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>
bfnvny8b

bfnvny8b1#

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

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

代码:

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

hmmo2u0o2#

下面的代码可能对您有用。

$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."; 
}

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

相关问题