Results 1 to 7 of 7

Thread: file uploading

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    file uploading

    Can anyone help with the following (details through post):

    Code:
    <form action="p_addphotos.php" method="POST" name="addphoto" ENCTYPE="multipart/form-data">
    
    <table border=0 cellpadding=0 cellspacing=0 width=100%>
    
    <tr>
    <td align=left valign=top><b>Thumbnail:</b></td>
    <td align=left valign=top>
    
    <INPUT TYPE="file" NAME="img1" SIZE="50"></p>
    <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="8000" />
    The size of the thumbnail must be exactly 100 by 100 pixels and must be in the format of *.jpg, *.jpeg or *.gif
    
    </td>
    </tr>
    
    <tr>
    <td align=left valign=top><b>Photo:</b></td>
    <td align=left valign=top>
    
    <INPUT TYPE="file" NAME="img2" SIZE="50"></p>
    The image must be in the format of *.jpg, *.jpeg or *.gif
    
    </td>
    </tr>
    and the code to upload the files

    Code:
    $uploaddir = 'photos/thumbnails/';
    	$uploadfile1 = $uploaddir . $_FILES['img1']['name'];
    
    	$filetypes = array("image/jpg","image/gif","image/jpeg","image/pjpeg");
    	$imageinfo = getimagesize($_FILES['img1']['tmp_name']);
    
    	// upload the file only if the file type is gif or jpg.
    	if(in_array(strtolower($_FILES['img1']['type']),$filetypes) && $imageinfo[0] = 100 && $imageinfo[1] = 100)
    	{
    
    	 // let us read all the files in the directory and rename the file, if exists.
    	 if (file_exists($uploadfile1))
    	 {
    		$path_parts = pathinfo($uploadfile1);
    		$uploadfile1 = $uploaddir.(substr($_FILES['img1']['name'],0,(strlen($_FILES['img1']['name'])-(strlen($path_parts['extension'])+1))).date("YmdHis").".".$path_parts['extension']);
    	 }
    
    	  if (move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile1))
    	  {
    		$FileSuccess1 = 1;
    	  }
    	  else
    	  {
    		$FileSuccess1 = 0;
    	  }
    	}
    
    	$uploaddir = 'photos/';
    	$uploadfile2 = $uploaddir . $_FILES['img2']['name'];
    
    	$filetypes = array("image/jpg","image/gif","image/jpeg","image/pjpeg");
    
    	// upload the file only if the file type is gif or jpg.
    	if(in_array(strtolower($_FILES['img2']['type']),$filetypes))
    	{
    	 // let us read all the files in the directory and rename the file, if exists.
    	 if (file_exists($uploadfile2))
    	 {
    		$path_parts = pathinfo($uploadfile2);
    		$uploadfile2 = $uploaddir.(substr($_FILES['img2']['name'],0,(strlen($_FILES['img2']['name'])-(strlen($path_parts['extension'])+1))).date("YmdHis").".".$path_parts['extension']);
    	 }
    
    	  if (move_uploaded_file($_FILES['img2']['tmp_name'], $uploadfile2))
    	  {
    		$FileSuccess2 = 1;
    	  }
    	  else
    	  {
    		$FileSuccess2 = 0;
    	  }
    	}
    img1 uploads fine but img2 doesn't go into the if statement:

    Code:
    	if(in_array(strtolower($_FILES['img2']['type']),$filetypes))
    no matter what image extension the file is.

    Can anyone see the reason why? Getting really frustrating
    Last edited by JamesNZ; Jan 6th, 2005 at 03:23 AM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: file uploading

    You need to debug: Firstly put this line at the top of your script.
    PHP Code:
     error_reporting(E_ALL);
     
     
    /* examine this varaible dump */
     
    echo('<pre>' print_r($_FILEStrue) . '</pre>'); 
    Do you get any notices or wornings? Is the content of the $_FILES array what you expect it to be?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: file uploading

    This is what I get:

    Code:
    Array
    (
        [img1] => Array
            (
                [name] => 01.jpg
                [type] => image/pjpeg
                [tmp_name] => /tmp/phpdpWBeo
                [error] => 0
                [size] => 2078
            )
    
        [img2] => Array
            (
                [name] => 01large.jpg
                [type] => 
                [tmp_name] => 
                [error] => 2
                [size] => 0
            )
    
    )
    
    
    Notice: Undefined variable: FileSuccess2 in p_addphotos.php on line 70
    Why hasn't 01large.jpg got a type, tmp_name or size? What's error 2?


    This is the full code and line 70 is:

    Code:
    If ($FileSuccess1 == 1 && $FileSuccess2 == 1) {
    Not sure why I've got Undefined variable: FileSuccess2. It is isn't it?

    Code:
    <?php include_once("startup.php") ?>
    <?php
    
    session_start();
    
    $UserIDAuto = $_SESSION['UserIDAuto'];
    
    if ($_SESSION['UserName'] =="" || $_SESSION['UserName'] == NULL) {
    	include 'notloggedin.php';
    }
    else {
    
    	error_reporting(E_ALL);
    
    /* examine this varaible dump */
    echo('<pre>' . print_r($_FILES, true) . '</pre>');
    
    	$uploaddir = 'photos/thumbnails/';
    	$uploadfile1 = $uploaddir . $_FILES['img1']['name'];
    
    	$filetypes = array("image/jpg","image/gif","image/jpeg","image/pjpeg");
    	$imageinfo = getimagesize($_FILES['img1']['tmp_name']);
    
    	// upload the file only if the file type is gif or jpg.
    	if(in_array(strtolower($_FILES['img1']['type']),$filetypes) && $imageinfo[0] = 100 && $imageinfo[1] = 100)
    	{
    
    	 // let us read all the files in the directory and rename the file, if exists.
    	 if (file_exists($uploadfile1))
    	 {
    		$path_parts = pathinfo($uploadfile1);
    		$uploadfile1 = $uploaddir.(substr($_FILES['img1']['name'],0,(strlen($_FILES['img1']['name'])-(strlen($path_parts['extension'])+1))).date("YmdHis").".".$path_parts['extension']);
    	 }
    
    	  if (move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile1))
    	  {
    		$FileSuccess1 = 1;
    	  }
    	  else
    	  {
    		$FileSuccess1 = 0;
    	  }
    	}
    
    	$uploaddir = 'photos/';
    	$uploadfile2 = $uploaddir . $_FILES['img2']['name'];
    
    	$filetypes = array("image/jpg","image/gif","image/jpeg","image/pjpeg");
    
    	// upload the file only if the file type is gif or jpg.
    	if(in_array(strtolower($_FILES['img2']['type']),$filetypes))
    	{
    	 // let us read all the files in the directory and rename the file, if exists.
    	 if (file_exists($uploadfile2))
    	 {
    		$path_parts = pathinfo($uploadfile2);
    		$uploadfile2 = $uploaddir.(substr($_FILES['img2']['name'],0,(strlen($_FILES['img2']['name'])-(strlen($path_parts['extension'])+1))).date("YmdHis").".".$path_parts['extension']);
    	 }
    
    	  if (move_uploaded_file($_FILES['img2']['tmp_name'], $uploadfile2))
    	  {
    		$FileSuccess2 = 1;
    	  }
    	  else
    	  {
    		$FileSuccess2 = 0;
    	  }
    	}
    
    If ($FileSuccess1 == 1 && $FileSuccess2 == 1) {
    
    	$PhotoCategoryIDAuto = $_POST["PhotoCategoryIDAuto"];
    	$PhotoDescription = $_POST["PhotoDescription"];
    	$Thumbnail = $uploadfile1;
    	$Photo = $uploadfile2;
    	$DateCreated = date("Y-m-d");
    	$UserCreated = $UserIDAuto;
    	$DateModified = date("Y-m-d");
    	$UserModified = $UserIDAuto;
    
    	$sql = "Insert Into Photo (PhotoCategoryIDAuto, PhotoDescription, Thumbnail, Photo, DateCreated, DateModified, UserCreated, UserModified) Values ($PhotoCategoryIDAuto, '$PhotoDescription', '$Thumbnail', '$Photo', '$DateCreated', '$DateModified', '$UserCreated', '$UserModified')";
    
    	mysql_query($sql,$db);
    
    	$UserAction = "Added Photo: ".$PhotoDescription;
    	$RemoteAddress = getenv("REMOTE_ADDR");
    	$UserAgent = getenv("HTTP_USER_AGENT");
    	$DateCreated = date("Y-m-d");
    	$TimeStampAddedArray = getdate();
    	$TimeStampAdded = $TimeStampAddedArray[0];
    
    	$sql = "Insert Into Log (UserIDAuto, Action, Remote_ADDR, HTTP_User_Agent, DateCreated, TimeStamp) Values ($UserIDAuto,'$UserAction','$RemoteAddress','$UserAgent','$DateCreated','$TimeStampAdded')";
    
    	mysql_query($sql,$db);
    
    	?>
    
    	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    	<html>
    
    	<head>
    
    	<title> - News - Photo Added</title>
    
    	<table border=0 cellpadding=0 cellspacing=0 width=100%>
    
    	<tr>
    	 <td bgcolor="#CCCCCC" align="center"><br><b><font size="5">Photo Added</font></b><br><br></td>
    	</tr>
    
    	</table>
    
    	<table>
    
    	<br>
    
    	<table border="0" width=100%>
    
    	<tr>
    	<td width="100%"><center>The photo has been successfully added to the database.</center></td>
    	</tr>
    
    	</table>
    
    	</body>
    
    	</html>
    
    <?php
    }
    else
    {
    ?>
    
    	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    	<html>
    
    	<head>
    
    	<title> - News - Invalid File Format or Incorrect Image Size!</title>
    
    	<table border=0 cellpadding=0 cellspacing=0 width=100%>
    
    	<tr>
    	 <td bgcolor="#CCCCCC" align="center"><br><b><font size="5">Invalid File Format or Incorrect Image Size!</font></b><br><br></td>
    	</tr>
    
    	</table>
    
    	<table>
    
    	<br>
    
    	<table border="0" width=100%>
    
    	<tr>
    	<td width="100%"><center>The file uploaded must be in *.jpg, *.jpeg or *.gif format and the thumbnail must be in the correct pixel size (100*100)!</center></td>
    	</tr>
    
    	</table>
    
    	</body>
    
    	</html>
    
    
    <?php
    }
    }
    ?>
    <?php include_once("footer.php") ?>

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: file uploading

    They are explained here: http://uk.php.net/manual/en/features...oad.errors.php

    code 2 means you have exceeded the maximum file size allowed by the HTML, so the file was never actually transmitted.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: file uploading

    Thanks for that.

    I can see it obviously relates to this then.

    Code:
    <tr>
    <td align=left valign=top><b>Thumbnail:</b></td>
    <td align=left valign=top>
    
    <INPUT TYPE="file" NAME="img1" SIZE="50"></p>
    <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="8000" />
    The size of the thumbnail must be exactly 100 by 100 pixels and must be in the format of *.jpg, *.jpeg or *.gif
    
    </td>
    </tr>
    
    <tr>
    <td align=left valign=top><b>Photo:</b></td>
    <td align=left valign=top>
    
    <INPUT TYPE="file" NAME="img2" SIZE="50"></p>
    The image must be in the format of *.jpg, *.jpeg or *.gif
    
    </td>
    </tr>
    The problem I therefore have is that img1 should be limited to 8KB but img2 can be any size but I guess I can limit it to something like 1MB just for the hell of it.

    How do I seperate the max_file_sizes for the two files? If I include two:

    Code:
    <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="8000" />
    won't they cancel each other out?

    Any ideas?

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: file uploading

    The browser will take the latest one. But there is no way of separating them, I would impose an upper file size limit and test the smaller file inside your script.

    You should test inside the script anyway as the file upload limit is very easy to bypass.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: file uploading

    Done and working, thanks very much for your help mate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width