move_uploaded_file() failed to open stream

114 Views Asked by At

I am trying to upload a file to my server, I eventually want to do some validations and resizing, but for now I just want to get it into my uploads directory. I am using wordpress and cannot get it to find any directory unless I use $_SERVER['DOCUMENT_ROOT'] which won't allow me to access the file later. Please help!

if (!empty($_FILES)) {
  $name       = $_FILES['file']['name'];
  $temp_name  = $_FILES['file']['tmp_name'];
  if(isset($name)){
    if(!empty($name)){
      $location = "wp-content/uploads/";
      if(move_uploaded_file($temp_name, $location.$name)){
        echo 'File uploaded successfully';
        echo "<img src='$location$name'/>";
      }
      else {
        echo "Not uploaded because of error #".$_FILES["file"]["error"];
      }
    }
  }  else {
    echo 'You should select a file to upload !!';
  }
}

in short, why won't my script find the correct directory? The desired directory is {Server}/wp-content/uploads

0

There are 0 best solutions below