php help. file submit script.

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
luke_barnes
Forum Newbie
Posts: 6
Joined: Thu Apr 09, 2009 6:45 pm

php help. file submit script.

Post by luke_barnes »

this is my code for a submit form.
all i want to do id create a script that allows users to upload files but only in zip or rar files.

Code: Select all

 
<?php
 
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
 
if($error > 0)
 
die("Error uploading file! Code $error.");
else
{
 
if ($type!="application/zip" && $type!="application/rar") //conditions for the file
{
die("Format not allowed or file size too big!");
}
else
{
move_uploaded_file($temp,"uploaded/".$name);
echo "Upload complete!";
}
 
}
 
 
 
?>
 
the problems im having is that this code lets nothing through and if i change != to == then it just lets everything through. i cant win at the moment. any idea?
Last edited by Benjamin on Sat May 02, 2009 4:21 pm, edited 1 time in total.
Reason: Added php tags.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php help. file submit script.

Post by McInfo »

Save this script to your server and use it to upload some samples of acceptable files. Take note of the MIME types of the uploaded files. You probably are not testing for the correct types. I uploaded a RAR file to my server and its type was "application/octet-stream".

Code: Select all

<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="uploadedFile" />
    <input type="submit" value="Upload" />
</form>
<pre><?php
if (!empty($_FILES)) {
    print_r($_FILES);
}
?></pre>
Here is a list of possible ZIP MIME types from webmaster-toolkit.com. Unfortunately, their list does not include RAR.

Code: Select all

application/x-compressed
application/x-zip-compressed
application/zip
multipart/x-zip
Edit: This post was recovered from search engine cache.
Post Reply