简体   繁体   中英

php - file uploads using uploadify

I'm having problems with uploadify. Whenever I use a string in the $post_id, uploadify only uploads a single file when I've selected 3 files for upload. But when I specify a non-existing value for $post_id such as a session variable that doesn't exist $_SESSION['something']. It inserts all three of the files into the database. I'm thinking that this might be an error on the data structure of $post_id.

 if(!empty($_FILES)){
            $post_id = 'aa';
            $name2 = mysql_real_escape_string($_FILES['Filedata']['name']);
            $mime2 = mysql_real_escape_string($_FILES['Filedata']['type']);
            $data2 = mysql_real_escape_string(file_get_contents($_FILES['Filedata']['tmp_name']));
            $size2 = intval($_FILES['Filedata']['size']);



            $db->query("INSERT INTO tbl_files SET post_id='$post_id', filename='$name2', file_data='$data2', mime_type_id='$mime2'");



    }

I tried to echo the rest of the data and it seems like they're only storing plain strings. So $post_id string should also work,

echo $_FILES['Filedata']['name'];

If you are using sessions, please check that you have started session ie session_start()

If you want to pass the post_id from uploadify function to php file, you can use the scriptData, which is mentioned in the below function.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
    //uploadify function
    $("#file_upload").uploadify({
        'uploader': 'uploadify.swf',
        'script': 'uploadify.php',
        'cancelImg': 'cancel.png',
        'folder': 'photos', //folder where images to be uploaded
        'auto': false, // use for auto upload
        'multi': true,
        'queueSizeLimit': 6,
        'buttonImg': 'images/upload.png',
        'width': '106',
        'height': '33',
        'wmode': 'transparent',
        'method': 'POST',
        'scriptData': {'myid':post_id}, //you can post the id here
        'onQueueFull': function(event, queueSizeLimit) {
            alert("Please don't put anymore files in me! You can upload " + queueSizeLimit + " files at once");
            return false;
        },
        'onComplete': function(event, ID, fileObj, response, data) {
            $("#uploadfiles").append(response+",");
        },
        'onAllComplete': function(response, data) {
            showAll();
        }
    });
</script>

uploadify.php

if (!empty($_FILES)) {
    $post_id = $_POST['myid'];
    include_once "config.php";

    //use this when uploading images into a folder
    $tempFile = $_FILES['Filedata']['tmp_name'];

    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';

    $fna = $_FILES['Filedata']['name'];
    $targetFile =  str_replace('//','/',$targetPath) . $fna;
    move_uploaded_file($tempFile,$targetFile);
    //folder upload end

           $name2 = mysql_real_escape_string($_FILES['Filedata']['name']);
            $mime2 = mysql_real_escape_string($_FILES['Filedata']['type']);
            $data2 = mysql_real_escape_string(file_get_contents($_FILES['Filedata']['tmp_name']));
            $size2 = intval($_FILES['Filedata']['size']);



        $db->query("INSERT INTO tbl_files SET post_id='$post_id', filename='$name2', file_data='$data2', mime_type_id='$mime2'");

}

Try this it should work as you expected

 $tblQry = 'INSERT INTO tbl_files ';
 $tblQry .= 'SET         
        post_id                 = "' .$_SESSION['post_id'] . '",
        filename                = "' .$name2. '",
        file_data               = "' .$data2.'",
        mime_type_id            = "' .$mime2.'"';
  $db->query($tblQry);

Just encountered this problem. Flash doesn't use the cookies that are set in your browser so you need to pass your session id into the flash script as a post variable and then set the cookie value from the post value in your upload.php script. See:

jQuery and Uploadify session in the php file

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