在表单页面上发布.php文件上载脚本的输出

时间:2012-08-13 14:03:18

标签: php json

我被要求编写一个脚本,将文件上传到网络服务器,然后在用于选择要上传的文件的表单下方显示输出消息。我无法显示输出消息。相关的html表单和javascript如下:

<script type="text/javascript">
$(function() {
$('#fileuploader').submit(function(e) {
    e.preventDefault(); // e.preventDefault() for prevent form submisson with page reload
    $.ajax({
        type: "POST",
        url: "genericuploader.php",
        data: dataString,
        cache: false,
        success: function(html) {
            $("#message_post").prepend(html);
            close_box();
        }
    });

});
});
</script>

<form id="fileuploader" action="genericuploader.php" method="post">
        Select a file to upload: <input name="uploaded" type="file" /><br />
        <select name='department'>
            <option value="0">Select a Department</option>
            <option value="D1">Department 1</option>
            <option value="D2">Department 2</option>
            <option value="D3">Department 3</option>
            <option value="D4">Department 4</option>
            <option value="D5">Department 5</option>
            <option value="D6">Department 6</option>
        </select> <br>
    <input type="submit" value="Upload" />
    </form>
    <div id="message_post"></div>

我的php脚本如下:     

ini_set('display_errors', 'On');
error_reporting(E_ALL);
// print_r($_FILES);

$return_json = '';
$filename = $_FILES['uploaded']['name'];
$filetmploc = $_FILES['uploaded']['tmp_name']; 
$uploaded_type = $_FILES["uploaded"]["type"];

$uploaded_size = $_FILES['uploaded']['size'];
$target = $_POST['department'];
$pathandname = $target . "/" . $filename;

function make_dir($target){
if (!is_file($target) && !is_dir($target)) {  
    mkdir($target, 0777);  
    $return_json = json_encode('The directory ' . $target . ' was successfully created.');  
} else {  
    $return_json = json_encode('The directory ' . $target . ' exists');
}
return $return_json;
}


function uploadfile($tar, $temploc, $pathandname, $filename){
make_dir($tar);
//If everything is ok we try to upload it
if(move_uploaded_file($temploc, $pathandname)) { 
    $return_json = json_encode('The file '. $filename . ' has been uploaded'); 
} else { 
    $return_json = json_encode('Sorry, there was a problem uploading your file.'); 
}
return $return_json;
}

if (!($uploaded_size > 0)) {
$return_json = json_encode('Select a file to upload.');
} else {
if ($uploaded_size > 10485760) {
    $return_json = json_encode('Your file is to big. The limit is 10 MB.');
} else {
    switch($uploaded_type) {
        case "application/application/vnd.ms-excel": //.xls
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.ms-excel.sheet.macroenabled.12": //.xlsm
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.openxmlformats-officedocument.presentationml.presentation": //.ppt
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.openxmlformats-officedocument.presentationml.presentation": //.pptx
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/msword": //.doc
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": //.docx
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": //.xlsx
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.visio": //.vsd
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/pdf": //.pdf
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
       case "application/vnd.oasis.opendocument.presentation": //.odp
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.oasis.opendocument.spreadsheet": //.ods
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/vnd.oasis.opendocument.text": //.odt
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "text/plain": //.txt
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;
        case "application/rtf": //.rtf
            $return_json = uploadfile($target, $filetmploc, $pathandname, $filename);
            break;  
        default: 
            $return_json = json_encode('You tried to upload an invalid filetype. <br>Supported filetypes: .xls, .xlsm, .ppt, .pptx, .doc, .docx, .xlsx, .vsd, .pdf, .odp, .ods, .odt, .txt, and .rtf.');
    }
}
echo $return_json;
}
?>

1 个答案:

答案 0 :(得分:0)

您可以使用this technique在后台进行文件上传。使用apc。