I am trying to upload an image to my server. I have a form that POSTS the filename. Then the page reloads and tries to upload that filename. I CHMOD' the directory of the files to read and write. I'm on a UNIX server. For some reason everytime I try to upload an image it won't work. here is the code:

if ($upload == "TRUE" || $upload == "EDIT") {
		$file = $_POST['file'];
		// If you havent already bought an avatar then
		if (isset($file) && trim($file) != "") {
			$clientpic = trim("graphics/avatars/".$_SESSION['name'].".jpg");
			if(file_exists("$clientpic")) unlink("$clientpic");
			if (copy($file, $clientpic)) {
				$tokens = $tokens - 5;
				$sql = "UPDATE members SET tokens = '".$tokens."' where username = '".$_SESSION['name']."'"; mysql_db_query($database, $sql, $Link);
				$sql = "UPDATE members SET avatar = '".$clientpic."' where username = '".$_SESSION['name']."'"; mysql_db_query($database, $sql, $Link);
				$showtext = "<br><br><b><center>You just purchased avatar privelages!</center></B><br>You can change your avatar at anytime by clicking on the avatar link on the left.<br>";
			} else {
				$showtext = "<br><br><b><center><font color='#FF0000'>Your image could not be uploaded!</font></center></B>";
				$UserPic = "";
			}
		}
	}

I haven't incorporated the resizing, but I want it to be able to scale the image down to fit in a 75x75 box.

    Hi,

    There are a number of small glitches in your code (e.g. your trimming of strings is not going right, the $_POST array doesn't contain the file etcetc. THerefore I post part of an upload script I wrote. Hope it helps you. You need to set most of the variables, such as newfilename and output sizes, since they get set in different parts of the file. But this way you also have an -in principle- working resize script.

    
       $original_picture = $file_array['name'];         // original filename
       $tmp_picture      = $file_array['tmp_name'];     // Filname in upload folder
       $new_picture      = $destination;                // full path filename
    
        // ****************** Test variables / files for correct input *******************************
    
        if (!file_exists($tmp_picture) )         // Check wether filesource exists
        {
        return("File doesn't exist in temporary folder on server!");
        // The script has died here
        }
    
        if(file_exists($new_picture))        // Remove existsing file
        {
        $delete = @unlink($new_picture);
        }
    
        // ***************** Get file details **************************
    
        $size = GetImageSize($tmp_picture);
        $aspectRat = (float)($size[1] / $size[0]);
    
        // ***************** Calculate new x/y size, maintaining original aspect ratio
    
        if( ($size[0] <= $new_max_width) && ($size[1] <= $new_max_width) )  // Image < max size
          {        
          copy($tmp_picture, $new_picture); 
          $newX = $size[0];
          $newY = $size[1];
          }
        else 
          {
    
          $width_ratio = $size[0] / $new_max_width;
          $height_ratio = $size[1] / $new_max_height;
    
        if($width_ratio >= $height_ratio)  // width needs to be reduced more
          {
          $newY = $new_max_width * $aspectRat;
          $newX = $new_max_width;
          }
        else
          {
          $newY = $new_max_height;
          $newX = $new_max_height * (1/$aspectRat);
          }
    
    
        //create the destination image resource. 
    
        $destImg = ImageCreateTrueColor($newX, $newY );
    
        //read the source image (original), use correct function depending on format
        // Vervangen voor een switch tzt.
    
        if ($size[2] == 1)
        {
        $sourceImg = ImageCreateFromGIF($tmp_picture);
        }
        if ($size[2] == 2)
        {
        $sourceImg = ImageCreateFromJPEG($tmp_picture);
        }
        else if ($size[2] == 3)
        {
        $sourceImg = ImageCreateFromPNG($tmp_picture);
        }
        else
        {
        return("This file cannot be used. It is not a jpg, gif or png file.<br>");
        die(); // This is a double-trap. Return should have ended execution fo the scriopt already 
        }
    
        // **************************** Create the new output file *******************
    
        imagecopyresampled($destImg, $sourceImg, 0, 0, 0, 0, $newX, $newY, $size[0],  $size[1]);
        imagejpeg($destImg, $destination);
    
        }
    
        // *************************** Remove the temproary file *********************
        if($remove_orig == 1)
          {
          $deletedfile = @unlink($tmp_picture);
          }
    
    
    
      Write a Reply...