Image Upload is Killing me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Image Upload is Killing me

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

change enctype="multipart/fomr-data"
to enctype="multipart/form-data"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Also, standards compliant browsers will handle form tags between table tags differently. Move them outside of the table tags.
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post 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
Post Reply