Forum Moderators: coopster

Message Too Old, No Replies

Uploading JPEG images through a form...

How can i upload a JPEG image through a form using php

         

PSWorx

8:57 am on Oct 3, 2003 (gmt 0)

10+ Year Member



I have created a script that uploads both text data and an image to a database, at the moment this script works fine with images of type GIF but when i try to upload a JPEG/JPG image in the same way the image isn't uploaded at all. The script is as follows (Im proving all of it as it may help with the answer):

<?php
$file_dir="images/";
$file_url=$Image;
if(isset($Image))
{
if($Image_type=="image/gif")
{
copy($Image,"$file_dir/$Image_name")or die("Couldnt copy image");
}
}
//Reads and prints the contents of the table.
$Image="<p><a href=\"images/$Image_name\" target=\"_blank\"><img src=\"images/$Image_name\" width=\"180\" height=\"180\" border=\"0\"></a></p>\n";
function read_and_print($Name,$Image,$Description)
{
$result=mysql_query("SELECT * FROM cus_gallery");
while($field=mysql_fetch_array($result))
{
print("<table id=\"pbslayout\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n<tr>\n<td>\n");
print"\t<div id=\"top\"><p id=\"title\">$field[Name]</p></div>\n";
print"<div id=\"mid\">$field[Image]\n";
print"<p id=\"text\">$field[Description]</p></div>\n";
print("<div id=\"bot\"></div>\n");
print("</td>\n</tr>\n</table>\n");
print"<hr>";
}
}
if (isset($Name)&&isset($Image)&&isset($Description))
{
if($Name=="")
{
$Name="No Name";
}
if($Description=="Enter a description of the product here")
{
$Description="No description";
}
$dberror="";
$ret=add_to_database($Name,$Image,$Description,$dberror);
if(!$ret)
print"Error: $dberror<BR>";
else
print write_form();
print"Thank you very much<p>Here is the information in the database:<hr>";
print read_and_print($Name,$Image,$Description);
}
else
{
write_form();
}
function add_to_database($Name,$Image,$Description,&$dberror)
{
$user="******";
$pass="******";
$db="displays";
$link=mysql_pconnect("localhost",$user,$pass);
if(!$link)
{
$dberror="couldnt connect to MySQL server";
return false;
}
if(!mysql_select_db($db,$link))
{
$dberror=mysql_error();
return false;
}
$query="INSERT INTO cus_gallery(Name,Image,Description)
values('$Name','$Image','$Description')";
if(!mysql_query($query,$link))
{
$dberror=mysql_error();
return false;
}
return true;
}
function write_form()
{
global$PHP_SELF;
print"<form action=\"$PHP_SELF\" method=\"POST\" enctype=\"multipart/form-data\"><a href=\"#\" name=\"top\"></a>\n";
print"<input type=\"text\" name=\"Name\">";
print"&nbsp;:Enter the product title here.<p>\n";
print"<input type=\"file\" name=\"Image\">\n";
print"<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"102400\">\n";
print"&nbsp;:&nbsp;Choose the products display image here.<p>\n";
print"<textarea type=\"text\" cols=\"50\" rows=\"10\" name=\"Description\">\n";
print"Enter a description of the product here";
print"</textarea><br>\n";
print"<input type=\"submit\" value=\"submitit!\">\n</form>\n";
}
?>

I have tried using the following line:

if($Image_type=="image/gif" or $Image_type=="image/jpeg")

But the results are still the same, no doubt i have missed something here, does anyone have a possible solution?

coopster

10:07 am on Oct 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have tried using the following line:

if($Image_type=="image/gif" or $Image_type=="image/jpeg")

But the results are still the same, no doubt i have missed something here, does anyone have a possible solution?

If you want to allow all types of images, go this route:


if (substr($Image_type, 0, 5)== 'image'))

PSWorx

10:21 am on Oct 3, 2003 (gmt 0)

10+ Year Member



Nope, unfortunatley not, perhaps its something to do with the header function header(). :-/ =?

Ive tried both:
if($Image_type=="image/gif" or $Image_type=="image/jpg")
and:
if($Image_type=="image/gif" or $Image_type=="image/jpeg")

But still the same results, not to worry im sure ill get there... thanks anyways

Ok the situation has been resolved thanks to those who participated in providing an answer... ;)

wireman

5:54 pm on Oct 3, 2003 (gmt 0)

10+ Year Member



I could start a new topic, but this is so close to the old. What if you want to upload a pdf file? Any solutions? Cheers.

coopster

6:14 pm on Oct 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Same as any other file upload [us3.php.net]. If you want, you can specify an accept attribute [w3.org] on your html <input> tag to specify pdf only. It would look something like this:

<input name="userfile" type="file" accept="application/pdf" />

But, you should also do some server side editing to make sure the upload is exactly what you want it to be. See the post in this thread by jatar_k [webmasterworld.com] for a starter on the editing that I'm referring to.

jatar_k

6:40 pm on Oct 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have to say I didn't know about the accept attribute, nice

When you run a file through an upload form take a look at what the file type ends up being, echo it.

If i remember correctly it should be "multipart/form-data"

coopster

7:57 pm on Oct 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, the type, if you are referring to the type in the predefined array $_FILES, will be the MIME type. For example:

text/plain
text/html
image/gif
etc.

Yeah, that accept attribute is neat, but don't trust it to stop the user from selecting a type you haven't specified! UA (User Agents, or more commonly known as Browsers) may use this information to filter out non-conforming files. I can confirm that both IE6.1 and NN7.1 will allow a user to upload a file with a .pdf extension even though the <input> tag has an accept="application/pdf" attribute. I think the attribute was designed more for server processing. The first sentence of the W3C statement says it all:

This attribute specifies a comma-separated list of content types that a server processing this form will handle correctly. User agents may use this information to filter out non-conforming files when prompting a user to select files to be sent to the server.

The second part would be pretty nice if browsers would look things over for you first. Wish in one hand, ...

RussellC

9:24 pm on Oct 3, 2003 (gmt 0)

10+ Year Member



<edited> nevermind </edited>

wireman

11:02 pm on Oct 3, 2003 (gmt 0)

10+ Year Member



Thanks.