这是我的代码,它不喜欢我从头文件调用方法的最后一行。它声称无效的参数号,即使我调用的方法采用相同数量的参数。我不明白参数是未定义的部分因为$ params是在调用之前定义的,而$ query是从我的其他文件中传递的值,我应该检查它是否已设置?该文件应该将图像从另一个文件中的表单上传到imgur。
<?php
include_once("connect.php");
class Image
{
public static function uploadImage($formname,$query,$params)
{
$formname = "";
$response = "";
$image = "";
if(file_exists($formname))
{
$image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
}
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer n/a\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
if ($_FILES[$formname]['size'] > 10240000) {
die('Image too big, must be 10MB or less!');
}
if(file_exists($formname))
{
$response = file_get_contents($imgurURL, false, $context);
}
$response = json_decode($response);
$preparams = array($formname=>$response->data->link);
$params = $preparams + $params;
connect::query($query,$params);
}
}
?>
Here's my call to the uploadImage method with the value of $query
$postid = connect::query('SELECT id FROM dry_posts ORDER BY ID DESC LIMIT 1');
Image::uploadImage('postimg', "UPDATE dry_posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
这是我的(connect.php)头文件:
<?php
class connect
{
private static function db()
{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=mysql;charset = utf8', 'adereje','Abubba23');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query,$params = array())
{
$statement = self :: db()->prepare($query);
$statement->execute($params);
if(explode(' ',$query)[0] == 'SELECT')
{
$data = $statement->fetchAll();
return $data;
}
}
}