/    Sign up×
Community /Pin to ProfileBookmark

Restricting uploads to zip

Can someone please fill the gap in this condition to ensure that users are only able to upload .zip files via my script? I could only find solutions for uploading images.

[code]
if ($_FILES[“file”][“size”] < 3145728 && $_FILES[“file”][“type”]== _________){

my code

}

Thanks πŸ™‚
[/code]

to post a comment
PHP

20 Comments(s) ↴

Copy linkTweet thisAlerts:
@aatwoauthorJan 07.2007 β€”Β Edit: I made a mistake. The operator should be == rather than != which may have confused people.
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 β€”Β This is a useful reference in case you want to expand your list to other compressed files: http://www.webmaster-toolkit.com/mime-types.shtml

The answer to your specific question would be: "application/x-zip-compressed".
Copy linkTweet thisAlerts:
@aatwoauthorJan 07.2007 β€”Β Thanks very much for that list.

Do you know why .rar is not on it?

Also do you know why some file extensions are mentions several times (.zip is mentioned on the list 4 times).

?
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β .zip application/x-compressed

.zip application/x-zip-compressed

.zip application/zip

.zip multipart/x-zip

It's a good question... I can only guess that over time, there has been a need to differentiate between one and the other, i.e. open WinZip automatically as opposed to just downloading the file. But don't quote me...

This is the official MIME site, but you better sit down first...

http://www.iana.org/assignments/media-types/
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β Do you have any idea why this is not working? It's not allowing through any files now.

<i>
</i>if ($_FILE["file"]["type"]=="application/x-zip-compressed" || $_FILE["file"]["type"]=="application/x-compressed" || $_FILE["file"]["type"]=="application/zip" || $_FILE["file"]["type"]=="multipart/x-zip"){

my code

}
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β Put[code=php]echo $_FILE["file"]["type"];[/code]right before the [b]IF[/b] and upload a .ZIP... It'll tell you why...
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β echo $_FILE["file"]["type"] has no value.

It echos out nothing. What does that mean?!
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β You may not have a file...

Try:[code=php]print "<hr>";
print_r($_FILE);
print"</pre>";[/code]
in place of ECHO.
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β Using that method or the echo method, irregardless of what type of file I upload the output is always blank (except for the horizontal rule of course). I'm very confused. The file type must contain a value :s
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β It was a typing mistake. Should've been this:[code=php]print "<pre>";
print_r($_FILE);
print"</pre>[/code]
Can you post the output?
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β Sorry... blew it again...[code=php]print "<pre>";
print_r($_FILES);
print"</pre>";[/code]
This time, missed the "S" in $_FILES.
Copy linkTweet thisAlerts:
@bokehJan 08.2007 β€”Β [CODE]$_FILE[B][color=red]S[/color][/B][/CODE]

By the way this test proves nothing as it is user controllable data.
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β Edit: Ahh ok sorry here it is.

Array

(

[file] => Array

(

[name] => timetable.zip

[type] => application/x-zip-compressed

[tmp_name] => C:PHPuploadtempphp3149.tmp

[error] => 0

[size] => 34819

)

)
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β Please change:[code=php]print_r($_FILE);[/code]to:[code=php]print_r($_FILES);[/code]
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β Done and edited above. But to save you moving your eyes all the way up there here it is again.

<i>
</i>Array
(
[file] =&gt; Array
(
[name] =&gt; timetable.zip
[type] =&gt; application/x-zip-compressed
[tmp_name] =&gt; C:PHPuploadtempphp3149.tmp
[error] =&gt; 0
[size] =&gt; 34819
)

)
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β Your array needs to be called [B]$_FILES[/B] and not [B]$_FILE[/B] ... throughout your code.
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β I just noticed that after you corrected yourself. Thanks alot. I'll go re-test it and come back with hugs and kisses if it works ?
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β I'm referring to this:
[code=php]if ($_FILE["file"]["type"]=="application/x-zip-compressed" || $_FILE["file"]["type"]=="application/x-compressed" ||
$_FILE["file"]["type"]=="application/zip" || $_FILE["file"]["type"]=="multipart/x-zip"){[/code]
Copy linkTweet thisAlerts:
@aatwoauthorJan 08.2007 β€”Β *Hugs and kisses*

Thanks a lot. This stuff is tough considering I started learning PHP this morning -_-
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 β€”Β Don't know about hugs and kisses ? but you're welcome...
Γ—

Success!

Help @aatwo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 5.18,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...