PHP move_uploaded_file is not working

I am not sure what is wrong here but I know it has to do with move_uploaded_file. I have tried changing the name that the file gets changed to and I have changed the chmod of the directory to 777. From what I can tell there is no reason why the file isn’t getting moved, but it isn’t.

Here is the code in question. Keep in mind this is part of a WordPress plugin, so I use functions from there in my code.

if ( isset($_FILES["file"])) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"];
  } else {
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } elseif(strtolower(end(explode('.', $_FILES['file']['name']))) != "csv") {
      echo "File is not a .csv";
      } else {
      if(move_uploaded_file($_FILES["file"]["tmp_name"], plugin_dir_path( __FILE__ ) . "uploads/uploaded_file.csv")) {
        echo "Stored in: " . plugin_dir_path( __FILE__ ) . "upload/uploaded_file.csv<br />";
      } else {
        echo "Temp file was not moved.<br />";
        echo '<pre>';
        echo 'Here is some more debugging info:<br />';
        print "</pre>";

echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<input type="file" name="file" id="file" />';
submit_button('Import CSV');
echo '</form>';

Read more here: PHP move_uploaded_file is not working

Leave a Reply

Your email address will not be published. Required fields are marked *