上传的图片不保存在目标文件夹中

Uploaded images don't save in destination folder

我是新来的,很高兴认识你。

我正在尝试为用户在安全检查后上传的图片创建一个干净的副本,并将此副本保存在指定的文件夹和我的数据库中。我在 Windows 10 上与 XAMPP 合作,我授予了对上述文件夹的正确权限。我的代码如下:

/*bit of code for picture addition, in a function for album publication*/
if($_FILES['albumCover']['error'] === 0) {
    $covType;
    $createCover;
    $covName = guidv4();

    $imgRegex = '/^[a-z\d][^.]*\.(jpe?g$)|\.(png$)/i';

    $covInfo = finfo_open(FILEINFO_MIME_TYPE);
    $covMime = finfo_file($covInfo, $_FILES['albumCover']['tmp_name']);
    finfo_close($covInfo);

    if(!preg_match($imgRegex, $_FILES['albumCover']['name']) ||
       str_contains($_FILES['albumCover']['name'], ' ') ||
       in_array($_FILES['albumCover']['type'], ['image/jpeg','image/png'])||
       strpos($covMime, 'image/') !== 0) 
    {

        $addMessages['errors'][] = 'Caractère autorisé pour le nom d\'image : lettres sans 
              accent / sans trémas / sans cédille, chiffres, tirets et underscores. Fichiers
              autorisés : images .jpg, .jpeg ou .png.';
    }

    if(empty($addMessages['errors'])) {

        if(preg_match($imgRegex, $_FILES['albumCover']['name'])
             && $_FILES['albumCover']['type'] == 'image/jpeg'
             && strpos($covMime, 'image/') !== 0) 
        {

            $covType = 'jpeg';
            $createCover = imagecreatefromjpeg($_FILES['albumCover']['tmp_name']);

            imagejpeg($createCover,
                self::COV_SECURE_PATH.$covName.'.'.pathinfo($_FILES['albumCover']['name'], 
                      PATHINFO_EXTENSION)
            );

            move_uploaded_file($_FILES['albumCover']['tmp_name'],
            self::COV_SECURE_PATH.'/'.$_FILES['albumCover']['name']);
        } elseif(preg_match($imgRegex, $_FILES['albumCover']['name'])
                && $_FILES['albumCover']['type'] == 'image/png'
                && strpos($covMime, 'image/') !== 0) 
        {

            $covType = 'png';
            $createCover = imagecreatefrompng($_FILES['albumCover']['tmp_name']);

            imagepng($createCover,
                    self::COV_SECURE_PATH.$covName.'.'.pathinfo($_FILES['albumCover']['name'],
                   PATHINFO_EXTENSION));

            move_uploaded_file($_FILES['albumCover']['tmp_name'],
            self::COV_SECURE_PATH.'/'.$_FILES['albumCover']['name']);
        }
    }
}

更多细节:上面的代码是 class 方法的一部分。常量 COV_SECURE_PATH 在方法之前的 class 中声明。图片名称的 guidv4() 函数位于同一文件中,在 class.

声明之前

我没有 PHP 错误消息,我的打印件显示了每个文件的正确名称和扩展名,并且我在我的 HTML 表单中声明了属性 enctype="multipart/form-data"。图片保存在我的数据库中(如果需要,我会添加有关数据库的代码),但它们不会保存在我的文件夹中,我不知道我遗漏了什么。到目前为止,我在任何网站上搜索时都没有找到答案,我们将不胜感激。

看看我的图片验证...

//connecting to the database
$mysqli = mysqli_connect( 'localhost', 'root', '', 'img_validation' )
or
die( 'Database connection error => '.mysqli_connect_error() );

//errors and success message handling
$errors = array();
$success = array();

//imgs dir
$imgs_dir = './assets/imgs/';

//if upload button is pressed...
if ( isset( $_POST['upload'] ) ) {

    //get image info...
    $imgName = $_FILES['image']['name'];
    $imgTmp = $_FILES['image']['tmp_name'];
    $imgSize = $_FILES['image']['size'];

    //image validation
    if ( $imgName ) {
        //get extension info
        $imgExt = strtolower( pathinfo( $imgName, PATHINFO_EXTENSION ) );
        //allowed image extensions
        $allowExt  = array( 'jpg', 'jpeg', 'png' );
        //create new random image name
        $uploadedPic = 'user-upload-'.rand( 100000, 999999 ).'.'.$imgExt;

        //validate image extension
        if ( in_array( $imgExt, $allowExt ) ) {
            //max image upload size 3MB
            if ( $imgSize < 3000000 ) {
                //move image file to designated directory/folder
                move_uploaded_file( $imgTmp, $imgs_dir.$uploadedPic );
            } else {
                array_push( $errors, "Image too large. Max. upload size 3MB." );
            }
        } else {
            array_push( $errors, "<script type='text/javascript'>
            alert('JPG, JPEG and PNG formats required.\nMax. upload size 3MB.');</script>" );
            array_push( $errors, "Please select a valid image." );
        }
    } else {
        $uploadedPic;
    }

    //if found no errors...
    if ( count( $errors ) == 0 ) {
        //insert new record in `user_uploads` table in the database...
        $admin = mysqli_query( $mysqli, "INSERT INTO user_uploads (image) VALUES ('$uploadedPic');" );
        if ( $admin == 1 ) {
            //success message
            array_push( $success, "Image uploaded successfully." );
            //redirect to profile after 2 seconds...
            header( 'refresh: 2; url=http://localhost/img-validation/' );
        } else {
            array_push( $errors, mysqli_error() );
        }
    }
}

当我在 PHP 的早期学习阶段时,我编写了这段代码,希望这对你有用:)

好吧,我终于解决了所有问题。我的错误是:if(preg_match($imgRegex, $_FILES['albumCover']['name']) && $_FILES['albumCover']['type'] == 'image/jpeg' && strpos($covMime, 'image/') !== 0) {

而不是if(preg_match($imgRegex, $_FILES['albumCover']['name']) && $_FILES['albumCover']['type'] == 'image/jpeg' && strpos($covMime, 'image/') === 0) {

而且我也意识到我没有在我的 php.ini 中激活 GD,现在已经完成了。 感谢所有回答我的人!