Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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 当类型为.jpeg时,将图像上载到服务器不起作用_Php_Mysqli - Fatal编程技术网

Php 当类型为.jpeg时,将图像上载到服务器不起作用

Php 当类型为.jpeg时,将图像上载到服务器不起作用,php,mysqli,Php,Mysqli,我正在使用这个脚本将.jpg和.png类型的图像上传到服务器和数据库,但当涉及到.jpeg或.jpg时,它不起作用,而是将文件放在正确的目录中,并具有正确的扩展名,它只执行以下操作/galleri/uploads/a4b7c7fb0de9c56110c2279f24ec820jpeg.php,它会自动在最后添加.php 我一直在尝试添加这些行 if ( $type == 'image/jpeg' ) { $filetype = '.jpeg'; } else { $filetype = str

我正在使用这个脚本将.jpg和.png类型的图像上传到服务器和数据库,但当涉及到.jpeg或.jpg时,它不起作用,而是将文件放在正确的目录中,并具有正确的扩展名,它只执行以下操作/galleri/uploads/a4b7c7fb0de9c56110c2279f24ec820jpeg.php,它会自动在最后添加.php

我一直在尝试添加这些行

if ( $type == 'image/jpeg' ) { $filetype = '.jpeg'; } else {  $filetype = str_replace( 'image/', '', $type ); }

if ( $type == 'image/jpeg' ) { $filetype = '.JPG'; } else {  $filetype = str_replace( 'image/', '', $type ); }
但是没有用

除此之外,还有没有更好的裁剪工具,我可以用在这种情况下,是不是这么先进

这是完整的脚本:

if(isset($_POST['addmedia'])) {
    $mediatype =    escape(striptags($_POST['mediatype']));
    $title =        escape(striptags($_POST['title']));
    $video =        escape(striptags($_POST['medialink']));
    $date =         date('Y-m-d');

    if ($mediatype === 'img') {
        if( !isset( $_POST['p'] ) ) { $_POST['p']= 0; }

        if( $_POST['p'] == 1 ) {
            $name =     $_FILES['image']['name'];
            $temp =     $_FILES['image']['tmp_name'];
            $type =     $_FILES['image']['type'];
            $size =     $_FILES['image']['size'];

            if ( $type == 'image/jpeg' ) { $filetype = '.jpg'; } else {  $filetype = str_replace( 'image/', '', $type ); }
            if ( $type == 'image/png' ) { $filetype = '.png'; } else {  $filetype = str_replace( 'image/', '', $type ); }

            $path =             md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . $filetype;
            $thumb_path =       md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . $filetype;

            $size2  =   getimagesize ($temp);
            $width  =   $size2[0];
            $height =   $size2[1];

            $maxwidth   =   1281;
            $maxheight  =   751;
            $allowed    =   array('image/jpeg', 'image/png');

            if( in_array( $type, $allowed ) ) {
                if( $width < $maxwidth && $height < $maxheight) {
                    if( $size < 10485760) {
                        if( $width == $height ) { $case = 1;}   // Square form
                        if( $width > $height ) { $case = 2;}    // Lying form
                        if( $width < $height ) { $case = 3;}    // Standing form

                        switch($case) {
                            case 1:
                            $newwidth   =    280;
                            $newheight  =    150;
                            break;

                            case 2:
                            $newheight  =   150;
                            $ratio      =   $newheight / $height;
                            $newwidth   =   round($width * $ratio);
                            break;

                            case 3:
                            $newwidth   =   280;
                            $ratio      =   $newwidth / $width;
                            $newheight  =   round($height * $ratio);
                            break;
                        }

                        switch($type) {
                            case 'image/jpeg':
                            $img    =   imagecreatefromjpeg( $temp );
                            $thumb  =   imagecreatetruecolor( $newwidth, $newheight );
                            imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
                            imagejpeg( $thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
                            break;

                            case 'image/png':
                            $img    =    imagecreatefrompng( $temp );
                            $thumb  =    imagecreatetruecolor( $newwidth, $newheight );
                            imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
                            imagepng( $thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
                            break;
                        }

                        move_uploaded_file( $temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/" . $path );
                        $addimg = "INSERT INTO uploads (`type`, `title`, `src`, `thumb`, `date`) VALUES ('$mediatype', '$title', '$path', '$thumb_path', '$date')";
                        if ($add_img = $db_connect->query($addimg)) {}
                        echo 'Din bild har laddats upp!';
                        header("Location: " . $_SERVER['HTTP_REFERER']);

                    } else {
                        echo '10MB';
                    }
                } else {
                    echo 'To big in size';
                }
            } else {
                echo '.jpg, .jpeg, .png!';
            }
        }
    } else if ($mediatype === 'vid') {
        $name       =     $_FILES['image']['name'];
        $temp       =     $_FILES['image']['tmp_name'];
        $size       =     $_FILES['image']['size'];
        $thumb_path =     md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . '.jpg';

        move_uploaded_file( $temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
        $addvid = "INSERT INTO uploads (`type`, `title`, `thumb`, `videolink`, `date`) VALUES ('$mediatype', '$title', '$thumb_path', '$video', '$date')";

        if ($add_vid = $db_connect->query($addvid)) {}
        echo 'Video uploaded';
        header("Location: " . $_SERVER['HTTP_REFERER']);
    }
}
if(isset($\u POST['addmedia'])){
$mediatype=escape(striptags($_POST['mediatype']);
$title=escape(带标签($_POST['title']);
$video=escape(带标签($_POST['medialink']);
$date=日期('Y-m-d');
如果($mediatype=='img'){
如果(!isset($_POST['p']){$_POST['p']=0;}
如果($_POST['p']==1){
$name=$_文件['image']['name'];
$temp=$\u文件['image']['tmp\u name'];
$type=$\u文件['image']['type'];
$size=$\u文件['image']['size'];
如果($type='image/jpeg'){$filetype='.jpg';}否则{$filetype=str_replace('image/','','$type);}
如果($type='image/png'){$filetype='.png';}否则{$filetype=str_replace('image/','','$type);}
$path=md5(rand(011000).rand(011000).rand(011000).rand(011000).rand(011000))。$filetype;
$thumb_path=md5(rand(0,1000).rand(0,1000).rand(0,1000).rand(0,1000))。$filetype;
$size2=getimagesize($temp);
$width=$size2[0];
$height=$size2[1];
$maxwidth=1281;
$maxheight=751;
$allowed=数组('image/jpeg','image/png');
if(在数组中($type,$allowed)){
如果($width<$maxwidth&&$height<$maxheight){
如果($10485760){
如果($width==$height){$case=1;}//方形
如果($width>$height){$case=2;}//
如果($width<$height){$case=3;}//站姿
交换机($case){
案例1:
$newwidth=280;
$newheight=150;
打破
案例2:
$newheight=150;
$ratio=$newheight/$height;
$newwidth=圆形($width*$ratio);
打破
案例3:
$newwidth=280;
$ratio=$newwidth/$width;
$newheight=圆形($height*$ratio);
打破
}
交换机($类型){
案例“图像/jpeg”:
$img=imagecreatefromjpeg($temp);
$thumb=imagecreatetruecolor($newwidth,$newheight);
imagecopyresized($thumb,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($thumb,$\u服务器['DOCUMENT\u ROOT'].“/galleri/uploads/thumbs/”$thumb\u路径);
打破
案例“image/png”:
$img=imagecreatefrompng($temp);
$thumb=imagecreatetruecolor($newwidth,$newheight);
imagecopyresized($thumb,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
imagepng($thumb,$\u服务器['DOCUMENT\u ROOT'].“/galleri/uploads/thumbs/”$thumb\u路径);
打破
}
移动上传的文件($temp,$$u服务器['DOCUMENT\u ROOT'].“/galleri/uploads/”$path);
$addimg=“插入上载(`type`、`title`、`src`、`thumb`、`date`)值(`mediatype`、`title`、`path`、`thumb_path`、`date');
如果($add\u img=$db\u connect->query($addimg)){}
回音“这是我的荣幸!”;
标题(“位置:“.$”服务器['HTTP\u REFERER']);
}否则{
回波“10MB”;
}
}否则{
回声“到大尺寸”;
}
}否则{
echo'.jpg、.jpeg、.png!';
}
}
}else if($mediatype==='vid'){
$name=$_文件['image']['name'];
$temp=$\u文件['image']['tmp\u name'];
$size=$\u文件['image']['size'];
$thumb_path=md5(rand(011000).rand(011000).rand(011000).rand(011000).rand(011000)).jpg';
移动上传的文件($temp,$\u服务器['DOCUMENT\u ROOT'].“/galleri/uploads/thumbs/”$thumb\u路径);
$addvid=“插入上载(`type`、`title`、`thumb`、`videolink`、`date`)值(`mediatype`、`title`、`thumb_path`、`$video`、`date');
如果($add_vid=$db_connect->query($addvid)){}
echo“上传视频”;
标题(“位置:“.$”服务器['HTTP\u REFERER']);
}
}
试试这个:

<?php

    ...

    $name = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $size = $_FILES['image']['size'];

    $type = image_type_to_mime_type(exif_imagetype($temp)); // get the real image mime type

    if ( $type == 'image/jpeg' ) { // jpeg

        $filetype = '.jpg'; 

    } else if ( $type == 'image/png' ){ // png

        $filetype = '.png';

    } else { // other image type

        $filetype = '.' . str_replace( 'image/', '', $type ); // to get .gif for a gif image, for example

    }

    ...

?>


你能做一个
echo$类型吗
对于未被接受的图像,请告诉我们结果?@akmozo当我删除.png格式时,我注意到一件事.jpg和.jpg工作正常。。这很奇怪这是因为你的条件不对。我给出了一个答案,看一看。谢谢,它工作得很好,但是当我上传ex.a.gif时,它会上传为.jpg,相反,应该会有一条消息说这种类型不是uploadableDo-yo