Search code examples
phpuploadify

uploadify - rename uploaded file


Hy , i was looking at uploadify.php and did'n understand a thing.

I have a form like this :

<form id="formid" name="upload_pic" action="upload.php">
<select name="product_id">
<option value="1">Apples</option>
<option value="2">Oranges</option>
... etc
</select>
<input id="file_upload" name="file_upload" />
</form>

and my uploadify settings are :

<script type="text/javascript">
$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : 'uploadify/uploadify.swf',
    'script'    : 'uploadify/uploadify.php',
    'cancelImg' : 'uploadify/cancel.png',
    'folder'    : '../images/level3/tabv_all/tab_header/',
    'auto'      : false,
    'multi'     : true,
    'fileExt'     : '*.jpg',
    'fileDesc'    : 'ONLY JPG (.JPG)',
    'removeCompleted' : false
  });
});
</script>

What i want to do is that if the user select Apples wich has the id=1 and browse for a file like Tasty_apples.jpg -> the uploaded file to be renames to product@1@Tasty_apples.jpg and then to be inserted in mysql like that?

The main question is how to add the extra product@id@ to a file based on a <select><option> value ?

Thank you very much

The uploadify.php is this :

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 str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}

Solution

  • You can send additional data to your backend script with scriptData option:

    http://www.uploadify.com/documentation/options/scriptdata/

    Example

    var selectedID = $("select[name=product_id]").val()
    
    'scriptData'  : {'pid': selectedID}
    
    // uplodify.php
    $targetFile =  str_replace('//','/',$targetPath) . 'product@' . $_POST['pid'] . '@' . $_FILES['Filedata']['name'];