Skip to main content
Inspiring
August 9, 2007
Question

PHP upload

  • August 9, 2007
  • 10 replies
  • 577 views
Hello,

I am using Dreamweaver Mx 2004 to develop a PHP site. I am trying to
implement a file upload page but I have run into a problem. Users are
supposed to select a file from one page and this file is supposed to be
uploaded using another. This is a snippet from the page where the user
selects the file:
--------------------------------------------------------------------------------------------
<form action="upload.php" method="post" enctype="multipart/form-data"
name="form1">
<label for="file">Filename:</label>
<table width="200" border="1">
<tr>
<td width="133"><input name="filefld" type="file" id="filefld"></td>
<td width="51"> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</form>
---------------------------------------------------------------------------------------------
The contents of the upload.php file are as follows:

<?php

if $_FILES["file"]["type"] == "image/pjpeg")
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("photos/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"photos/" . $_FILES["file"]["name"]);
echo "Stored in: " . "photos/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
" http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>


After selecting a .JPG file I click on the submit button and the upload.php
file loads but nothing happens (meaning that the selected file is not
downloaded). I have checked the php.ini file to ensure that all the
necessary setting are there and that the temp upload folder has the
necessary windows permissions but still no luck. Any help would be
appreciated.

Liam



This topic has been closed for replies.

10 replies

Inspiring
August 10, 2007
both suggestions worked out fine. Thanks for the help.

Liam


"David Powers" <david@example.com> wrote in message
news:f9eqlg$dh4$2@forums.macromedia.com...
> MikeL7 wrote:
>> I think you need to change to this:
>>
>> @copy($_FILES["filefld"]["tmp_name"],
>> "photos/" . $_FILES["filefld"]["name"]);
>
> Using copy for an uploaded file is a major security risk. You should use
> move_uploaded_file() instead.
>
> http://www.php.net/manual/en/function.move-uploaded-file.php
>
> --
> David Powers, Adobe Community Expert
> Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
> Author, "PHP Solutions" (friends of ED)
> http://foundationphp.com/


Inspiring
August 10, 2007
Thanks for the replies guys. I am gonna try them out now.

Liam


"David Powers" <david@example.com> wrote in message
news:f9eqos$dh4$3@forums.macromedia.com...
> Gary White wrote:
>> This:
>>> if $_FILES["file"]["type"] == "image/pjpeg")
>> Should be:
>> if ($_FILES["file"]["type"] == "image/jpeg")
>
> Actually, it should be this:
>
> if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] ==
> "image/pjpeg")
>
> You need to test for both, because Internet Explorer uses the non-standard
> image/pjpeg MIME type.
> --
> David Powers, Adobe Community Expert
> Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
> Author, "PHP Solutions" (friends of ED)
> http://foundationphp.com/


Inspiring
August 9, 2007
On Thu, 09 Aug 2007 16:30:02 +0100, David Powers <david@example.com>
wrote:

>I discovered recently that Firefox uses a non-standard MIME type for PDF
>files (forget what it is off-hand). So much for standardization.

Guess not everyone reads the RFC's. ;-)

Thanks again.

Gary
Inspiring
August 9, 2007
.oO(David Powers)

>MikeL7 wrote:
>> I think you need to change to this:
>>
>> @copy($_FILES["filefld"]["tmp_name"],
>> "photos/" . $_FILES["filefld"]["name"]);
>
>Using copy for an uploaded file is a major security risk. You should use
>move_uploaded_file() instead.

... and never suppress error messages with the '@' operator unless you
_exactly_ know what you're doing.

Micha
Inspiring
August 9, 2007
Gary White wrote:
> Okay. Thanks. I never knew that. Guess I always tested the file
> name/extension directly and never use the mime type from the $_FILES
> array.

It's one of those things that you find out the hard way. ;-)

I discovered recently that Firefox uses a non-standard MIME type for PDF
files (forget what it is off-hand). So much for standardization.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
August 9, 2007
On Thu, 09 Aug 2007 11:36:54 +0100, David Powers <david@example.com>
wrote:

>You need to test for both, because Internet Explorer uses the
>non-standard image/pjpeg MIME type.


Okay. Thanks. I never knew that. Guess I always tested the file
name/extension directly and never use the mime type from the $_FILES
array.

Gary
Inspiring
August 9, 2007
Gary White wrote:
> This:
>> if $_FILES["file"]["type"] == "image/pjpeg")
> Should be:
> if ($_FILES["file"]["type"] == "image/jpeg")

Actually, it should be this:

if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"]
== "image/pjpeg")

You need to test for both, because Internet Explorer uses the
non-standard image/pjpeg MIME type.
--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
August 9, 2007
MikeL7 wrote:
> I think you need to change to this:
>
> @copy($_FILES["filefld"]["tmp_name"],
> "photos/" . $_FILES["filefld"]["name"]);

Using copy for an uploaded file is a major security risk. You should use
move_uploaded_file() instead.

http://www.php.net/manual/en/function.move-uploaded-file.php

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
August 9, 2007
I think you need to change to this:

@copy($_FILES["filefld"]["tmp_name"],
"photos/" . $_FILES["filefld"]["name"]);

the ['filefld'] is the name of the file field in your form, i use @copy but at least change the ['file'] to ['filefld']

Inspiring
August 9, 2007
On Wed, 8 Aug 2007 20:25:20 -0400, "Macromedia Forums"
<liaml1@hotmail.com> wrote:

This:
> if $_FILES["file"]["type"] == "image/pjpeg")
Should be:
if ($_FILES["file"]["type"] == "image/jpeg")


Gary