0

我正在尝试从 php 网页在 mySQL 数据库中上传简单的 .TXT 文件。所以我使用以下查询在 mySQL 服务器上创建了表。

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS `Files` (
  `ID` int(25) NOT NULL auto_increment,
  `Title` varchar(250) NOT NULL default '',
  `File_Name` varchar(250) NOT NULL default '',
  `File_Type` varchar(15) NOT NULL default '',
  `File_Size` varchar(45) NOT NULL default '',
  `File_Content` longblob NOT NULL,
  `File_Extension` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`ID`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

而我的upload.php文件如下

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

$dbh = mysqli_connect("xxx.xx.xxx.xx","xxx","xxx","db") or die("There was a problem with the database connection.");

$acceptable_extensions[0] = "pdf";
$acceptable_extensions[1] = "jpg";
$acceptable_extensions[2] = "gif";
$acceptable_extensions[3] = "doc";
$acceptable_extensions[4] = "ppt";
$acceptable_extensions[5] = "xls";
$acceptable_extensions[6] = "xsl";
$acceptable_extensions[7] = "PDF";
$acceptable_extensions[8] = "JPG";
$acceptable_extensions[9] = "GIF";
$acceptable_extensions[10] = "DOC";
$acceptable_extensions[11] = "PPT";
$acceptable_extensions[12] = "XLS";
$acceptable_extensions[13] = "XSL";
$acceptable_extensions[14] = "txt";
$acceptable_extensions[15] = "TXT";



// Check the uploaded file to make sure it's a valid file

$validated = 1;

if($_FILES && $_FILES['file']['name']){

    //make sure the file has a valid file extension

    $file_info = pathinfo($_FILES['file']['name']);
    $acceptable_ext = 0;



    for($x = 0; $x < count($acceptable_extensions); $x++){

        if($file_info['extension'] == $acceptable_extensions[$x]){
            $acceptable_ext = 1;

        }
    }

    if(!$acceptable_ext){
        $validated = 0;
    }   
}else{
    $validated = 0;
}

//Now that we're sure we have a valid file, 
//we'll add it into the database

if($validated){

    $fileName = $_FILES['file']['name'];
    $tmpName  = $_FILES['file']['tmp_name'];//Nothing in tempName
    $fileSize = $_FILES['file']['size']; // fileSize 0
    $fileType = $_FILES['file']['type'];

    // Slurp the content of the file into a variable

    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()){
        $fileName = addslashes($fileName);
     }

    $file_info = pathinfo($_FILES['file']['name']);

    $sql = "INSERT INTO Files SET 
                Title = '".htmlentities(stripslashes($_POST['title']))."', 
                File_Name = '".$fileName."', 
                File_Type = '".$fileType."',
                File_Size = '".$fileSize."',
                File_Content = '".$content."',
                File_Extension = '".$file_info['extension']."'";


   // $result = mysqli_query($dbh,$sql);

    // If the query was successful, give success message

    if(!$result){
        echo "Could not add this file.";
         exit;
    } 
    else{
        echo  "New file successfully added.";
    }

}else{
    echo "Invalid file.";
    exit();
}
}
?>
<html>

<form action="upload.php" method="POST" enctype="multipart/form-data" name="upload_form" id="upload_form">
    <label>Title:</label><input name="title" type="text" id="title" value="<?=htmlentities(stripslashes($_POST['title']));?>" size="35">
    <label>File:</label><input type="file" name="file">    

<input type ="submit" value="submit">
</form>
<form action="download.php"  method="GET">
 <label>ID:</label><input type="number" name="id" />   <input type ="submit" value="download"/>
</form>
</html>

我已按照此链接http://mirificampress.com/permalink/saving_a_file_into_mysql上给出的教程进行操作

代码正在运行,但是当我看到表格时在此处输入图像描述

FileSize 和 File_Content 为 0。

我检查过

if ($_FILES['file']['error'] > 0)
  {
  echo "Error: " . $_FILES['file']['error'] . "<br>";
  }

然后错误是 6 即 UPLOAD_ERR_NO_TMP_DIR 但我没有得到解决方案。谁能帮我解决这个问题。任何帮助将不胜感激。

谢谢

4

1 回答 1

0
  • 重启你的apache
  • 使用 phpinfo 函数执行脚本。
  • 检查upload_tmp_dir 中的路径是否有效。(注意:它应该显示一个值)
  • 检查您是否已授予临时目录的权限。(注:许可 777)
于 2013-10-18T07:13:41.937 回答