0

I have a an input type file which is hidden and triggered using another button .. the input must upload images only to a folder named Covers but the code is not working and not uploading any image..
html code

<div class="cover">
    <img src="Layout/images/cover.jpg" alt="cover" name="cover-img" class="cover-img"> 
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
        <button type="submit" name="submit-cover" id="cover-btn">Change Cover</button>
    <input type="file" name="avatar" id="cover-img-input" class="hidden" />
</form>


php codes:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['submit-cover'])) {
        $avatarName = $_FILES['avatar']['name'];
        $avatarTempName = $_FILES['avatar']['tmp_name'];

        // List of allowed image extensions
        $avatarAllowedExtensions = array("jpeg","jpg","png","gif");

        // Get avatar extension
        $avatarExtension = strtolower(end(explode('.',$avatarName)));

        // Check if uploaded image extension is in allowed image extensions
        $formErrors=array();
        if(! empty($avatarName) && ! in_array($avatarExtension, $avatarAllowedExtensions)) {
            $formErrors[]='This extension is <strong>not allowed</strong>';
        }
        if(empty($avatarName)) {
            $formErrors[]='No image <strong>uploaded</strong>';                                         
        }

        if(empty($formErrors)) {
            // Create random number between zero to million to concatinate it with image name
            $avatar = rand(0,1000000) . '_' . $avatarName;

            // Move image into Covers folder
            move_uploaded_file($avatarTempName, "Uploads\Covers\\" . $avatar);

        }
    }
}

I get

Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in D:\XAMPP\htdocs\Warina\connect.php on line 7

after that I searched for a solution and get this: extension=php_pdo_mysql.dll should be uncommented in my php.ini and it is uncommented now I'm confused about this error too.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Raizada Dev
  • 63
  • 11
  • Have you checked your error log if there are any clues about what's going on? A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Aug 31 '18 at 19:39
  • Did you add the error display code suggested in @MagnusEriksson comment yet? – RiggsFolly Aug 31 '18 at 19:54
  • @RiggsFolly yes I did what MagnusEriksson said and I got a fatel error says Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in D:\XAMPP\htdocs\Warina\connect.php on line 7 after that I searched for a solution and get this: extension=php_pdo_mysql.dll should bu uncommented in my php.ini and it's uncommented now I'm confused about this error too.. can you give me a solution – Raizada Dev Sep 01 '18 at 09:13
  • So you had better show us `connect.php` and any code that attempts to access the database in this script – RiggsFolly Sep 01 '18 at 10:59
  • Also run a little script with just this in the script. `phpinfo()` and look for the parameter `Loaded Configuration File` and check that is the `php.ini` file you have look at – RiggsFolly Sep 01 '18 at 11:02

1 Answers1

0

This line sends a notice:

$avatarExtension = strtolower(end(explode('.',$avatarName)));

Notice: Only variables should be passed by reference in file.php on line xx

Replace with

$avatarExtension = explode('.',$avatarName);
$avatarExtension = strtolower(end($avatarExtension));

Fix this path:

move_uploaded_file($avatarTempName, "Uploads\Covers\\" . $avatar);

With

move_uploaded_file($avatarTempName, "Uploads\\Covers\\" . $avatar);

And make sure it exists

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • The path exists I'm sure of that and also I replaced my lines with yours but still the problem exist and no image uploaded – Raizada Dev Aug 31 '18 at 19:38
  • 1
    Are you using a *nix where directory names are case sensitive, if so do the actual directory names contain a capital letter at the beginning – RiggsFolly Aug 31 '18 at 19:53
  • 1
    Its also simpler to use `/` rather than the escape character `\\` regardless of OS. PHP will work out what to actually use internally on any OS – RiggsFolly Aug 31 '18 at 19:55