成功上载后使用文件URL更新数据库

时间:2012-06-17 01:53:06

标签: php mysql

我已经尝试了本节中的所有内容,我知道这是我忘记的事情。无论我添加什么都会停止上传,但目前正在努力将图像保存在服务器上。

 protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index) {
    $file = new stdClass();
    $file->name = $this->trim_file_name($name, $type, $index);
    $file->size = intval($size);
    $file->type = $type;
    if ($this->validate($uploaded_file, $file, $error, $index)) {
        $this->handle_form_data($file, $index);
        $file_path = $this->options['upload_dir'].$file->name;
        $append_file = !$this->options['discard_aborted_uploads'] &&
            is_file($file_path) && $file->size > filesize($file_path);
        clearstatcache();
        if ($uploaded_file && is_uploaded_file($uploaded_file)) {
            // multipart/formdata uploads (POST method uploads)
            if ($append_file) {
                file_put_contents(
                    $file_path,
                    fopen($uploaded_file, 'r'),
                    FILE_APPEND
                );              

            } else {
                move_uploaded_file($uploaded_file, $file_path);
            }
        } else {
            // Non-multipart uploads (PUT method support)
            file_put_contents(
                $file_path,
                fopen('php://input', 'r'),
                $append_file ? FILE_APPEND : 0
            );

        }
        $file_size = filesize($file_path);
        if ($file_size === $file->size) {
            if ($this->options['orient_image']) {
                $this->orient_image($file_path);
            }
            $file->url = $this->options['upload_url'].rawurlencode($file->name);
            foreach($this->options['image_versions'] as $version => $options) {
                if ($this->create_scaled_image($file->name, $options)) {
                    if ($this->options['upload_dir'] !== $options['upload_dir']) {
                        $file->{$version.'_url'} = $options['upload_url']
                            .rawurlencode($file->name);
                    } else {
                        clearstatcache();
                        $file_size = filesize($file_path);
                    }
                }
            }
        } else if ($this->options['discard_aborted_uploads']) {
            unlink($file_path);
            $file->error = 'abort';
        }
        $file->size = $file_size;
        $this->set_file_delete_url($file);
    }
    return $file;

}

如何在上传后连接到数据库并将文件网址发布到我需要的字段中,我该怎么做呢。

我一直在上面插入它,这只会导致它不起作用:

$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 ('', 'test_upload_file_name')");

mysql_close($con);

header("Location: http://-----.com/index.php");

非常感谢。

0 个答案:

没有答案