PHPBuilder - File uploads made easy Page 5



RSS Twitter
Articles Php Functions

File uploads made easy - Page 5

by: Darren Beale
|
August 31, 2007

This next function isn't really needed, but I've put it in so our user gets some confirmation that the file was uploaded, all it does is loop through the upload directory and print out the files in it, except for '.' and '..'

<?php

function list_files() {

global 
$the_path;
    
    
$handle dir($the_path);
    print 
"\n<b>Uploaded files:</b><br>";
    while (
$file $handle->read()) {
        if ((
$file != ".") && ($file != "..")) {
            print 
"\n" $file "<br>";
           }
    }
    print 
"<hr>";
}

?>
Our last function brings all of our functions together. It takes the file as input and then uses validate_upload() to check that it is allowed. If it is then we need to copy it from the temp directory (this is specified in php.ini) and move it to it's final resting place. One last sanity check is to see that we can copy to that directory. If this falls over it is almost certainly a permission problem or an invalid path. If everything is OK, then we call list_files() and then re-display the form.

<?php

function upload($the_file) {

global 
$the_path,$the_file_name;
    
    
$error validate_upload($the_file);
    if (
$error) {
        
form($error);
    } else { 
# cool, we can continue
        
if (!@copy($the_file$the_path "/" $the_file_name)) {
            
form("\n<b>Something barfed, check the path to and ".
            
"the permissions for the upload directory</b>");
        } else {
            
list_files();
            
form();
        }
    }
# END upload

?>
Our last bit of code now is to kick things off. We use the case() statement to switch between tasks. It's a slight overkill here, but I've left it in so you can see how it might be used, for larger projects it makes for easily readable code, though some developers might disagree with me.
If $task = upload then the upload process begins, otherwise as a default we just show the form

<?php

print "<html>\n<head>\n<title>Upload example</title>\n</head>\n<body>";

switch(
$task) {
    case 
'upload':
        
upload($the_file);
    break;
    default:
        
form();
}

print 
"\n</body>\n</html>";

?>
That's it, upon running this script the user will see just a form with an upload field, they browse the hard-drive for a file and click submit. If there are no errors in the validation stage, they will get returned a directory listing of the upload folder and the form again so they can upload another file.
-- Bealers

Notes:

I am well aware that I've added no error checking for the max_file_size, AFAIK the only way to gracefully capture this error is to turn all error_reporting off in php.ini and do code based checking. However I like to see my errors for development, so I guess we are stuck with the messy PHP error that is generated.
This example does not show you what to do with the image once it is uploaded. For example, as I will be for the next release of Back-end, you might want to allow a user to upload an image for a news article. In this case you would possibly add the file name to the article so when the page is viewed, php can pull the corresponding image onto the page. I'll leave you to figure that one out =)

« Previous Page
1
|
2
|
3
|
4
|
5

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Darren Beale

Comment:



Comment:

(Maximum characters: 1200). You have characters left.