簡體   English   中英

文件上傳不檢查現有圖像

[英]file upload doesn't check for existing image

我從整潔的設計中獲得了以下代碼,並且除了不檢查現有圖像外,它工作正常。 當我再次上傳同一張圖片時,它接受,沒有任何錯誤。 我發現腳本一旦上載到新的隨機名稱后便會重命名該文件,並且還試圖查看新名稱在保存圖像的文件夾中是否存在。 這意味着它將為每個上載的圖像賦予新的隨機名稱,並嘗試檢查新名稱是否存在。 如何檢查現有圖像? 我需要刪除重命名上傳圖像的功能嗎?

     <?php

function uploadFile ($file_field = null, $check_image = false, $random_name = false) {

//Config Section    
//Set file upload path
$path = 'productpic/'; //with trailing slash
//Set max file size in bytes
$max_size = 2097152;
//Set default file extension whitelist
$whitelist_ext = array('jpg','png','gif');
//Set default file type whitelist
$whitelist_type = array('image/jpeg', 'image/png','image/gif');

//The Validation
// Create an array to hold any output
$out = array('error'=>null);

if (!$file_field) {
$out['error'][] = "Please specify a valid form field name";           
}

if (!$path) {
$out['error'][] = "Please specify a valid upload path";               
}

if (count($out['error'])>0) {
return $out;
}

//Make sure that there is a file
if((!empty($_FILES[$file_field])) && ($_FILES[$file_field]['error'] == 0)) {

// Get filename
$file_info = pathinfo($_FILES[$file_field]['name']);
$name = $file_info['filename'];
$ext = $file_info['extension'];

//Check file has the right extension           
if (!in_array($ext, $whitelist_ext)) {
  $out['error'][] = "Invalid file Extension";
}

//Check that the file is of the right type
if (!in_array($_FILES[$file_field]["type"], $whitelist_type)) {
  $out['error'][] = "Invalid file Type";
}

//Check that the file is not too big
if ($_FILES[$file_field]["size"] > $max_size) {
  $out['error'][] = "We are sorry, the image must be less than 2MB";
}

//If $check image is set as true
if ($check_image) {
  if (!getimagesize($_FILES[$file_field]['tmp_name'])) {
    $out['error'][] = "The file you trying to upload is not an Image, we only accept images";
  }
}

//Create full filename including path
if ($random_name) {
  // Generate random filename
  $tmp = str_replace(array('.',' '), array('',''), microtime());

  if (!$tmp || $tmp == '') {
    $out['error'][] = "File must have a name";
  }     
  $newname = $tmp.'.'.$ext;                                
} else {
    $newname = $name.'.'.$ext;
}

//Check if file already exists on server
if (file_exists($path.$newname)) {
  $out['error'][] = "the image you trying to upload already exists, please upload only once";
}

if (count($out['error'])>0) {
  //The file has not correctly validated
  return $out;
} 

if (move_uploaded_file($_FILES[$file_field]['tmp_name'], $path.$newname)) {
  //Success
  $out['filepath'] = $path;
  $out['filename'] = $newname;
  return $out;
} else {
  $out['error'][] = "Server Error!";
}

} else {
$out['error'][] = "No image uploaded";
return $out;
}      
}
?>

<?php
if (isset($_POST['submit'])) {
$file = uploadFile('file', true, true);
if (is_array($file['error'])) {
$message = '';
foreach ($file['error'] as $msg) {
  $message .= '<p>'.$msg.'</p>';    
}
} else {
$message = "File uploaded successfully";
}
echo $message;
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="file" type="file" size="20" />
<input name="submit" type="submit" value="Upload" />
</form>

您不需要刪除任何代碼-只需更改調用函數的方式即可。

該函數定義為:

function uploadFile ($file_field = null, $check_image = false, $random_name = false)

您可以看到$random_name變量作為參數傳遞給函數。 如果設置為true ,則為上傳的圖像設置隨機文件名。

您正在這樣調用函數:

$file = uploadFile('file', true, true);

這樣您就可以看到您傳遞的第三個參數(即,與$random_name參數對應的參數為true

嘗試將其更改為

$file = uploadFile('file', true, false);

阻止函數強制文件具有隨機文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM