Search code examples
phplinuxflashfile-uploadswfupload

SWFUpload image upload fails on PHP with 'Partial File Uploaded'


Okay, so this is a tough one, I've spent hours looking for a solution/problem. I'm using SWFUpload to upload images without reloading the page (with a fancy progressbar too), this works fine when I'm on localhost (Wamp server), but it goes nuts when I try to do so on my real linux server (which is the only possible flag as far as I could see), it's running Apache2 and PHP5. As I said the front-end is fine (apart maybe from the fact it's flash). The back-end code is as follows:

SWFUpload_settings.js

var swfu_settings ={upload_url  : "upload.php",
                flash_url   : "flash/swfupload.swf",
                button_placeholder_id   : "upload_flash",
                file_size_limit : "2 MB",
                file_types  : "*.gif;*.jpg;*.png",
                file_post_name  :   "Filedata",
                file_queue_limit    :   1,
                post_params     :   {
                        "PHPSESSID" : getCookie()
                },
                upload_start_handler    : upload_start,
                upload_error_handler    : upload_error,
                upload_complete_handler : upload_complete,
                upload_progress_handler : upload_progress,
                file_queued_handler :   file_queued,
                button_disabled : false,
                button_width : 120, 
                button_height : 22,
                button_text : '<div class="adm_upload">Select image</div>',
                button_text_style : '.adm_upload{color:#ff0000;}'
                };

upload.php

function manageUpload(){
        if( isset($_FILES['Filedata']) ){
            $dest_dir = $_SERVER[DOCUMENT_ROOT]."/images/products";
            $destination =  $_SERVER[DOCUMENT_ROOT]."/images/products/" . $_FILES['Filedata']['name'];
            if( is_dir($dest_dir) ){
                if( is_writable($dest_dir) ){
                    if( !move_uploaded_file($_FILES['Filedata']['tmp_name'], $destination ) ){
                        $html_body = '<h1>File upload error!</h1>';
                        switch ($_FILES['Filedata']['error']) {
                            case 1:
                                $html_body .= 'The file is bigger than this PHP installation allows';
                                break;
                            case 2:
                                $html_body .= 'The file is bigger than this form allows';
                                break;
                            case 3:
                                $html_body .= 'Only part of the file was uploaded';
                                break;
                            case 4:
                                $html_body .= 'No file was uploaded';
                                break;
                            default:
                                $html_body .= 'unknown errror';
                        } 
                        echo ($html_body);
                    }
                }
                else{
                    echo "Says it's not writable: ".$dest_dir;
                }
            }
            else{//not a directory?
                echo "Says it's not a directory:".$dest_dir;
            }
        }
        else{
            echo "No file POSTED.\n";
        }
    }

The only error that I get is from $_FILES['Filedata']['error'] = 3, 'Only part of the file was uploaded'. The destination directory does have 777 permission and you can see I made the required checks. It simply will not work, I have no idea why. Also, the files I tried uploading had no spaces in the filename, so that shouldn't fit under the issue 206 with SWFUpload.

As far as I can tell it can be either due to the front-end SWFUpload or back-end server configuration. Please help.

P.S. no need to mention security, this is only allowed to be used by the server admin with external access anyhow, plus there's the front-end limitation on the files he can select (images). There was no point securing it further.


Solution

  • So it turns out the problem was with the SWFUpload itself, it simply failed when used with linux servers. To anyone who has similar problems, consider using this instead:

    ridiculously easy to adapt, works out of the box and you can customize it to your hearts content with ease. I implemented all I wanted in under 20 minutes, it's a pure delight to use and works well. Beautifully designed.