Useful multiple file upload PHP script.

March 5th, 2008 by jason

I have created this script, that allows users to upload multiple files in PHP (find it attached at the end of this article). It is great for anyone that needs to add any sort of upload functionality, and would be a good base to make an upload service with.

Some features:

  • Dynamic amount of uploads.
  • Limit to only certain file types.
  • Size limit on uploads.
  • Avoids over writing due to duplicate file names.



You can edit the amount of uploads in the $numuploads variable, with simple edit you can allow users to select how many files they would like to upload. You can set the maximum upload size in the ‘maxsize’ hidden input. At default this script is set to only allow for Microsoft Word .doc files, but can bet set to anything in the $filetype == “…” IF statement, for example if you want to only allow MP3s set the filetype to audio/mpeg.

Hope this helps you, leave a comment if you have any questions.

Multi file upload script

Posted in PHP Code

22 Comments

  1. rk

    hi jason,

    sorry if this sounds lame, but where do you specify the directory and the domain name where the files should be uploaded to?

  2. jason

    Have a look at the $target variable ;)

  3. kazz

    Is it possible to email the files or email someone of a new upload?

  4. Steve

    Hi great script thanks!

    say if I want to only accept a ceratin file type, for example *.tos, how would I do that?

  5. Steve

    sorry just found out you wrote it above :)

  6. Stadford

    Script look great, but is it possible to allow every file type to be uploaded?

  7. Chris

    @ Stadford

    Just look at line 15 and make it the following:

    if ($filetype != ” “)

    That should do it quick and dirty :o)

    Chris

  8. Newbie

    I don’t know what a .rar filetype is. How am I supposed to run it? Sorry but I’m new to this. thanks.

  9. Leonard

    hi, thanks for the script

    i have 2 question.

    what if i want to allow upload of jpg, gif and png files? (3 file types)

    where do i set the upload dir?

    hope you can help, thnaks.

  10. leo

    hi,

    thanks for the script.

    is it possible for me to enable multiple file extensions? eg, jpg, gif and png?

    thanks

  11. Reinder

    Yes of course, that is not so hard :)
    Look at if($filetype = “application/msword”)
    For example you can do:
    if($filetype = “image/jpeg” or $filetype = “image/png” or $filetype = “image/gif”){
    This will check for jpeg or png OR gif. Quite easy not? :)

  12. Pompey Photos

    An easy way to set allowed file extensions is if ($string1 == “jpg” || $string1 == “JPG” || $string1 == “jpeg” || $string1 == “JPEG”||…………..) etc …….’do this’….

    Always advisable to check the filetype properly, not just using the extension…

    use…

    if ($_FILES["xxxxxxx"]["type"][0] == “image/jpeg”)…….’do this’………

  13. BW

    Hi:

    Excellent script. Love it! Quick question from a newbie. sorry.
    How do I specify the exact location (directory and all) in $target. Can you please give an example? Many thanks!

  14. Yurii

    Nice script! Thanks!

  15. Daleos

    Hi Jason, just found this script which looks exactly what I’m looking for. what do I need to make sure that its secure and not open to abuse? Cheers, D.

  16. Donovan

    If you add a “Rating” system to this then I believe you would be the only one! I have searched hi and low for an MP3 upload that allows members to rate or vote for on MP3 and I cannot find one. It would need to have protection from more than 1 vote from the same IP.

  17. mike

    Hello, would anyone know how to add video files such as, FLV, Mpeg, or WMV to be the files you want to upload? does not seem to work you put, video/flv or flash/flv?

  18. mike

    Hello, would anyone know how to add video files to upload, like, flv, wmv or mpeg? all my efforts have failed, alls i keep getting is the improper file error?

  19. angelina

    HI,

    I’m developing a site that allows for multiple jpg upload but after they are uploaded, i need the script to do some processing like file rename, and then make an entry in the database for each file.

    Any suggestions??

    Cheers!

  20. Cola-Cola

    Nice script, but how can I put the form on some page of Wordpress Blog? Forgive me I don“t understand php :,,(

  21. Jason

    What would be really cool if after the upload it provided a list of the newly created URLs for the uploaded files, maybe someone who knows what they are doing can hook it up?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.