简体   繁体   中英

jQuery File Upload not Uploading

This PHP code works when I use HTML <input type="file"> , but not when I use the jQuery file upload script at the very bottom.

  1. I believe all .js & .css files are accounted for and working (tested)
  2. I believe the uploadUrl: 'doc_upload.php', fileInputName: 'userfile' are correct. Don't know 'cause no errors are showing.
  3. Using echo getcwd(); , I believe I'm using correct paths
  4. I'm only uploading a 1 kb .TXT file
  5. I get no errors and all error reporting is on and maxed

One thing that is also not working, which I'm saying to possibly give a clue, is the upload & cancel buttons next to the file name are not showing all though clicking on them does seem to work.

Echo'ing $_POST shows me nothing on submit, but I think that's a javascript thing; not sure as I'm a beginner (3-months).

PHP page

    $filename = $_FILES['userfile']['name'];
    $tmpname  = $_FILES['userfile']['tmpname'];
    $filesize = $_FILES['userfile']['size'];
    $filetype = $_FILES['userfile']['type'];

    $fp = fopen($tmpname, 'r');
    $content = fread($fp, filesize($tmpname));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()) {

        $filename = addslashes($filename);
    }

    $filename = preg_replace('/[ ]/', '~', $filename);

    $document_folder = $_SESSION['users_id'];
    $destfile = "../_documents/".$document_folder."/".$_FILES['userfile']['name'];
    move_uploaded_file( $_FILES['userfile']['tmpname'], $destfile );

HTML page

<link type="text/css" rel="Stylesheet" href="../jqx.base.css" />

<script type="text/javascript" src="../scripts/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxfileupload.js"></script>

<script type="text/javascript">


        $(document).ready(function () {
      $('#jqxFileUpload').jqxFileUpload({ width: 300, uploadUrl: 'doc_upload_jquery.php', fileInputName: 'userfile', autoUpload: true });
        });



    $('#jqxFileUpload').on('uploadStart', function (event) {
            var fileName = event.args.file;
            $('#log').prepend('Started uploading: <strong>' + fileName + '</strong><br />');
    });



    $('#jqxFileUpload').on('remove', function (event) {
            var fileName = event.args.file;
        $('#log').prepend('Removed file: <strong>' + fileName + '</strong><br />');

    });



    $('#jqxFileUpload').on('uploadEnd', function (event) {
        var args = event.args;
        var fileName = args.file;
        var serverResponce = args.response;
    });



</script>


</head>
<body>
<div id="jqxFileUpload">
</div>
<div id="log" style="margin-top: 20px;"></div>
<br />
</body>
</html>

mdpalow -

You probably aren't getting any errors or output because of the if statement at the top:

if (isset($_POST['submit']) && $_FILES['userfile']) {

Try putting this at the top to output all of your post variables so that you can determine what post variables are actually being sent (if any):

print_r($_POST);

I have done file uploads before but not with this specific library - my guess is that its just sending mulipart data instead of POST variables. Looking at the preview headers in the browser developer tools will also shed light as to what data is being sent.

The problem is that jQuery plugin uploads file via AJAX. In order to fix the issue please follow next steps:

  • Keep your HTML and PHP code in separate files.
  • In new PHP file remove isset($_POST['submit']) validation as there is no submission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM