PHP上传到数据库

PHP上传到数据库,php,mysql,forms,upload,Php,Mysql,Forms,Upload,我正试图把东西上传到数据库。我看了一些教程,但没有一个奏效。我想将图像和文本文档(包括PowerPoint演示文稿)等文件上载到数据库 这是我的表格 <form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform"> <input type="hidden" name="MAX_FILE_SIZE" value="350000"> <inpu

我正试图把东西上传到数据库。我看了一些教程,但没有一个奏效。我想将图像和文本文档(包括PowerPoint演示文稿)等文件上载到数据库

这是我的表格

<form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform">
    <input type="hidden" name="MAX_FILE_SIZE" value="350000">
    <input name="picture" type="file" id="picture" size="50">
    <input name="upload" type="submit" id="upload" value="Upload Picture!">
</form>

这是upload.php

<?php
// if something was posted, start the process...
if(isset($_POST['upload']))
{
    // define the posted file into variables
    $name = $_FILES['picture']['name'];
    $tmp_name = $_FILES['picture']['tmp_name'];
    $type = $_FILES['picture']['type'];
    $size = $_FILES['picture']['size'];

    // get the width & height of the file (we don't need the other stuff)
    list($width, $height, $typeb, $attr) = getimagesize($tmp_name);

    // if width is over 600 px or height is over 500 px, kill it    
    if($width>600 || $height>500)
    {
        echo $name . "'s dimensions exceed the 600x500 pixel limit.";
        echo '<a href="form.html">Click here</a> to try again.';
        die();
    }

    // if the mime type is anything other than what we specify below, kill it    
    if(!($type=='image/jpeg' || $type=='image/png' || $type=='image/gif')) 
    {
        echo $type .  " is not an acceptable format.";
        echo '<a href="form.html">Click here</a> to try again.' ;
        die();
    }

    // if the file size is larger than 350 KB, kill it
    if($size>'350000') {
        echo $name . " is over 350KB. Please make it smaller.";
        echo '<a href="form.html">Click here</a> to try again.' ;
        die();
    } 

    // if your server has magic quotes turned off, add slashes manually
    if(!get_magic_quotes_gpc()){
        $name = addslashes($name);
    }

    // open up the file and extract the data/content from it
    $extract = fopen($tmp_name, 'r');
    $content = fread($extract, $size);
    $content = addslashes($content);
    fclose($extract);  

    // connect to the database
    include "inc/db.inc.php";

    // the query that will add this to the database
    $addfile = "INSERT INTO files (name, size, type, content ) ".
        "VALUES ('$name', '$size', '$type', '$content')";

    mysql_query($addfile) or die(mysql_error());

    // get the last inserted ID if we're going to display this image next
    $inserted_fid = mysql_insert_id();

    mysql_close(); 

    echo "Successfully uploaded your picture!";

    // we still have to close the original IF statement. If there was nothing posted, kill the page.
}
else{
    die("No uploaded file present");
}
?>  

您的代码从根本上被破坏了:

1) 您只需假设已执行上载,而从不检查失败。至少你应该有

if ($_FILES['picture']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['picture']['error']);
}
此处定义了错误代码:

2) addslashes()提供的防御SQL注入攻击的能力与使用一张正方形的湿厕纸使湖水干涸的能力相当。因为您使用的是mysql库,所以必须使用mysql\u real\u escape\u string()来正确地转义数据

3) 您使用的是mysql库,它已经过时,不推荐使用。停止使用它。改用mysqli或PDO


4) 您的实际错误消息表明您从未调用过
mysql\u select\u db()
来设置默认数据库。您只需将查询修改为
INSERT-INTO-name\u of\u db.name\u of\u table…

您的代码基本上已被破坏:

1) 您只需假设已执行上载,而从不检查失败。至少你应该有

if ($_FILES['picture']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['picture']['error']);
}
此处定义了错误代码:

2) addslashes()提供的防御SQL注入攻击的能力与使用一张正方形的湿厕纸使湖水干涸的能力相当。因为您使用的是mysql库,所以必须使用mysql\u real\u escape\u string()来正确地转义数据

3) 您使用的是mysql库,它已经过时,不推荐使用。停止使用它。改用mysqli或PDO


4) 您的实际错误消息表明您从未调用过
mysql\u select\u db()
来设置默认数据库。您只需将查询修改为
INSERT-INTO-name\u of_db.name\u of_table…

就可以解决这个问题,确保您在
inc/db.inc.php
文件中正确调用了
mysql\u-select\u-db()

在下面的代码中,您只是简单地回显文本,而不执行任何检查。无论成功与否,都将显示成功消息

echo "Successfully uploaded your picture!";

确保在
inc/db.inc.php
文件中正确调用了
mysql\u select\u db()

在下面的代码中,您只是简单地回显文本,而不执行任何检查。无论成功与否,都将显示成功消息

echo "Successfully uploaded your picture!";

inc/db.inc.php
的内容是什么?你确定那个文件被正确地包含了吗?如果将include更改为
require
,代码是否仍然有效?上面的代码似乎还可以,我认为错误可能与include“inc/db.inc.php”有关;未选择数据库通常表示您未选择数据库错误表示您在调用
mysql\u-select\u-db()
后从未调用过
mysql\u-connect()
。一个好的教程会很有帮助…
inc/db.inc.php
的内容是什么?你确定那个文件被正确地包含了吗?如果将include更改为
require
,代码是否仍然有效?上面的代码似乎还可以,我认为错误可能与include“inc/db.inc.php”有关;未选择数据库通常表示您未选择数据库错误表示您在调用
mysql\u-select\u-db()
后从未调用过
mysql\u-connect()
。一个好的教程将对您有所帮助。。。