试图上传图片。但是我收到了错误

时间:2016-06-25 14:27:02

标签: php

我正在尝试上传图片,但我收到错误。这是我到目前为止的代码

<?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;
            }
        }
    }
?>

该代码不起作用。我已经在这两天了。首先尝试将其放入数据库。现在是一个目录。这是localhost给我的错误。

Errors given by localhost

2 个答案:

答案 0 :(得分:3)

你需要检查 如果文件夹存在

php代码:

<?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;
?>

答案 1 :(得分:0)

你应该检查&#39; ads /&#39;目录存在,您的Web服务器用户具有写入权限。