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.

Whenever I try setting file permissions in filezilla I get an error

“Response: 500 ‘SITE CHMOD 777 uploads’: command not understood”

Ok, I set permissions on my GoDaddy account to allow writing and it seems to have worked…

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.

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?

Are you running IIS? If so read this http://koderguru.com/tutorials/phptutorials/permissiondenyiis.php

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?

There is also a lot of info on this in da manual.

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?

Great, thanks a lot. I’ll give it a try!

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.

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

<!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

<?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