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