2

I have an uploadify script running, basic setup. Works fine when I hard code the destination folder for the images into uploadify.php - now I want to make that folder dynamic. How do I do this?

I have a PHP variable $uploadify_path which contains the path to the folder I want. I have switched out my hard coded $targetPath = path/to/directory for $targetPath = $uploadify_path in both uploadify.php and check_exists.php, but it does not work. The file upload animation runs, says it is complete, yet the directory remains empty. The file is not hiding out somewhere else either.

I see there is an option in the Javascript to specify a folder. I tried this also, but to no avail.

If anyone could educate me on how to pass this variable destination to uploadify, I'd be very grateful.

I include my current code for checking (basically default):

The Javascript

<script type="text/javascript">
$(function() {

    $('#file_upload').uploadify({
        'swf'      : 'uploadify/uploadify.swf',
        'uploader' : 'uploadify/uploadify.php',
        // Put your options here
    });
});
</script>

uploadify.php

$targetPath = $_SERVER['DOCUMENT_ROOT'] . $uploadify_path; // Relative to the root

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetFile = $targetPath . $_FILES['Filedata']['name'];

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

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
2
  • my guess is that you don't have the proper permissions set on just any random folder and it works when you hard code it because you have the proper permissions set on that particular folder. Commented Jul 10, 2012 at 3:13
  • I actually create the folders earlier on in the page. All set to 755. I did manually change them to 777 just to test, but no joy... Commented Jul 10, 2012 at 3:14

1 Answer 1

4

JS

<script type="text/javascript">
    $(function() {

        $('#file_upload').uploadify({
            'formData' : {'path':'/file/path'},
            'swf'      : 'uploadify/uploadify.swf',
            'uploader' : 'uploadify/uploadify.php'
            // Put your options here
        });
    });
</script>

PHP

$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_POST['path']; // Relative to the root

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetFile = $targetPath . $_FILES['Filedata']['name'];

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

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.