我使用此脚本上传.jpg和&amp ;; .png类型到服务器和数据库但是当涉及到.jpeg或.JPG时它不起作用,而不是将文件放在正确的目录中并且具有正确的扩展名,它只是自动添加/galleri/uploads/a4b7c7fb0de9c561110c2279f24ec820jpeg.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']);
}
}
答案 0 :(得分:2)
试试这个:
<?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
}
...
?>