为什么我的变量在被传递一个值时被视为空值?
问题描述:
使用file_get_contents()
函数定义$image
变量之后我已经得到这样的警告:为什么我的变量在被传递一个值时被视为空值?
警告:file_get_contents()函数:文件名不能为空
即使我通过了一项价值$formname
与此方法调用:
Image::uploadImage('postimg', "UPDATE dry_posts SET postimg = :postimg WHERE id = :postid", array(':postid' => $postid),array(':postimg' => $postimg));
$postimg
是文件变量的形式。我试着检查文件是否存在,它解决了错误,但当然没有任何执行。它似乎不喜欢它,只要我使用file_get_contents()
,我该如何解决这个问题?
<?php
include_once("connect.php");
class Image
{
public static function uploadImage($formname,$query,$params)
{
$formname = "";
$response = "";
$image = "";
echo 'hello';
//if(file_exists($formname))
//{
echo 'hello';
$image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
//}
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer access code here\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))
//{
echo 'hell0';
$response = file_get_contents($imgurURL, false, $context);
//}
$response = json_decode($response);
//from
//$prearams = array($formname=>$response->data->link);
//$params = $preparams + $params;
//from changes
//$params=array(':postid'=>$postid);
$params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);
connect::query($query,$params);
}
}
?>
答
您是不是在此设置“$ formname”? $ formname =“”;因此,如果您在方法调用中传递它,并不重要,它将始终为空
但是您在此处取消设置“$ formname”? $ formname =“”;因此,如果您在方法调用中传递它并不重要,它将始终为空。 – espradley
哇我想我只是需要一双新鲜的眼睛 – Bubba
谢谢,只是添加它作为正式答案。如果您将其标记为解决方案,将不胜感激 – espradley