SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP file uploading from a form

    I'm looking for help processing form data as well as file uploads. I've got my .html form set up complete with several data fields as well as 4 file-upload fields. I don't know anything about programming, but am relatively certain I need to use a .php script to make my form work.

    All I want to do is have the form data and files stored in a folder on my websites server for me to access. The form I have will be used infrequently so I don't need to do any major processing to the data; perhaps just putting it into a basic txt document would help. I'm very new to web design so I don't really know where to begin. I'm currently using GoDaddy for my web hosting, if that helps.

    If anyone could at least point me in the right direction it would help a lot!
    Thanks.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This is just a basic version but it will do great for what you need it for. Here is the HTML

    File Name: index.html

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; UTF-8" />
    <title>Upload Form</title>
    </head>
     
    <body>
     
    <form action="upload.php" method="post" enctype="multipart/form-data">
    File Input #1: <input type="file" name="upload_fields[]" /><br />
    File Input #2: <input type="file" name="upload_fields[]" /><br />
    File Input #3: <input type="file" name="upload_fields[]" /><br />
    File Input #4: <input type="file" name="upload_fields[]" /><br />
    <input type="reset" value="Reset" /> <input type="submit" name="submit" value="Submit" />
    </form>
     
    </body>
    </html>

    and here is the PHP

    File Name: upload.php

    Code PHP:
    <?php
     
    // Allowed Extensions
    $AllowedExtensions = array('png','gif','jpg','jpeg','zip','rar');
    // Maximum File Size (In Bytes)
    $max_file_size = '10485760'; #<-- 10 MB/s
    // Path For File Uploads
    $upload_path = 'uploads/';
    // Errors Array
    $errors = array();
     
    if (isset($_POST['submit'])){
        // Loop through the upload fields
        for($i=0; $i<count($_FILES['upload_fields']['name']); $i++){
            // Set the upload file name and pathparts
            $filename = $_FILES['upload_fields']['name'][$i];
            $pathparts = pathinfo($filename);
            // Check to make sure the file field isn't empty
            if (!empty($filename)){
                // Check the filesize
                if ($_FILES['upload_fields']['size'][$i] <= $max_file_size){
                    // Check to make sure the file doesn't already exist
                    if (!file_exists($upload_path.$filename)){
                        // Check the file extension
                        if (in_array($pathparts['extension'], $AllowedExtensions)){
                            // Now upload the file
                            if (move_uploaded_file($_FILES['upload_fields']['tmp_name'][$i], $upload_path.$filename)){
                                echo '<font color="green">The file <strong>'.$filename.'</strong> was successfully uploaded!</font><br />';
                            } else {
                                $errors[] = '<font color="red">The file <strong>'.$filename.'</strong> failed to upload!</font>';
                            }
                        } else {
                            $extension_list = '';
                            while(list($null,$extension) = each($AllowedExtensions)){
                                $extension_list .= ($extension_list != '') ? ', '.$extension : $extension;
                            }
                            $errors[] = '<font color="red">The file <strong>'.$filename.'</strong> has an incorrect extension type, please make sure its one of the following ( <font color="black"><strong>'.$extension_list.'</strong></font> )</font>';
                        }
                    } else {
                        $errors[] = '<font color="red">The file <strong>'.$filename.'</strong> already exists in the upload folder!</font>';
                    }
                } else {
                    $errors[] = '<font color="red">The file <strong>'.$filename.'</strong> exceeds the maximum file size aloud!</font>';
                }
            } else {
                $errors[] = '<font color="black">#'.($i+1).': </font><font color="red">The following upload field in the form is empty there for the no file was uploaded from it!</font>';
            }
        }
        // Check for errors
        if (!empty($errors)){
            echo '<br />';
            foreach($errors as $error){
                echo $error.'<br />';
            }
        }
        // Return Link
        echo '<br />[ <a href="index.html">Return To Form</a> ]';
    }
     
    ?>

    By default I've set the upload size to 10 MB/s but if you goto Google and type in say 20 megabytes in bytes it will do the conversion for you. Let me know if there's any problems with it or if you get confusing about something in it

  3. #3
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, thanks a lot. I'll give it a try!

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    There is also a lot of info on this in da manual.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I keep getting a "500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed."

    I think it's because I'm using a windows server that does not support PHP. Does this make sense? I think I might need to look for an asp script.

    edit: My hosting account on GoDaddy says I have PHP 5.x, so does anyone know why I'm getting an error?

  6. #6
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is just a basic version but it will do great for what you need it for. Here is the HTML
    Other than uploading these two files to my server is there any modifications or directories I need to add? Will the PHP you posted work with a form I've already created as long as I set the action and proper enctype?

  7. #7
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright so I used the code provided in the original post and chose a bunch of random jpegs from my computer, here's the error I got:

    Warning: move_uploaded_file(uploads/Avatar 1.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\6026795\html\upload.php on line 27

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php69DB.tmp' to 'uploads/Avatar 1.jpg' in D:\Hosting\6026795\html\upload.php on line 27

    Warning: move_uploaded_file(uploads/Perspective Panels.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\6026795\html\upload.php on line 27

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php85E4.tmp' to 'uploads/Perspective Panels.jpg' in D:\Hosting\6026795\html\upload.php on line 27

    The file Avatar 1.jpg failed to upload!
    The file Perspective Panels.jpg failed to upload!

    [ Return To Form ]
    sorry for the multiple posts.

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you running IIS? If so read this http://koderguru.com/tutorials/phptu...iondenyiis.php

  9. #9
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    Are you running IIS? If so read this http://koderguru.com/tutorials/phptu...iondenyiis.php
    Whenever I try setting file permissions in filezilla I get an error

    "Response: 500 'SITE CHMOD 777 uploads': command not understood"

  10. #10
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That error after searching Google is due to the CHMOD extension been disabled on your system but from what i read it does not exist nor is needed on Windows but in another search i read that sometimes even if that error appears the CHMOD may still have succeeded.

    Have you tested to see if the uploads work?

  11. #11
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got php to work and send form data to my email address. I still can't get file uplaods working. I got this error when running a test with a txt file.


    Warning: move_uploaded_file(./files/file_1_58441026052010.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\6026795\html\uploadtest\processor.php on line 8

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\phpE44B.tmp' to './files/file_1_58441026052010.txt' in D:\Hosting\6026795\html\uploadtest\processor.php on line 8
    File jjj.txt was not uploaded.
    I'm not running the original script. This is a new script but same error as before.

  12. #12
    SitePoint Member
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I set permissions on my GoDaddy account to allow writing and it seems to have worked....


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •