Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用图像上载脚本上载文件_Php_Mysql - Fatal编程技术网

Php 使用图像上载脚本上载文件

Php 使用图像上载脚本上载文件,php,mysql,Php,Mysql,我正在尝试制作一个简单的上传脚本,它有一个图像上传部分和一个文件上传部分。我想尝试将图像和文件以及一些其他变量存储在同一个数据库中,但我很难做到这一点。我想知道是否有人能帮我解决这个难题 <form method="post" action="insert_file.php" enctype='multipart/form-data'> 索引: <body> <form method="post" action="insert_file.php">

我正在尝试制作一个简单的上传脚本,它有一个图像上传部分和一个文件上传部分。我想尝试将图像和文件以及一些其他变量存储在同一个数据库中,但我很难做到这一点。我想知道是否有人能帮我解决这个难题

 <form method="post" action="insert_file.php" enctype='multipart/form-data'>
索引:

<body>
    <form method="post" action="insert_file.php">
        <table>
            <tr><td>Title:</td><td><input type="text" name="title" /></td></tr>
            <tr><td>Author:</td><td><input type="text" name="author"/></td></tr>
            <tr><td>Description:</td><td><textarea cols="30" rows="10" name="description"></textarea></td></tr>
            <tr><td>Category:</td>
                <td>
                    <select name="category">
                        <option value="poker">Poker</option>
                        <option value="sportsbetting">Sports Betting</option>
                        <option value="financialbetting">Financial Betting</option>
                        <option value="casino">Casino</option>
                        <option value="bingo">Bingo</option>
                        <option value="socialgaming">Social Gaming</option>
                        <option value="affiliatemarketing">Affiliate Marketing</option>
                    </select>
                </td>
            </tr>
            <tr><td>Publication Date:</td><td><input type="text" name="pub_date" id="datepicker"/></td></tr>
            <tr><td>Tags:</td><td><input type="text" name="tags"/></td></tr>
            <tr><td>Price:</td><td><input type="text" name="price"/></td></tr>
            <tr><td>Image:</td><td><input type="file" name="image"/></td></tr>
            <tr><td>Website:</td><td><input type="text" name="website"/></td></tr>
            <tr><td>Contact Email:</td><td><input type="text" name="email"/></td></tr>
            <tr><td>File:</td><td><input type="file" name="uploaded_file"/></td></tr>
            <tr><td></td><td><input type="submit" value="Submit"/></td></tr>
        </table>
    </form>
</body>

标题:
作者:
说明:
类别:
扑克
体育博彩
金融博彩
赌场
答对 了
社交游戏
联盟营销
出版日期:
标签:
价格:
图片:
网站:
联络电邮:
文件:

插入:

<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        include('../config.inc');
        // Connect to the database
        $dbLink = $con;     

        // Gather all required data
        $username       = $_SESSION['username'];            
        $title          = $_POST['title'];
        $author         = $_POST['author'];
        $description    = $_POST['description'];
        $category       = $_POST['category'];
        $pub_date       = $_POST['pub_date'];
        $tags           = $_POST['tags'];
        $price          = $_POST['price'];
        $website        = $_POST['website'];
        $email          = $_POST['email'];
        $name           = $title;
        $mime           = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data           = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size           = intval($_FILES['uploaded_file']['size']);

        // Create the SQL query
        $query = "
            INSERT INTO `file2` (
               `username`, 
               `title`, 
               `author`,
               `description`, 
               `category`, 
               `pub_date` ,
               `tags`, 
               `price`, 
               `website`,
               `email`,  
               `name`, 
               `mime`, 
               `size`, 
               `data`, 
               `created`
            )
            VALUES (
               '{$username}', 
               '{$title}',
               '{$author}', 
               '{$description}',
               '{$category}',
               '{$pub_date}',
               '{$tags}',
               '{$price}',
               '{$website}',
               '{$email}',
               '{$name}', 
               '{$mime}', 
               '{$size}', 
               '{$data}',
                NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);

        // Check if it was successfull
        if($result) {
            echo '<center>Success! Your file was successfully added!';
        }
        else {
            echo '<center>Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<center><font face=arial>';
echo 'You file has been uploaded successfully, please allow upto 24 Hours for your report to be approved by administration. ';
echo '<p>Click <a href="index.php">here</a> to go back</p>';
?>

要获得要工作的
文件
输入,您需要指定表单的
enctype
。您需要
enctype=“多部分/表单数据”
。有关更多信息,请参阅。


 <form method="post" action="insert_file.php" enctype='multipart/form-data'>
原因: 当您发出POST请求时,您必须以某种方式对构成请求主体的数据进行编码

 <form method="post" action="insert_file.php" enctype='multipart/form-data'>

HTML表单提供两种编码方法。默认值为application/x-www-form-urlencoded,与URL末尾的查询字符串大致相同。另一种是多部分/表单数据,它是一种更复杂的编码,但它允许在数据中包含整个文件。(HTML5引入了文本/普通编码,它只对调试有用…即使是其他的也最好提供合理的调试工具)。

。。。享受您的服务器pwn3d。缺少一些元素和一些神奇的词语。@我需要放置MarcB来防止这些攻击吗?如何将图像插入数据库?
 <form method="post" action="insert_file.php" enctype='multipart/form-data'>