• 26-04-2022, 12:34:18
    #1
    Merhabalar,

    jquery sistemi ile çalışan içerik yönetim sisteminde resimleri upload sistemimiz sorunsuz çalışıyor. Lakin upload ederken gerek dosya uzantısı, gerekse boyutu ve çözünürlük kontrolü yapmıyor. Örnek kodlar konu altında iletiyorum, jquery ve PHP'den anlayan kişiler dosyaları görmesi için sunucuya uzaktan bağlanıp gösterebilirim. Anlaştığımız kişi uzak masaüstü ile dosyalara erişim sağlayıp gerekli düzenlemeyi (ÜCRETLİ) yapmasını talep ediyorum.

    İletişim için Whatsapp'dan dönüş yapılırsa sevinirim. Whatsapp mesajı göndermek için tıklayın.

    Şuan çalışan upload dosyası (PHP)

    <?php
    header('Cache-Control: no-cache, must-revalidate');
    
    // URL yolu
    $path = '../uploads/';
    $urlpath = '/uploads/';
    
    // Özel değeri
    $customvalue = $_REQUEST['hidcustomval'];
    
    
    // Klasör kontrolü
    $pagefolder = $path;
    if (!file_exists($pagefolder)) {
        mkdir($pagefolder, 0777);
    }
    
    
    // İsteğe bağlı: Yükleme klasörünü belirtmek için özel değer kullanılıyorsa
    if ($customvalue!='') {
      $pagefolder = $path . $customvalue. '/';
      if (!file_exists($pagefolder)) {
          mkdir($pagefolder, 0777);
      }
      $urlpath = $urlpath . $customvalue. '/';
    }
    
    
    $filename = basename($_FILES["fileCover"]["name"]);
    
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
    // Resim dosyasının gerçek bir resim mi yoksa sahte bir resim mi olduğunu kontrol edin
    $check = getimagesize($_FILES["fileCover"]["tmp_name"]);
    if($check !== false) {
        //echo "Dosya bir resimdir - " . $check["mime"];
        $uploadOk = 1;
    } else {
        echo "<html><body onload=\"alert('Dosya bir resim değil.')\"></body></html>";
        exit();
        $uploadOk = 0;
    }
    
    // Belirli dosya formatlarına izin ver
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "webp" ) {
        echo "<html><body onload=\"alert('Sadece JPG, JPEG, PNG & GIF uzantıları yüklenebiliyor.')\"></body></html>";
        exit();
        $uploadOk = 0;
    }
    
    if ($uploadOk == 0) {
        echo "<html><body onload=\"alert('Üzgünüz, dosyanız yüklenmedi.')\"></body></html>";
        exit();
    } else {
    
        $random = base_convert(rand(),10,36) . date("his");
        $pic_type = strtolower(strrchr($_FILES["fileCover"]['name'],"."));
        $pic_name = "original$random$pic_type";
    
        if($pic_type == ".webp") {
    
            //Save as is
            move_uploaded_file($_FILES["fileCover"]['tmp_name'], $pagefolder . "$random$pic_type");
            
        } else {
    
            move_uploaded_file($_FILES["fileCover"]['tmp_name'], $pagefolder . $pic_name);
    
            //Save image
            if (true !== ($pic_error = @image_resize($pagefolder . $pic_name, $pagefolder . "$random$pic_type", 1600, 1600))) { // Güvenli boyut için resmi maksimum 1600x1600 olarak yeniden boyutlandırın
                echo "<html><body onload=\"alert('".$pic_error."')\"></body></html>";        
                exit();
            }
    
            unlink($pagefolder . $pic_name); // orijinali sil
        }
      
      
        // yeni kaydedilen dosyayla değiştirin
        echo "<html><body onload=\"parent.applyBoxImage('" . $urlpath . "$random$pic_type')\"></body></html>";
    }
    
    
    function image_resize($src, $dst, $width, $height, $crop=0){
    
      //if(!list($w, $h) = getimagesize($src)) return "Desteklenmeyen resim türü!";
      list($w, $h) = getimagesize($src);
    
      $type = strtolower(substr(strrchr($src,"."),1));
      if($type == 'jpeg') $type = 'jpg';
      switch($type){
        case 'bmp': $img = imagecreatefromwbmp($src); break;
        case 'gif': $img = imagecreatefromgif($src); break;
        case 'jpg': $img = imagecreatefromjpeg($src); break;
        case 'png': $img = imagecreatefrompng($src); break;
        default : return "Unsupported picture type!";
      }
      if($w < $width or $h < $height) {
        $width = 1629;
        $height = 850;
      }
      if($w < $width or $h < $height) {
        $width = 1533;
        $height = 800;
      }
      if($w < $width or $h < $height) {
        $width = 1438;
        $height = 750;
      }
      if($w < $width or $h < $height) {
        $width = 1380;
        $height = 720;
      }
      if($w < $width or $h < $height) {
        $width = 1342;
        $height = 700;
      }
      if($w < $width or $h < $height) {
        $width = 1246;
        $height = 650;
      }
      if($w < $width or $h < $height) {
        $width = 1150;
        $height = 600;
      }
      if($w < $width or $h < $height) {
        $width = 1054;
        $height = 550;
      }
      if($w < $width or $h < $height) {
        $width = 958;
        $height = 500;
      }
      if($w < $width or $h < $height) {
        $width = 863;
        $height = 450;
      }
      if($w < $width or $h < $height) {
        $width = 767;
        $height = 400;
      }
      if($w < $width or $h < $height) {
        $width = 671;
        $height = 350;
      }
      if($w < $width or $h < $height) {
        $width = 575;
        $height = 300;
      }
      if($w < $width or $h < $height) {
        return "Resim çok küçük. Minimum boyut: 575 x 350 piksel.";
      }
      
      // resize
      if($crop){
        $ratio = max($width/$w, $height/$h);
        $h = $height / $ratio;
        $x = ($w - $width / $ratio) / 2;
        $w = $width / $ratio;
      }
      else{
        $ratio = min($width/$w, $height/$h);
        $width = $w * $ratio;
        $height = $h * $ratio;
        $x = 0;
      }
    
      $new = imagecreatetruecolor($width, $height);
    
      // preserve transparency
      if($type == "gif" or $type == "png"){
        imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
        imagealphablending($new, false);
        imagesavealpha($new, true);
      }
    
      imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
    
      switch($type){
        case 'bmp': imagewbmp($new, $dst); break;
        case 'gif': imagegif($new, $dst); break;
        case 'jpg': imagejpeg($new, $dst); break;
        case 'png': imagepng($new, $dst); break;
      }
      return true;
    }
    ?>
  • 26-04-2022, 13:02:02
    #2
    İçeride bir kaç hata mevcut, exit() yaptıktan sonra değişken tanımlamışsınız.

    $dosya_buyutu = $_FILES["dosya"]["size"];
    $dosya_uzanti = end(explode(".",$dosya_adi)) ;
    
    if ($dosya_boyutu > 5*1024*1024) {
     echo "Dosya boyutu 5MB'yi aştı!";
     exit();
    }
    if ($dosya_uzanti != "JPG" && $dosya_uzanti != "PNG") {
     echo "Bu uzantıya izin verilmemektedir.";
     exit();
    }
  • 26-04-2022, 13:12:15
    #3
    uzantı kontrolü yapıyor hocam.

    Çözünürlülük ve boyut kontrolü için aşağıdaki kodu 43. satıra eklerseniz olacaktır. En üstteki değerleri kendinize göre ayarlayın.

            $max_width = 1080;
            $max_height = 1080;
            $max_file_size = 10485760; // byte cinsinden 10MB
    
            if ($check[0] > $max_width or $check[1] > $max_height) {
                echo "<html><body onload=\"alert('Maximum '.$max_width.'x'.$max_height.' çözünürlülüğünde olabilir.')\"></body></html>";
                exit();
            }
    
            if ($_FILES['fileCover']['size'] > $max_file_size) {
                echo "<html><body onload=\"alert('Maximum '.intval($max_file_size / 1048576).'MB boyutunda olabilir.')\"></body></html>";
                exit();
            }