【问题标题】:API for uploading image is not working用于上传图像的 API 不起作用
【发布时间】:2017-11-16 04:06:49
【问题描述】:

我已经创建了一个 API,因此 android 应用程序可以连接到网站我面临的问题是我无法提供访问权限来提供 API 以将图像上传到服务器,因为我现在已经创建,所以图像将保存为 base64代码,但主要问题是,当进行每个 API 调用并上传图像时,它会给我一个错误,即找不到无法加载文件流的图像。这是我遇到的错误

<b>Warning</b>: fopen(uploads/testimg.png): failed to open stream: No such 

file or directory in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>87</b><br />
<br />
<b>Warning</b>: fwrite() expects parameter 1 to be resource, boolean given in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>88</b><br />
<br />
<b>Warning</b>: fclose() expects parameter 1 to be resource, boolean given in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>89</b><br />
{"result":1,"message":"Image upload complete, Please check your php file directory"}

这是我创建的将图像上传到服务器的功能

function upload_photo($connect, $base, $filename) {     
        $binary   = base64_decode($base);

        $query = $connect->prepare("INSERT INTO test_photo (filename) VALUES (:filename)");
        $query->execute(array(':filename' => $filename));

        header('Content-Type: bitmap; charset=utf-8');

        $file = fopen('uploads/'.$filename, 'wb');
        fwrite($file, $binary);
        fclose($file);
        $err['result']  = 1;
        $err['message'] = 'Image upload complete, Please check your php file directory';
        return json_encode($err);
    }

处理请求的主要 api 文件

    switch($action)
  {
  case 'upload_photo':
    $base     = $_REQUEST['image'];
    $filename = $_REQUEST['filename'];

    echo $retval = $photo->upload_photo($connect, $base, $filename);
break;

default:
    echo "Invalid Request";
break;

}

这是用来拨打电话的网址

www.domainname.com/api/api.php?action=upload_photo&fimename=(filename)&image=iamge nae

谁能帮我解决这个错误

【问题讨论】:

  • 没有人帮助我:-(

标签: php android image api


【解决方案1】:

如果您的 PHP 文件包含在位于其他位置的另一个文件中,那么代码将使用 那个 文件路径作为基础。

Include 基本上将包含文件的内容“复制”到您的脚本中,这使得路径相对于父脚本,而不是包含的脚本。

要解决此问题,您可以使用魔术常量__DIR__,它返回文件本身的绝对路径。

如此变化:

fopen('uploads/' . $filename, 'wb')

fopen(__DIR__ . '/uploads/' . $filename, 'wb')

应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2013-11-06
    • 2015-02-08
    相关资源
    最近更新 更多