Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
制作一个目录,并用PHP上传其中的文件_Php_Mysql - Fatal编程技术网

制作一个目录,并用PHP上传其中的文件

制作一个目录,并用PHP上传其中的文件,php,mysql,Php,Mysql,我已尝试使用此代码上载文件。我正在尝试使用rand()+time()创建目录并上载其中的文件。 代码有什么问题 if(!empty($_POST['title']) ) { if(isset( $_FILES['img']['name']) && ( $_FILES['img']['type'] == "image/jpeg" || $_FILES['img']['type'] == "image/jpg"

我已尝试使用此代码上载文件。我正在尝试使用
rand()+time()
创建目录并上载其中的文件。 代码有什么问题

if(!empty($_POST['title']) ) {
    if(isset( $_FILES['img']['name']) 
        && (  
        $_FILES['img']['type'] == "image/jpeg" 
        || $_FILES['img']['type'] == "image/jpg"
        )){   

            $temp = explode(".",$_FILES['img']['name']);
            $newfilename = ($new_name = ( rand(1,99999) + time() ) ). '.' .end($temp);
            mkdir($adr = '../news_img/'.$new_name);
            move_uploaded_file( $_FILES['img']['tmp_name'] , $adr.'/'.$newfilename );
            $connect = mysqli_connect('localhost' , 'root' , '' , 'project' );
            $sql = "
            insert into `news` values(
            NULL,
             '$_POST[title]' ,
             '$_POST[text]'  ,
             '$_POST[date]'  ,
             '$_POST[cat]'   ,
             '$_POST[sub]'   ,
             '".$adr.'/'.$newfilename."',
             '$_POST[pub]'   ,
             '$_POST[top_or_main]',
            '$_POST[src]'  )";
            mysqli_query($connect , $sql );
        }   
    }   
?>

如果您认为代码还可以,那么其他部分(如html或phpmyadmin)中可能出现的错误是什么?

您的代码有很多问题

最令人担忧的是SQL注入:

如果将两个整数相加,则最终可能会出现重复的目录。您希望将两个整数连接成一个字符串。我为你创建了一个函数

/**
 * Creates a new directory with a random name based on the time and a randomly
 * generated number.
 * 
 * @param string $baseDirectoryPath The base directory where the new directory will be created
 * @return string The path to the new directory
 */
function createRandomDirctory($baseDirectoryPath) {
    $randomDirectoryName = time() . '-' . rand(1, 99999);
    $directoryPath = $baseDirectoryPath . DIRECTORY_SEPERATOR . $randomDirectoryName;
    if (mkdir($directoryPath)) {
        return $directoryPath;
    }
    throw new Exception('Directory was not created: ' . $directoryPath);
}

$baseDirectoryPath = '../news_img';

if (!empty($_POST['title'])) {

    if (isset($_FILES['img']['name']) &&
            $_FILES['img']['type'] == "image/jpeg" ||
            $_FILES['img']['type'] == "image/jpg") {

        $temp = explode(".", $_FILES['img']['name']);
        $newFileName = end($temp);
        $randomDirectory = createRandomDirctory($baseDirectoryPath);
        $imageDestination = $randomDirectory . DIRECTORY_SEPARATOR . $newFileName;

        move_uploaded_file($_FILES['img']['tmp_name'], $imageDestination);

        $connect = mysqli_connect('localhost', 'root', '', 'project');

        // always escape user input that goes into an SQL statement.
        foreach($_POST as $key => $value) {
            $_POSY[$key] = mysql_real_escape_string($value);
        }

        $sql = " insert into 
                          `news`
                        values(  NULL            ,
                                 '$_POST[title]' ,
                                 '$_POST[text]'  ,
                                 '$_POST[date]'  ,
                                 '$_POST[cat]'   ,
                                 '$_POST[sub]'   ,
                                 '$imageDestination',
                                 '$_POST[pub]'   ,
                                 '$_POST[top_or_main]',
                                '$_POST[src]'  )";

        mysqli_query($connect, $sql);
    }
}   

大家好,欢迎来到StackOverflow。您需要向我们显示您收到的错误消息,以及导致错误的代码部分的任何指示。复制粘贴代码时的“为我修复”态度在这里。然而,这是大多数新用户常见的误解。因此,你应该稍微编辑一下你的帖子,包括你收到的错误的任何相关信息,并以一种更合理的方式表达你的其他问题