0

ファイルを Web サーバーにアップロードし、アップロードするファイルを選択するために使用するフォームの下に出力メッセージを表示するスクリプトを作成するよう依頼されました。出力メッセージの表示に問題があります。関連する 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;
}
?>
4

1 に答える 1

0

この手法を使用して、バックグラウンドでファイルのアップロードを行うことができます。apc を使用します。

于 2012-08-13T14:20:41.923 に答える