【问题标题】:How to upload file in a target folder with php?如何使用php将文件上传到目标文件夹中?
【发布时间】: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


【解决方案1】:

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

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

代码:

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

【讨论】:

    【解决方案2】:

    以下代码可能对你有用。

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

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

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多