PDA

View Full Version : PHP Uploader Script Problems




Macman1993
Jun 2, 2009, 12:00 AM
I have a php uploader script that has some file restrictions on it. It only allows certain kinds of pictures to be added. It also has a restriction that rejects any file over 500mb. I want to be able to upload any kind of file type but at the same time I want to keep the file size limit. Can someone please help me because I don't know php I just got this code off the w3schools page and really don't know how to edit it. If someone could edit the code for me I would really appreciate it.


<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/mp4")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 65540000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/mp4")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 65540000))
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>



angelwatt
Jun 2, 2009, 07:39 AM
Change
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/mp4")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 65540000))
{
to
if ($_FILES["file"]["size"] < 65540000)
{

Macman1993
Jun 2, 2009, 09:41 AM
Nope didn't work all I got was this.

Parse error: syntax error, unexpected T_STRING in /hermes/bosweb/web112/b1127/glo.clm51193/upload_file.php on line 16

If you want to test out the uploader I left the bad php script in it so you could see it. clm51193.com/uploader.html

Darth.Titan
Jun 2, 2009, 09:51 AM
What angelwatt gave you is correct. That error generally means you forgot to close a bracket or parentheses. Double check your code, because we cannot see the php from the URL you provided.

You've typed something wrong.

Macman1993
Jun 2, 2009, 12:36 PM
Ok I checked but still not working for me, heres the code I have running on the site now.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

angelwatt
Jun 2, 2009, 01:02 PM
You've got a bunch of asterisk in places like below that don't belong. Not sure how they got there, but should be deleted.
if*($_FILES["file"]["size"]*<*65540000)
**{*

Macman1993
Jun 2, 2009, 01:20 PM
Thats totally weird I have no idea how they got into my post being as they aren't in the code which I copy&pasted into the post. Another computer mystery I guess. Besides those which don't exist in my code anyway does anyone else see what could be wrong? Here is the code the way it really looks on my computer sorry for all the confusion.


<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

angelwatt
Jun 2, 2009, 01:37 PM
Are you still getting error messages? Also, what program are you using to edit the file?

Macman1993
Jun 2, 2009, 01:50 PM
Yup still getting the parse error every time I do something. Its totally weird. And I used bbedit to edit the php but it was originally written in text edit.

angelwatt
Jun 2, 2009, 02:18 PM
Yup still getting the parse error every time I do something. Its totally weird. And I used bbedit to edit the php but it was originally written in text edit.

It sounds like TextEdit may have left some invisible characters behind. In BBEdit you can turn on invisible characters so you can tell if there are any weird characters. Alternatively I would create a new file and type what you see in the file to make sure no odd characters get introduced.

Darth.Titan
Jun 2, 2009, 02:28 PM
If you're using TextEdit, make sure to go to Format->Make Plain Text.

I'd recomment downloading TextWrangler (http://www.barebones.com/products/TextWrangler/)(free) to edit PHP files.

maboroshi
Jun 2, 2009, 02:30 PM
try copying the code you posted here and pasting it into your php file. It ran without any errors for me.

Macman1993
Jun 2, 2009, 02:31 PM
I think your right, I just turned on the show invisible characters setting and it turns out that every space is actually an invisible circle that text edit put in. Thanks for the idea, I should have all of them gone soon and I'll say if that worked.

Macman1993
Jun 2, 2009, 02:44 PM
Ahh the drama continues only files that I had originally allowed are working, once I tried a .mp4 video I got this

Error: 1
Error: 1
Return Code: 1

Heres my code
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>

<?php
if ($_FILES["file"]["size"] < 65540000)
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

I really appreciate the time you guys are giving this. Hopefully you can fix the script so only files that are more then 500mb get denied.

angelwatt
Jun 2, 2009, 03:44 PM
From http://us3.php.net/manual/en/features.file-upload.errors.php
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
I haven't really dealt with file uploading. Have you tried small files or large ones? Also, have you tried various types of files e.g., images, text, etc.?

Macman1993
Jun 2, 2009, 05:52 PM
Everything works that is under 1mb I have raised the file limit to a number so ridiculously huge but it still wont work. This is getting really frustrating.

Macman1993
Jun 2, 2009, 06:15 PM
I have finally found the part of the php.ini that I had to edit thank you guys so much for the help I now have exactly what I wanted in my uploader.