PHP move_uploaded_file没有上传

时间:2017-10-02 21:53:07

标签: php

我正在尝试在博客上传图片,但它不会这样做。没有错误消息,只是不上传它。检查文件夹的权限,当我将图像放在images文件夹中时它们工作,但是在添加帖子时没有。 以下是我正在使用的代码。

非常感谢任何帮助。

$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];

$post_content = mysqli_real_escape_string($connection, $_POST['post_content']);
$post_tags = mysqli_real_escape_string($connection, $_POST['post_tags']);
$post_status = mysqli_real_escape_string($connection, $_POST['post_status']);

// $post_comment_count = 4; //THIS NEEDS TO BE DYNAMIC

move_uploaded_file($post_image_temp,"../images/".$post_image) ;
// if(!move_uploaded_file($post_image_temp,"../images/".$post_image)){
//   echo 'Moved successfully:';
// }else{
//   print_r(error_get_last());
// }

$query = "INSERT INTO posts(post_category_id, post_title, post_author, post_date, post_image, post_content, post_tags, post_status)";
$query .= "VALUES ({$post_category_id},'{$post_title}','{$post_author}',now(),'{$post_image}','{$post_content}','{$post_tags}','{$post_status}') ";

      $create_post_query = mysqli_query($connection, $query);
      confirm($create_post_query);

}

3 个答案:

答案 0 :(得分:0)

替换

move_uploaded_file($post_image_temp,"../images/".$post_image) ;

move_uploaded_file(
    $post_image_temp,
    __DIR__ . "/../images/" . $post_image
);

答案 1 :(得分:0)

更改这些

move_uploaded_file("$post_image_temp,","../images/" . $post_image) ;
if(!move_uploaded_file($post_image_temp,"../images/".$post_image)){

到这些

move_uploaded_file("$post_image_temp,", "images/" . $post_image) ;
if(move_uploaded_file($post_image_temp, "images/" . $post_image)){

现在测试并正常工作!

答案 2 :(得分:-1)

您可以使用以下代码获取图像文件夹

move_uploaded_file($post_image_temp,dirname(__FILE__)."/images/".$post_image) ;

但是你应该确保images文件夹与php文件存在于同一个文件夹中。如果images文件夹位于比php文件更高的目录中,则使用

dirname(__FILE__)."../images/"