Page 1 of 1

Image Upload is Killing me

Posted: Fri Nov 03, 2006 4:59 pm
by NotOnUrNelly
Hi All,

I am struggling to get an image upload procedure to work.

I have copied the code form a previously successful site of mine.

The problem is centred around the ['name'] part of

Code: Select all

$_FILES['file']['name']
The file to be uploaded is determined from a form code below

Code: Select all

<?php
session_start();
$pubid = $HTTP_GET_VARS['pubid'];
$imagetype = $HTTP_GET_VARS['ImageType'];
?>
<body bgcolor="#CCCCCC">
<table border="0" width="80%" cellspacing="5" cellpadding="5">
<form action="fileupload.php" method="post" enctype="multipart/fomr-data" target="_self">
<tr>
    <td><font color="#000000" size="3" face="Arial, Helvetica, sans-serif"><strong>Comment</strong></font></td>
</tr>
<tr>
<td><textarea name="comment" cols="50" id="comment"></textarea></td>
</tr>
<tr>
<td>
<input type = "file" size="40" name="file">
</td></tr>
<tr>
<td colspan="2"><input type="hidden" name="pubid" value="<?php echo $pubid ?>">
      <input name="upload" type="submit" value="upload">
      <input type="hidden" name="imagetype" value="<?php echo $imagetype ?>">
      <input type="hidden" name="MAX_FILE_SIZE" value="10000"></td>
    <td colspan="2">&nbsp;</td>
    <td colspan="2">&nbsp;</td>
</tr>
<tr><td>
    <td colspan="2">&nbsp;</td>
</tr>
</form>
</table>
there may be a problem here with the line

Code: Select all

<input type = "file" size="40" name="file">
The code that uploads the file is found in a seperate file fileupload.php

Code: Select all

<?php
session_start();
include ('db_fns.php');
echo $HTTP_SESSION_VARS['PubID'];
echo $HTTP_SESSION_VARS['UserID'];
echo $HTTP_SESSION_VARS['RealName'];
$person = $HTTP_SESSION_VARS['RealName'];
$pubid = $_POST['pubid'];
$key_id = $pubid;
$imagetype = $_POST['imagetype'];
$comment = $_POST['comment'];

if (isset($_POST['upload'])){
echo "Upload Set";




//if (!empty($_FILES['file']['name'])){

$formats = array('png','jpg');
echo "Files Set";
$img_path = "/domains/r/runthelakes.co.uk/user/htdocs/ale/images/".$ImageType."/";
echo $img_path;
echo '<br>';

echo $_FILES['file']['name'];
if(!in_array(strtolower(substr($_FILES['file']['name'],-3)),$formats)){


echo "<font face =\"verdona,arial,helvetica\" size=\"2\" color=\"#000000\"><b>Sorry ! only .JPG's and .PNG's are allowed</b></font>";

}else{
$conn=db_connect();
}

$query = sql_return("insert into images (key_id,owner,comment,image_name,image_path,album_id)values('".$key_id."','".$person."','".$comment."','".$_FILES['file']['name']."','".$img_path."','".$imagetype."'");
echo "Query :".$query;
//$result = mysql_query($query) or die (mysql_error());

$newname=mysql_insert_id().".jpg";
copy($_FILES['file']['tmp_name'],$img_path . $newname);
unlink($_FILES['file']['name']);

echo "Image Uploaded Successfully";
//require("THUMBNAIL3.inc");
//thumbs($newname,$img_path,$img_path,"50","th");
//thumbs($newname,$img_path,$img_path,"400","th");

}

else
{
echo "You must specify a file to upload";
exit();
}
//}

?>
I think the problem line here is

Code: Select all

//if (!empty($_FILES['file']['name'])){
Which I have commented out.

When the line is commented out the code runs but the $_FILES part is missing from the sql insert code that I print to the screen

I also get the incorrect format message that comes from

Code: Select all

if(!in_array(strtolower(substr($_FILES['file']['name'],-3)),$formats)){


echo "<font face =\"verdona,arial,helvetica\" size=\"2\" color=\"#000000\"><b>Sorry ! only .JPG's and .PNG's are allowed</b></font>";
Again which I suspect has something to do with

Code: Select all

if(!in_array(strtolower(substr($_FILES['file']['name'],-3)),$formats)){

Hope someone can help is driving me mad

Cheers

Jamie

Posted: Fri Nov 03, 2006 5:08 pm
by pickle
The first problem I found was with your form. The MAX_FILE_SIZE element needs to be placed in the form before the file element. That might be causing some of your upload problems. Also, while using $HTTP_SESSION_VARS isn't going to break anything, that array is deprecated. You should use $_SESSION instead.

To be honest, I didn't look any further through your code - I'm not sure what your problem status will be after you fix what I mentioned, so I didn't bother.

Posted: Fri Nov 03, 2006 5:11 pm
by nickvd
change enctype="multipart/fomr-data"
to enctype="multipart/form-data"

Posted: Fri Nov 03, 2006 5:17 pm
by feyd
Also, standards compliant browsers will handle form tags between table tags differently. Move them outside of the table tags.

Posted: Fri Nov 03, 2006 5:29 pm
by NotOnUrNelly
Thanks for your help guys,

I think it was the misspelling of the form-data, that caused my problems.

its took me all night trying to work out what was wrong.

Thanks a lot

Jamie