上传后获取图片网址发布到MySQL

时间:2012-06-17 21:22:25

标签: php mysql

所以这个语法确实连接并获取发布到数据库的信息,但我无法弄清楚如何将图像的URL发布到数据库。无论我将test1更改为它都不会发布。

protected function trim_file_name($name, $type, $index) {
    // Remove path information and dots around the filename, to prevent uploading
    // into different directories or replacing hidden system files.
    // Also remove control characters and spaces (\x00..\x20) around the filename:
    $file_name = trim(basename(stripslashes($name)), ".\x00..\x20");
    // Add missing file extension for known image types:
    if (strpos($file_name, '.') === false &&
        preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
        $file_name .= '.'.$matches[1];
    }
    if ($this->options['discard_aborted_uploads']) {
        while(is_file($this->options['upload_dir'].$file_name)) {
            $file_name = $this->upcount_name($file_name);
        }
    }
    $con = mysql_connect("localhost","-----","-----");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("-----", $con);

    mysql_query("INSERT INTO posts (postid, post_content)
    VALUES ('', 'test1')");

    mysql_close($con);
    {
    header("Location: http://snarb.com/index.php");
    }
    return $file_name;

}

1 个答案:

答案 0 :(得分:0)

将mysql_query行更改为:

 mysql_query("INSERT INTO posts (post_content) VALUES ('" . mysql_real_escape_string($file_name) . "')");