简体   繁体   中英

PHP move_uploaded_file not uploading

I'm trying to upload an image on blog but it just doesn't do it. No error mesgs, just doesn't upload it. Checked permissions on the folder and they work when i place the image in the images folder but not when adding a post. Below is the code im using.

Any help is greatly appreciated.

$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);

}

Replace

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

with

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

Change these

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

to these

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

Tested and works fine now!

you can use below code for acessing images folder

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

but you should make sure images folder exists in same folder as the php file. If images folder is in an higher directory than your php file then use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM