J. Doe doe doe doe
J. Doe doe doe doe

Reputation: 69

Trying to upload an image. But I get errors

I am trying to upload an image but I am getting errors. Here is my code so far

<?php
    $servername = "localhost"; 
    $username = "root"; 
    $password = "";  
    $dbname = ""; //Taken out for stackoverflow question

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    $dir = "ads/";
    $file = $dir . basename($_FILES['cf']['name']);
    $uploadSuccess = 1;
    $imageFileType = pathinfo($file, PATHINFO_EXTENSION);

    if (!$conn)
    {
        echo 'Could not connect';
    }
    else 
    {
        if (!empty($_FILES['cf'] && $_POST['category']))
        {
            $filesize = getimagesize($_FILES['cf']['tmp_name']);
            if ($filesize != false)
            {
                if (file_exists($file))
                {
                    echo 'File already exists';
                    $uploadSuccess = 0;
                }
                if ($_FILES['cf']['size'] > 500000)
                {
                    echo 'Cannot use that large of a file';
                    $uploadSuccess = 0;
                }
                if ($imageFileType != 'jpg' && $imageFileType != 'png' && $imageFileType != 'jpeg')
                {
                    echo 'Only jpg, png, and jpeg files are allowed';
                    $uploadSuccess = 0;
                }
                if ($uploadSuccess == 0)
                {
                    echo 'Sorry, your file was not uploaded';
                }
                else
                {
                    if (move_uploaded_file($_FILES['cf']['tmp_name'], $file))
                    {
                        echo 'The file: '.basename($_FILES['cf']['name'])." has been uploaded";
                    }
                    else 
                    {
                        echo 'Sorry, there was an error uploading your file';
                    }
                }
            }
            else 
            {
                $uploadSuccess = 0;
            }
        }
    }
?>

That code isn't working. I have been on this for two days. First trying to get it into a database. Now a directory. Here is the errors localhost gave me.

Errors given by localhost

Upvotes: 0

Views: 135

Answers (2)

Maksym Semenykhin
Maksym Semenykhin

Reputation: 1945

you need to check if folder exist

php code:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = ""; //Taken out for stackoverflow question

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    $rootPath = dirname(__FILE__);
    $dir = $rootPath. DIRECTORY_SEPARATOR ."ads";


    $file = 'ads'.DIRECTORY_SEPARATOR .basename($_FILES['cf']['name']);
    $fileNewName = $dir.DIRECTORY_SEPARATOR.$_SERVER['REQUEST_TIME_FLOAT'].'_'.rand(1,9999) .'_'. basename($_FILES['cf']['name']);
    $uploadSuccess = 1;
    $imageFileType = pathinfo($file, PATHINFO_EXTENSION);

    if (!$conn)
    {
        echo 'Could not connect';
    }
    else
    {
        if (!empty($_FILES['cf'] && $_POST['category']))
        {
            $filesize = getimagesize($_FILES['cf']['tmp_name']);
            if ($filesize != false)
            {
                if (file_exists($fileNewName))
                {
                    echo 'File already exists';
                    $uploadSuccess = 0;
                }
                if ($_FILES['cf']['size'] > 500000)
                {
                    echo 'Cannot use that large of a file';
                    $uploadSuccess = 0;
                }
                if ($imageFileType != 'jpg' && $imageFileType != 'png' && $imageFileType != 'jpeg')
                {
                    echo 'Only jpg, png, and jpeg files are allowed';
                    $uploadSuccess = 0;
                }
                if ($uploadSuccess == 0)
                {
                    echo 'Sorry, your file was not uploaded';
                }
                else
                {

                    if (!is_dir($dir)){

                        if (!mkdir($dir, 0777, true)) {
                            die('Sorry, failed to create folder'.$dir );
                        }
                    }

                    if (!is_writable($dir)) {
                        die( 'folder is not writeble'.dir);
                    }


                    if (move_uploaded_file($_FILES['cf']['tmp_name'], $fileNewName))
                    {
                        echo 'The file: '.basename($_FILES['cf']['name'])." has been uploaded";
                    }
                    else
                    {
                        echo 'Sorry, there was an error uploading your file';
                    }
                }
            }
            else
            {
                $uploadSuccess = 0;
            }
        }
    }

exit;
?>

Upvotes: 3

franbenz
franbenz

Reputation: 712

You should check that 'ads/' directory exists and your web server user has write permissions on it.

Upvotes: 0

Related Questions