Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 25th, 2004, 07:10 PM
Authorized User
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default uploading different file types

I was able to download images with code from

Now, I tried to download other types of files like texts in .doc, pdf, txt, video in .mov and .avi.
I attempted to change lines

if (($_FILES['userfile']['size'] <= 10000) && ($_FILES['userfile']['type'] == 'image/jpeg' || $_FILES['userfile']['type'] == 'image/pjpeg'))
$new_file_name = mktime().'.jpg';

to another file extension like

if (($_FILES['userfile']['size'] <= 10000) && ($_FILES['userfile']['type'] == 'image/txt' || $_FILES['userfile']['type'] == 'image/rtf'))
$new_file_name = mktime().'.txt';

However, it gives an error. Could you please help me to change a code so I can let user upload any type of files?
Thank you,

Old July 25th, 2004, 09:46 PM
richard.york's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

When you upload the file output the value of the MIME type so that you know what type of file the server sees it as.

echo "Upload failed: File must be a JPEG file type and 10KB or less in size<br />\n";
echo "The real MIME type is: {$_FILES['userfile']['type']}.<br />\n";
echo "The real file size is: {$_FILES['userfile']['size']}.<br />\n";

That said, the MIME type for .txt is text/plain. The MIME type for .doc is application/ms-word .. output the 'type' value to get the MIME type for the other extensions that you want to be able to upload.

If you want the user to be able to upload any type of file, remove the MIME type testing all together.

if ($_FILES['userfile']['size'] <= 10000)

That still leaves a size constraint of 10KB. Increase that number for larger files. Don't forget there is an upper constraint of 2MB in the php.ini file.


The Spicy Peanut Project
Old July 25th, 2004, 11:32 PM
Friend of Wrox
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran

hi lanita
find this link, this will help you


(Anything is Possible)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
Determining File Types seananderson Access VBA 2 February 12th, 2007 12:59 PM
Please help me!!!How can I recognize file types tqbinh C# 1 August 13th, 2004 09:45 AM
BLOB field file types kend SQL Server ASP 0 December 19th, 2003 02:45 PM
File uploading zabedin Classic ASP Basics 1 July 16th, 2003 08:33 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.