-1

I have this custom upload script running within WordPress because I don't want/need to use the build in WordPress script.
If I upload a csv file the error: The file test.csv could not be copied, try again Can't figure out why this error pops up!

Here is the script.

<?php
$updir = 'wp-content/themes/ddpp/upload_csv/';
$max_size = 100;  
$allowtype = array('csv'); 
if (isset($_FILES['field_name'])) {

if ($_FILES['field_name']['error'] > 0) {
echo 'Error: '. $_FILES['field_name']['error']. '<br />';
}
else {
// get the name, size (in kb) and type (the extension) of the file
$fname = $_FILES['field_name']['name'];
$fsize = $_FILES['field_name']['size'] / 1024;
$ftype = end(explode('.', strtolower($fname)));

// checks if the file already exists
if (file_exists($updir. $fname)) {
  echo 'The file: '. $fname. ' already exists';
}
else {
  // if the file not exists, check its type (by extension) and size
  if (in_array($ftype, $allowtype)) {
    // check the size
    if ($fsize <= $max_size) {
      // uses  function to copy the file from temporary folder to $updir
      if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) {
        echo 'The file '. $fname. ' could not be copied, try again';
      }
      else {
        echo $fname. ' ('. $fsize. ' kb) was successfully uploaded';
      }
    }
    else {
      echo 'The file '. $fname. ' exceeds the maximum permitted size, '. $max_size. ' KB';
    }
  }
  else {
    echo $fname. ' - invalid file type';
  }
}
  }
}
?>
<div style="display: inline-block; width: 25%; vertical-align: top;">
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="field_name" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>

UPDATE
Here are some warning created after upload attempt

Strict Standards: Only variables should be passed by reference in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 182

Warning: move_uploaded_file(wp-content/themes/ddpp/upload_csv/test.csv): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194

Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpvT6vFM' to 'wp-content/themes/ddpp/upload_csv/test.csv' in /Applications/MAMP/htdocs/ddpp/wp-content/plugins/ddpp_csv_upload/index.php on line 194
Interactive
  • 1,474
  • 5
  • 25
  • 57

2 Answers2

1

Let's understand why this is happening. You're getting the error The file test.csv could not be copied, try again, so the following line is the issue:

if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname))

According to the docs for move_uploaded_file(), here are the reasons why this would be false:

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

Ensure you have warnings enabled, to help you diagnose your issue further (Add this toward the top of your script):

ini_set('display_errors', 1);
error_reporting(E_ALL);

The most common problem is that your file directory is not writable, as PHP cannot create the filename at the destination directory. Ensure the directory exists, and the PHP process can write to that folder.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

The code is failing here

  if (!move_uploaded_file ($_FILES['field_name']['tmp_name'], $updir. $fname)) {
    echo 'The file '. $fname. ' could not be copied, try again';
  }

This means that move_uploaded_file is failing to do what you need it to do. Check your php error log and there will likely be an error about permissions. You might have created the directory using FTP or something and as a result, apache is unable to write to the directory.

In order to give apache access you have 2 options. Add apache to the group that is allowed to write, or simply make apache the owner. I always find that if it is a directory for a website, its no harm making apache the ownser. If you have SSH access run the following command.

chown -R apache /wherever/the/dir/is
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71