I've reviewed previous solutions here, but none fixed it for me. Here's my PHP 7 code:
<?php
$file_size = $_FILES['fileToUpload']['size'];
$file_types = array("sh", "txt", "py", "php", "html", "css", "rb");
$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
$ext = strtolower($ext);
function toText() {
$content = file_get_contents($_FILES['fileToUpload']['tmp_name']);
return $content;
}
function uploadFile() {
$myfile = fopen("uploads/".$_FILES['fileToUpload']['name'], 'w+');
//if (!$myfile) {
// return "There was an error writing the file.";
// }
$link = "http://[site]"."/uploads/".$_FILES['fileToUpload']['name'];
fwrite($myfile, toText());
fclose($myfile);
return $link;
}
if ($file_size / 1024 > 200000) {
echo "File too big";
}
if (in_array($ext, $file_types)) {
echo uploadFile();
}
else {
echo "File not a valid extension.";
echo "\nExtension: ".$ext;
}
Here are the errors:
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in [file] on line 18, referer: [site]
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in [file] on line 19, referer: [site]
I've concluded that fopen is false (see commented out testing).
This is running on an Ubuntu 17.04 VPS, on Apache. PHP.ini has file upload enabled, and both uploads
and the script have permissions 755.