2015-12-03 4 views
-1

Я загружаю правильный файл в формате jpg, но он отображается. Извините, только JPG, JPEG, PNG & Файлы GIF разрешены. Я не ошибаюсь, я хочу, чтобы проверка файлов и тип файла после перемещения изображение на папку и имя файла Ставитьfileupload не работает в php

<?php 
$target_dir = "original-photo/"; 
$original_file=md5($_FILES["file"]["name"].time().rand(10,1000)).'.'.$imageFileType; 
$target_file = $target_dir .$original_file; 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["file"]["tmp_name"]); 
    if($check !== false) { 
    echo "File is an image - " . $check["mime"] . "."; 
    $uploadOk = 1; 
} else { 
    echo "File is not an image."; 
    $uploadOk = 0; 
} 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
echo "Sorry, file already exists."; 
$uploadOk = 0; 
} 
// Check file size 
if ($_FILES["file"]["size"] > 5000000) { 
// echo "Sorry, your file is too large."; 
header("Location:profilepic.php?filesize=error"); 
$uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" &&  $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
header("Location:profilepic.php?filetype=error"); 
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
    // if everything is ok, try to upload file 
    } else { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))  { 
    echo "The file ". basename($_FILES["file"]["name"]). " has been uploaded."; 
    } else { 
    echo "Sorry, there was an error uploading your file."; 
    } 
    } 
    ?> 
+0

Вы указываете pathinfo для поиска нового файла, который хотите переместить после проверки, но поскольку проверка не выполняется, файл не существует в новом местоположении. Вам нужно использовать временный путь для '$ _FILES' global. –

ответ

0

Заменить ....

//Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType!="jpeg" && $imageFileType != "gif") { 
header("Location:profilepic.php?filetype=error"); 
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
$uploadOk = 0; 
} 

С этим ....

$allowTheseImageTypes=array("image/gif", "image/pjpeg", "image/jpeg", "image/png", "image/x-png"); 
//test the image type here 
if (!in_array($_FILES['file']['type'], $allowTheseImageTypes)){ 
    header("Location:profilepic.php?filetype=error"); 
    //echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
0

Попробуйте это:

// Allow certain file formats 
$arrayTypeAllowed = array("image/jpg", "image/jpeg", "image/pjpeg", "image/png"); 
$imageFileType = $_FILES["file"]["type"] 

изменить это

if($imageFileType != "jpg" && $imageFileType != "png" &&  $imageFileType != "jpeg" 
&& $imageFileType != "gif") 

с этим

if (!in_array($imageFileType,arrayTypeAllowed)) { 
    //echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0 
} 
0

вы делаете md5 для всего файла. попробуйте ниже код может быть полезным

<?php 
$target_dir = "original-photo/"; 

$image_path = $target_dir . basename($_FILES["file"]["name"]); 
$target_file = $image_path['filename'].'_'.time().'.'.$image_path['extension'] 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["file"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["file"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["file"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 
0

Если вы хотите только расширение файла, используйте

вместо

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

это:

$extension = trim(substr(strrchr($filename, "."), 1));

Это много раза быстрее, чем используя pathinfo() и получая значение из массива.

+0

Так что я мог бы просто загрузить растровые изображения, просто изменив расширение на .jpg? Большой! –