0

i am writing a php page that let the user to upload his profile image to the database sql but i am getting these errors i tried many solutions i found online but i couldn't know how to fix my code , i would really appreciate your time i am a new developer . thank you in advance :

the errors are :

( ! ) Warning: file_get_contents(https://api.imgur.com/3/image): failed to open stream: HTTP request failed! HTTP/1.1 403 Permission Denied in C:\wamp64\www\final-project\classes\Image.php on line 22
Call Stack
#   Time    Memory  Function    Location
1   0.0009  381752  {main}( )   ...\my-account.php:0
2   0.0175  439648  Image::uploadImage( )   ...\my-account.php:33
3   0.0224  789608  file_get_contents ( )   ...\Image.php:22

( ! ) Notice: Trying to get property of non-object in C:\wamp64\www\final-project\classes\Image.php on line 25
Call Stack
#   Time    Memory  Function    Location
1   0.0009  381752  {main}( )   ...\my-account.php:0
2   0.0175  439648  Image::uploadImage( )   ...\my-account.php:33

( ! ) Notice: Trying to get property of non-object in C:\wamp64\www\final-project\classes\Image.php on line 25
Call Stack
#   Time    Memory  Function    Location
1   0.0009  381752  {main}( )   ...\my-account.php:0
2   0.0175  439648  Image::uploadImage( )   ...\my-account.php:33

./classes/Image.php:

<?php
class Image {

        public static function uploadImage($formname, $query, $params) {
                $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));

                $options = array('http'=>array(
                        'method'=>"POST",
                        'header'=>"Authorization: Bearer a5d4b7d7eb41f3c2049e1212c628996839cc3066\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!');
                }

                $response = file_get_contents($imgurURL, false, $context);
                $response = json_decode($response);

                $preparams = array($formname=>$response->data->link);

                $params = $preparams + $params;

                DB::query($query, $params);

        }

}
?>

this is the php page given to the user so he can upload his profile picture to the database.

if (Login::isLoggedIn()) {
        $userid = Login::isLoggedIn();
} else {
        die('Not logged in!');
}

if (isset($_POST['uploadprofileimg'])) {

        Image::uploadImage('profileimg', "UPDATE users SET profileimg = :profileimg WHERE id=:userid", array(':userid'=>$userid));
        Image();
}
?>
karim
  • 1
  • 1
  • 1
  • 7
  • Please take a look at my answer to this question here: [Full Secure Image Upload Script](https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921). It will teach you how to securily work with images uploaded by users, including the use of a database. At the end of my answer you will find a ready-to-use class that you can download and put to use straight away without all the difficult stuff if you want to. – icecub Oct 31 '17 at 22:47
  • thank you icecub i will check it now :) – karim Oct 31 '17 at 22:49
  • what does `api.imgur.com` have to do with a user uploading a file and you storing it in your db ? –  Oct 31 '17 at 23:14
  • well i don't really know i got guidance from my friend with the image upload as i said i am new in coding :) – karim Oct 31 '17 at 23:20
  • this code is trying to save the file to imgur, not your db –  Oct 31 '17 at 23:48

0 Answers0