Hi all
I’m hoping someone can help me please. I have a PHP model and controller that upload image files to a folder from a html form and it works perfectly, even renaming the file. However, I now want to store the path to the image and some other info in the database and that’s where I am stuck. The image gets renamed and uploaded but nothing saved to db. I get this error message:
‘Too few arguments to function controllers\Profiles\bits\AvatarCtrlr::uploadAction(), 0 passed and exactly 1 expected’
Here is my code thus far:
FROM THE MODEL:
public static function uploadAvatar($data) {
try {
// File Route.
$fileRoute = "/avatars/";
$fieldname = "avatarPic";
// Get filename.
$filename = explode(".", $_FILES[$fieldname]["name"]);
// Validate uploaded files.
// Do not use $_FILES["file"]["type"] as it can be easily forged.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// Get temp file name.
$tmpName = $_FILES[$fieldname]["tmp_name"];
// Get mime type.
$mimeType = finfo_file($finfo, $tmpName);
// Get extension. You must include fileinfo PHP extension.
$extension = end($filename);
// Allowed extensions.
$allowedExts = array("gif", "jpeg", "jpg", "png", "svg", "blob");
// Allowed mime types.
$allowedMimeTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml");
// Validate image.
if (!in_array(strtolower($mimeType), $allowedMimeTypes) || !in_array(strtolower($extension), $allowedExts)) {
throw new \Exception("File does not meet the validation.");
}
// Generate new random name.
$name = sha1(microtime()) . "." . $extension;
$fullNamePath = "./images/users". $fileRoute . $name;
// Check server protocol and load resources accordingly.
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {
$protocol = "https://";
} else {
$protocol = "http://";
}
// Save file in the uploads folder.
move_uploaded_file($tmpName, $fullNamePath);
// Generate response.
$response = new \StdClass;
$response->link = $protocol.$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]).$fileRoute . $name;
// Send response.
echo stripslashes(json_encode($response));
$db = static::getDBProfiles($fullNamePath);
$sql = 'INSERT INTO profileAvatars (avatarUID, avatarAlias, avatarPath, avatarCreated)
VALUES (:avatarUID, :avatarAlias, :avatarPath, :avatarCreated)';
$stmt = $db->prepare($sql);
$stmt->bindValue(':avatarUID', $data['avatarUID'], PDO::PARAM_INT);
$stmt->bindValue(':avatarAlias', $data['avatarAlias'], PDO::PARAM_STR);
$stmt->bindValue(':avatarPath', $data['avatarPath'], PDO::PARAM_LOB);
$stmt->bindValue(':avatarCreated', date('Y-m-d H:i:s', time()), PDO::PARAM_STR);
return $stmt->execute();
} catch (PDOException $e) {
// Send error response.
echo $e->getMessage();
http_response_code(404);
}
}
FROM THE CONTROLLER:
public function uploadAction() {
if (AvatarModel::uploadAvatar($_POST)) {
Flash::addMessage('You have successfully created a New Avatar');
$this->redirect('/avatars');
} else {
View::renderTemplate('global/profiles/bits/avatars/new.html');
}
}
Thank you