0

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

  • `echo` out `$temp_name`, see what you get if anything. This will at least tell you where the temporary location and filename is. – GetSet Jan 30 '20 at 01:28
  • C:\Wamp\tmp\php9F5.tmp is where the temp is.... I am able to get it to save, if I use the $_SERVER['DOCUMENT_ROOT'], I don't want to use that though as it makes accessing things more difficult – Jacob Atwell Parry Jan 30 '20 at 02:04
  • The script you posted, is it located one level up from `wp-content/` ? – GetSet Jan 30 '20 at 02:09
  • The script is in wp-content/themes/{theme-name}/ – Jacob Atwell Parry Jan 30 '20 at 02:16
  • _“unless I use $_SERVER['DOCUMENT_ROOT'] which won't allow me to access the file later”_ - why wouldn’t it allow you access later …? That doesn’t make sense. _“The desired directory is {Server}/wp-content/uploads”_ - then stop messing around with path segments on your own, and use https://developer.wordpress.org/reference/functions/wp_get_upload_dir/ to begin with. – 04FS Jan 30 '20 at 09:21
  • When I say I can't access it, my $_SERVER['DOCUMENT_ROOT'] returns the C drive as part of the path, so I can't access it with jquery, which is the end goal. – Jacob Atwell Parry Jan 30 '20 at 16:53

0 Answers0