This forum is a community forum meant for users of the plugin to collaborate and help solve issues with implementation, etc. Unfortunately, as the creator of the plugin, I do not have much time to attend to every request here as this is only a side project and I must work a full-time job to provide for my family. This is how I keep the Flash version free and the HTML5 version low cost.
UploadiFive 1.1.1 has been released which includes a small fix for added support on touch devices including iOS 6 devices.
For all those who struggle on inserting filenames into a DB after uploading
  • Hi
    I read many questions, but no real solution here about inserting the file name into a database after having the file uploaded.
    Many people say to insert a sql-query into the upload.php script, but I think this isn't a good idea as you maybe don't have an ID for your dataset before a form has been submitted and you need to worry about php issues and error treatment to not insert a record into the db whenever the upload failed.
    The better way for me is to upload the file and give the filename back to the form before it has been submitted.
    Like this you can create or even update consistent datasets.
    In the uploadify documentation there is somewhere an example, which has an error as getElementById has a typo and it reads getElementByID.
    I found a good solution on a coldfusion site (http://www.john-sieber.com) which lead me to the right track for non Cf sites.
    He created an example, which inserts a hidden field into the initial form after the upload is done.
    I think it is the best way as you submit your form only once but with all data.
    There are some things different from his example, I had to change to make it work for a normal php/mysql setup .
    I actually integrated it into a cakephp project, but cake doesn't matter here much.

    1. The upload.php (uploadify.php) needs to return at last a echo 'ok'; to trigger the "onComplete" function. Without a return from the php it didn't fire any events for me.

    2. John Sieber used a onSelect event, which didn't work for me. On 'stackoverflow.com' they somewhere also said not wrap function names into quotes, but I had to do this .

    3. The field names like data[Banner][image] in my code are for cakephp; just use a normal string, if you need to

    4. My database table has a field images in a table banners which holds the Image name and therefore my input field in the form has to be named data[Banner][image] and get the id BannerImage : Those are conventions from Cakephp and need to be adopted for your forms.

    5. I didn't post the actual php which treats the form POST-data because I have this in a controller and anyway there is nothing special about it as you just need to treat a form with text fields like this

    Here is the code:
    My "uploadify-custom.js": (Or put it into the php-form into script tags)

    /**
    * Long description for file uploadify-custom.js
    * Copyright 2011, TuxaneSoft,France (http://www.tuxane.com/)
    * Licensed under The MIT License
    * Redistributions of files must retain the above copyright notice.
    * @version 0.5
    * @modifiedby $jw$
    * @lastmodified 16.02.2011
    * @license http://www.opensource.org/licenses/mit-license.php The MIT License
    */

    $(document).ready(function() {
    $('#file_upload').uploadify({
    'uploader' : '/uploadify/uploadify.swf',
    'script' : '/uploadify/uploadify.php',
    'cancelImg' : '/uploadify/cancel.png',
    'folder' : '/uploads/upload_banner_image',
    'auto' : true,
    'multi' : false,
    'fileDesc' : 'Images',
    'onComplete': function(event, queueID, fileObj, response, data){
    var form = document.forms['BannerAddForm'];
    var i = 0;
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "data[Banner][image]";
    el.id = "BannerImage";
    el.value = fileObj.name;
    form.appendChild(el);
    },
    'onAllComplete': function(event,data) { document.getElementById('BannerAddForm').submit();},
    });
    });


    The uploadify.php

    <?php
    if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
    move_uploaded_file($tempFile,$targetFile);
    echo '0'; // Required to trigger onComplete function on Mac OSX (same for linux and windows as it seems)

    }
    else { // Required to trigger onComplete function on Mac OSX (same for linux and windows as it seems)
    echo '1';
    }
    ?>


    The page hold a form field like in the examples

    <input id="file_upload" name="file_upload" type="file" />

    If you don't use auto=true
    put a submit buttom to your form like
    <a href="javascript:$('#file_upload').uploadifyUpload();"><img id="withupload" src="img/submit.gif" /></a>

    But I would consider it as better style to attach a normal HTML submit button to the page and have uploadify submit the form 'onComplete' (with auto=true and the submit() method 'onComplete') as it gives the user a possibillity to modify other form elements and save it without changing the image (or better dataset) of this page

    You could maybe hide the HTML button , if the user clicks on 'select files' and have the form submitted after upload.

    Hope it helps someone
    Die Schiffsratte
  • This for multiple file upload and file name storage to a DB table:


    /**
    * Long description for file uploadify-custom.js
    * Copyright 2011, TuxaneSoft,France (http://www.tuxane.com/)
    * Licensed under The MIT License
    * Redistributions of files must retain the above copyright notice.
    * @version 0.6
    * @modifiedby $jw$
    * @lastmodified 18.02.2011
    * @license http://www.opensource.org/licenses/mit-license.php The MIT License
    */

    $(document).ready(function() {
    var i = 0;
    $('#file_upload').uploadify({
    'uploader' : '/backend/uploadify/uploadify.swf',
    'script' : '/backend/uploadify/uploadify.php',
    'cancelImg' : '/backend/uploadify/cancel.png',
    'folder' : '/uploads',
    'auto' : false,
    'multi' : true,
    'fileDesc' : 'Images',
    'onComplete': function(event, queueID, fileObj, response, data){
    var form = document.forms['ImageAddForm'];
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "data[Image][name]["+i+"]";
    el.id = "ImageFile["+i+"]";
    el.value = fileObj.name;
    form.appendChild(el);
    i++;
    },
    });
    });

    It inserts a hidden input field for each file like this :
    <input id="FormImageFile[1]" type="hidden" name="data[Image][name][1]" value="file1.jpg">

    'auto' needs to be set to false and a upload button additionally to the submit button is needed.
    The filenames should be easily retrieved in the php which processes the form, as you'll have an array of filenames in the example it would be data[Image][name][]

    The counter will be reset to zero on page reload , so you can upload several times before reloading the page and the input fields will be named differently
  • Hi, I'm french and try to put your code in my site but I don't understand how the filenames are store in the DB.

    uploadify.js

    $(document).ready(function() {
    $("#fileUploadstyle").fileUpload({
    'uploader': '/uploadify/uploader.swf',
    'cancelImg': '/uploadify/cancel.png',
    'script': '/photo/enregistrer_ajout.php',
    'folder': '/upload/<?php echo $nomVille?>',
    'buttonImg': '/uploadify/images/browseBtn.png',
    'multi': true,
    'displayData': 'speed',
    'width': 120, //80
    'height': 24,
    'rollover': true,
    'fileDesc': 'Fichier image : JPG (*.jpg), JPEG(*.jpeg)',
    'fileExt': '*.jpg;*.jpeg;',
    'scriptData': {'idVille':'<?php echo $idVille?>'},

    'onComplete': function(event, queueID, fileObj, response, data){
    var form = document.forms['ajout'];
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "fichier";
    el.id = "ImageFile["+i+"]";
    el.value = fileObj.name;
    form.appendChild(el);
    i++;
    }
    })


    ajout.php is the script with the uploadify form

    <div id="fileUploadstyle">Vous avez un problème avec Javascript</div>
    <a href="javascript:$('#fileUploadstyle').fileUploadStart()">Télécharger</a> | <a href="javascript:$('#fileUploadstyle').fileUploadClearQueue()">Vider la liste</a>


    uploadify.php

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

    // Creation du dossier de destination
    mkdir(str_replace('//','/',$targetPath), 0755, true);

    move_uploaded_file($tempFile, $targetFile);

    echo 0;
    } else {
    echo '1';
    }


    What I miss there?

  • The idea isn't that bad, but I don't understand why you shouldn't let the uploadify.php do the stuff of inserting to the DB... that works fine for me (the thing that does not work is that it reads a dropdown boxes' value)
  • I found Christian Louboutin Sale discount here with good price.And I so happy to get Christian Louboutin Slingback looks so fashion.Christian Louboutins hot sale now.Don't miss it.
  • Hi there

    I would like to give my Regards and wishes to MR schiffsratte.I was using multiple upload and was trying hard to figure out how to send images name to the php.

    your post saved my life .

    Thank you so much sir , i really appreciate your help and i would also share my knowledge with the world .

    Thanks again
  • The user and all related content has been deleted.
  • @schiffsratte - glad to hear that my blog post was helpful!
  • Hi - I am having some troubles getting this to work. Here is what I have:


    <script type="text/javascript">

    $(document).ready(function() {
    $('#sub_form').hide();
    $('#file_upload').uploadify({
    'uploader' : '/uploadify/uploadify.swf',
    'script' : '/uploadify/uploadify.php',
    'cancelImg' : '/uploadify/cancel.png',
    'folder' : '/gfx/upload',
    'multi' : true,
    'auto' : false,
    'queueID' : 'custom-queue',
    'queueSizeLimit' : 3,
    'simUploadLimit' : 3,
    'removeCompleted': false,
    'fileExt' : '*.jpg;*.gif;*.png',
    'fileDesc' : 'Image Files (.JPG, .GIF, .PNG)',
    'onSelectOnce' : function(event,data) {
    $('#status-message').text(data.filesSelected + ' files have been added to the queue.');
    },
    'onComplete': function(event, queueID, fileObj, response, data){
    var form = document.forms['form1'];
    //var i = 0;
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "data[Image][name]["+i+"]";
    el.id = "ImageFile["+i+"]";
    el.value = fileObj.name;
    form.appendChild(el);
    i++;
    },
    'onAllComplete' : function(event,data) {
    $('#status-message').text(data.filesUploaded + ' files uploaded, ' + data.errors + ' errors.');
    $('#sub_form').show();
    }
    });
    });
    </script>


    And my uploadify.php:

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

    // $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
    // $fileTypes = str_replace(';','|',$fileTypes);
    // $typesArray = split('\|',$fileTypes);
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $fileExt = '.'.$fileParts['extension'];

    // if (in_array($fileParts['extension'],$typesArray)) {
    // Uncomment the following line if you want to make the directory if it doesn't exist
    // mkdir(str_replace('//','/',$targetPath), 0755, true);
    //below code gives each picture a unique name
    $tfn = 0;
    while(is_file($targetFile)) {
    $targetFile = str_ireplace($fileExt, '', $targetFile);
    $targetFile = str_ireplace('['.$tfn.']', '', $targetFile);
    $tfn++;
    $targetFile = $targetFile.'['.$tfn.']'.$fileExt;
    }
    move_uploaded_file($tempFile,$targetFile);
    echo '0';
    //echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
    } else {
    echo '1';
    }
    ?>

    The images get uploaded, but the hidden fields do not appear to be added to my form (form1). Any thoughts? Thanks!
  • wrong post sorry...
  • Hi! I'm using your script, but i don't know how to recover de data in the hidden inputs. This is what I have:

    $(document).ready(function() {
    var i = 0;
    $('#file_upload').uploadify({
    'uploader' : '/uploadify/uploadify.swf',
    'script' : '/uploadify/uploadify2.php',
    'cancelImg' : '/uploadify/cancel.png',
    'folder' : '/uploads',
    'auto' : false,
    'multi' : true,
    'fileDesc' : 'Images',
    'onComplete': function(event, queueID, fileObj, response, data){
    var form = document.forms['ImageAddForm'];
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "datos"+i;
    el.id = "datos"+i;
    el.value = fileObj.name;
    form.appendChild(el);
    i++;
    },
    });
    });
    </script>
    </head>
    <body>
    <form id="ImageAddForm" name="ImageAddForm">
    <input id="file_upload" name="file_upload" type="file" />
    <a href="javascript:$('#file_upload').uploadifyUpload();">Up</a>
    </form>


    And my uploadify.php

    ...
    $id_usuario = $_REQUEST['datos0']; //For example
    if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
    move_uploaded_file($tempFile,$targetFile);
    echo '0';
    $resultado = $conexion->modifica("INSERT INTO foto (id_user, url_foto) VALUES('$id_usuario','$newFileName')", $nombre_bd);
    echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
    } else {
    echo '1';
    }


    What am I doing wrong?
  • Wouldn't it be easier to use the scripData value-pairs in combination with the uploadifySettings() function to dynamically pass Data via POST to the uploader.php?

    Greetz

    M.
  • Hello back everybody,
    it's been a long time since I posted this, so I guess people have found their solutions meanwhile.
    Just to answer the question why not storing the file-names into the db from the uploadify.php:
    First of all , if that works for you, don't worry.
    But what I wanted to do was to have a form for multiple datasets like id , name, category, imagename, imageid etc. In my database the data had been split over two tables :
    One holded all info about images, the other one holded in that case info about banners, clients, clicks and just the ID of the corresponding image.
    So I needed a form which would send the data to the 'banners' table and which holds already the filename and it's id from the images table .
    In this case you have to make sure that the upload and renaming of the image has finished without errors and which id the image will have in the images table, or to be sure to send valid data and use the autoincrement which let's you know the ID before.
    Here it was important to verify that uploadify had given back the ID or name of the uploaded file back to the form as I did not want to send of invalid data, or let the user upload images, store that info in the DB but in the end he would maybe not send the form and I would have ended up with images in the DB without a corresponding dataset in the other table.

    That's why I had choosen to let uploadify upload the image, check if it had been uploaded, rename it and give everything back to the form before sending off any forms.
    That's why I had it added into the form before submitting it.

    But this maybe not needed, if you only need to upload an image and store it's name into the DB. In that case it can be done from the upload.php, even if I think that it's harder to track any upload or php/sql errors when you don't have a feedback and it all happens behind the scenes.

    @jsieber: Yes it was very helpful as I also had no clue before I had read you post.
    That was the first time I started to think about not doing the SQL part in the uloadify.php but in the original form and add input fields to it after the upload happened.
    So all the credits for this go to you ;)

    SR
  • Thanks a lot Dude...!!!
    It really helped me....

    I thought the same logic but wasn't aware of the JS programming that much..
    anyways
    Thanks again...:)