Question

I am working on a way for the user to select which folder they want to upload their files to with Uploadify. I have a bunch of javascript code, but it all works except for the last bit. I can show it all if you want but I firmly believe that isn't the problem.

Here's my code:

$("#file_upload").uploadify({
        'buttonText'    : 'Upload New Files',
        'height'        : 30,
        'swf'           : 'uploadify/uploadify.swf',
        'uploader'      : 'uploadify/uploadify.php',
        'width'         : 120,
        'folder'        : $('#folder').val(),
        'auto'          : true,
        'multi'         : true,
        'onUploadComplete' : function(file) {
            location.reload();
        }
    });

    $("#folder").change(function () {
        var path = "/" + $(this).val();
        $('#file_upload').uploadifySettings("scriptData", {'folder': path });
        alert(path);
    });

This code is wrapped in a document.ready().

What this code does is

  1. Initialize the uploadify function
  2. Have the value "folder" in the uploadify function to change when the user changes the value of #folder (which is the drop-down).

When I click the select box, nothing happens. If I comment out the line:

$('#file_upload').uploadifySettings("scriptData", {'folder': path });

I get the alert, bur if I leave the line in it doesn't do anything.

I consulted Firebug and saw I was getting the following error:

TypeError: $(...).uploadifySettings Not A Function

Logically, I would say this means I don't have uploadify isn't linked onto the page. But if I upload the file, it uploads, so it's there and works.

Here is my HTML/PHP:

<h3>Your Uploaded Files</h3>
    <input type="file" name="file_upload" id="file_upload">
    <span style="margin: 5px 10px 0 0; float: left;">to</span>
    <select id="folder">
        <?php
        echo '<option value="'.$directory.'">Downloads</option>';
        $friendlyDir;
        foreach(glob('downloads/*', GLOB_ONLYDIR) as $dir) {
          $friendlyDir = str_replace("downloads/","",$dir);
          echo '<option value="'.$dir.'">'.ucfirst(strtolower($friendlyDir)).'</option>';
        }
        ?>
    </select>
    <div id="file_viewer"></div>

The HTML/PHP works fine but I thought I would include it just so you could see what the triggers and such are.

If somebody could look into this, I would be thankful. I have not cross-browser tested it but the browser I'm using is Firefox 19.0.2.

Thank you.

Was it helpful?

Solution

I ended up doing this to file with the uploadify function on it:

var path;
    $("#file_upload").uploadify({
        'buttonText'    : 'Upload New Files',
        'height'        : 30,
        'swf'           : 'uploadify/uploadify.swf',
        'uploader'      : 'uploadify/uploadify.php',
        'width'         : 120,
        'folder'        : $('#folder').val(),
        'auto'          : true,
        'multi'         : true,
        'folder'        : path,
        'onUploadStart' : function(file) {
           $('#file_upload').uploadify("settings", 'formData', {'folder' : path});
        },
        'onUploadSuccess' : function(file, data, response) {
            //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
            location.reload();
        }
    });

    $("#folder").change(function () {
        path = "/" + $(this).val();
    });

And on uploadify.php I changed it to this:

//$targetFolder = '/file/downloads'; // Relative to the root
$targetFolder = "/file".$_POST['folder'];

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','pdf','doc','docx'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo $targetFolder;
} else {
    echo 'Invalid file type.';
}
}

And now it works :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top