what's wrong with this codes?

Discussion in 'PHP' started by jonhyhar, Feb 24, 2010.

  1. #1
    hi guyz, why doesn't this codes work? it's an image upload ( and resize ) php page but it's doesnt upload :eek:

    here it is,

    <?php 
    @ob_start();
    $randomsayi = rand(1,11515454745484410); 
    $file = $_FILES['file2'];
    
    $useradi = $_POST['username'];
    $usergadi = $_POST['gercekadi'];
    
    $categories = $_POST['categories'];
    $file_n = $_FILES['file2']['name'];
    $file_name = "$file_n-$randomsayi.jpg";
    $file_size = $_FILES['file2']['size'];
    $file_temp = $_FILES['file2']['tmp_name'];
    $bad_files = "0";
    $max_size = "10048576"; // Maximum File Size (In Bytes) 
    $extensions = ".gif$|.png$|.jpg$"; // Allowed File Extensions 
    if ($file) { 
    if ($file_size > $bad_files) {
    if ($file_size <= $max_size) { 
    if (ereg($extensions , $file_name)) { 
    if (move_uploaded_file($file_temp, "files_uploaded/$myusername/$file_name")) { 
    
    $maxWidth = 320;
    $maxHeight = 240;
    
    
       $file = "files_uploaded/$myusername/$file_name";
       if(is_readable($file))
       {
          $sizeInfo = getimagesize($file);
          if($sizeInfo !== FALSE)
          {
             $imgTypes = array(            // IMAGETYPE_* for getimagesize() index 2
                IMAGETYPE_GIF  => IMG_GIF, // IMG_* for imagetype() check
                IMAGETYPE_JPEG => IMG_JPG,
                IMAGETYPE_PNG  => IMG_PNG,
                IMAGETYPE_WBMP => IMG_WBMP
             );
             if(isset($imgTypes[$sizeInfo[2]]) and
                   (imagetypes() & $imgTypes[$sizeInfo[2]]))
             {
                $ratio = (($sizeInfo[0]/$sizeInfo[1]) > ($maxWidth/$maxHeight)) ?
                         $maxWidth / $sizeInfo[0] :
                         $maxHeight / $sizeInfo[1];
                $newWidth = $sizeInfo[0] * $ratio;
                $newHeight = $sizeInfo[1] * $ratio;
                $thumb = imagecreatetruecolor($newWidth, $newHeight);
                switch($sizeInfo[2])  // image type
                {
                   case IMAGETYPE_GIF:
                      $source = imagecreatefromgif($file);
                      break;
                   case IMAGETYPE_JPEG:
                      $source = imagecreatefromjpeg($file);
                      break;
                   case IMAGETYPE_PNG:
                      $source = imagecreatefrompng($file);
                      break;
                   case IMAGETYPE_WBMP:
                      $source = imagecreatefromwbmp($file);
                      break;
                   default:
                      header("HTTP/1.0 404 Not Found");
                      exit;
                }
                imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $sizeInfo[0], $sizeInfo[1]);
                @header("Content-type: image/jpeg");
    		$afilename = explode('.', $file);
    		$uzan = $afilename[count($afilename) - 1];
    	$ad   = substr(md5(rand(0,999999999)),-10);
    	$yenisi = "".$ad.".".$uzan."";
    	//veritabanina kayit edilecek yeni boyutlandirdigimiz fotonun yolu
    	$file_name2 = "files_uploaded/$myusername/".$yenisi;
    	imagejpeg($thumb,$file_name,100);
    
    	//ilk upload ettigimiz yani boyutlandirmadan önceki fotoyu siliyoruz
    	unlink("$file");
    
             }
          }
       }
    
    
    $sql = "insert into $categories (foto1,foto1oy,categories,tanim,isim,uname,isimi) values ('http://www.picciz.com/$file_name2','0','$categories','$tanim','$isim','$useradi','$usergadi')";
    $kayit = mysql_query($sql);
    echo "<td valign=top width=350><center><img class='resssim' src='/files_uploaded/$myusername/$file_name' alt='' name='sa' id='sa' /><br><br></center></td>";
    }else{ 
    print "<font color=\"red\" font size=\"3\">System error!</font></P>\n"; 
    } 
    }else{ 
    print "<font color=\"red\" font size=\"3\">unknown extension </font><p>\n";
    print "File name: $file_name<p>\n";  
    } 
    }else{ 
    print "<font color=\"red\" font size=\"3\">the file is too large</font><p>\n"; 
    }
    }else{
    print "<font color=\"red\" font size=\"3\">File Must Be Greater Than 0 Byte's</font><p>\n";
    }
    
    } 
    @ob_end_flush();
    ?>
    Code (markup):
     
    jonhyhar, Feb 24, 2010 IP
  2. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #2
    What happens when you try and upload a file? Does it say anything? Post the HTML of the upload form.
     
    CoreyPeerFly, Feb 24, 2010 IP
  3. jonhyhar

    jonhyhar Active Member

    Messages:
    166
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    thanks for your reply but i just fixed it, maybe some1 needs it, the problem was

    This code
    
    	//veritabanina kayit edilecek yeni boyutlandirdigimiz fotonun yolu
    	$file_name2 = "files_uploaded/$myusername/".$yenisi;
    	imagejpeg($thumb,$file_name,100);
    
    Code (markup):
    must be like this
    
    	//veritabanina kayit edilecek yeni boyutlandirdigimiz fotonun yolu
    	$file_name2 = "files_uploaded/$myusername/".$yenisi;
    	imagejpeg($thumb,$file_name2,100);
    Code (markup):
    it was only "$file_name" must be "$file_name2", it took my hours! :( :D
     
    jonhyhar, Feb 24, 2010 IP
  4. suresh981

    suresh981 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this it might work.....:)
     
    suresh981, Feb 25, 2010 IP