3

I'm using Uploadify as part of a form. Let me give you a bit of background, it may help. I have a form where a user can add "projects" to a website. First they type in the name of the project and a description. On submit, this updates a PHP/MySQL database table named "project" and is given an ID.

The user can then upload files to a location on the server. I wish to add the project name onto the start of the file name for upload AND the project ID (which I need to add to the database) before upload begins, then when upload completes add the file details to a database table "image" - linked to "project" via the project ID.

I know I'm kinda bouncing back and forth a lot, I need to know how to do this. Two database tables to update, one on form submit and one on file-upload. I need to pass the project name and ID to the uploadify upload script.

SOLUTION:

I had to use the below uploadify method to send the Project ID to the uploadify script, having previously filled variable pid with the mysql_insert_id result:

'onSelectOnce': function(event,data) {
     $('#file_upload').uploadifySettings('scriptData', {'pid': pid});
 }

I could then receive the pid variable in the PHP uploadify script using a simple post:

$pid = $_POST['pid'];

It was then a matter of running a select within this script to get the data I needed for the database (the project alias) and adding it to the filename before upload:

    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

Hopefully this will help people in the future.

3 Answers 3

3

I had to use the below uploadify method to send the Project ID to the uploadify script, having previously filled variable pid with the mysql_insert_id result:

'onSelectOnce': function(event,data) {
     $('#file_upload').uploadifySettings('scriptData', {'pid': pid});
 }

I could then receive the pid variable in the PHP uploadify script using a simple post:

$pid = $_POST['pid'];

It was then a matter of running a select within this script to get the data I needed for the database (the project alias) and adding it to the filename before upload:

$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

Hopefully this will help people in the future.

Sign up to request clarification or add additional context in comments.

2 Comments

I use this plugin along with a php uploaded class. Search in google for upload.class.php. I se the PHP to handle file names and uploading my file to my server. I can then send the new name or I can rename on the fly with the script so that the JS picks it up and outputs anything i want with the call back functions
@ravz then you must have something else going on in your code. I posted this as a solution to my own question because this is the solution I used to get it to work. This is almost a year old and I've used it for many projects, so I know from experience that it works. Perhaps ask a new question and post some of your code so we can help you directly.
1

In the uploadify script there is part that gives the syntax for the file being handled by the upload form. I don't have the script on hand, but uplodify hs a onbefore complete callback and an on complete call back features.

use the before complete and append the name to an ajax request that will save it to your database, from there just perform 2 queries, upload the name of the image and set user_id to the ID of the user thats probably from ur session.

   var = file_before_upload_name: filename // here use the sytax that Uploadify uses to capture the name of the file
   var = file_after_upload_name: filename // here use the sytax that Uploadify uses to capture the name of the file

then on the aftercomplete callback use an ajax request and set

uid : uid //from a session before: file_before_upload_name, after : file_after_upload_name

in the ajax your queries would look like

mysql_queries("INSERT INTO `tbl-projects` SET `user_id` = {$_POST['uid']}, `file` = {$_POST['after']}");

//another query here to set the data to your other table that relates to tbl-projects

3 Comments

Don't ever insert $_POST fields in a query like that. You've just created a SQL-injection bug. Better use mysql_real_escape_string on all inputs, or use an existing database API that "binds" the values to the SQL string.
it was an example of how to go about it, i'm sure he knows he should sanitize his data before updating to a database =P
it's okay, I wasn't going to leave my website open to attack, it's the principle of the code setup I needed, the individual syntax is fine :)
1

Try this http://programmintalk.blogspot.com/2011/02/jquery-uploadify-rename-uploaded-file.html

2 Comments

the way your passing the image name uploadify.php?=imagename won't work. If your passing any variables you need to use the scriptData method in my answer above. Other than that, our methods are the same
and it's not "uploadify.php?=imagename" it should be "uploadify.php?image=imagename". try my example, it worked for me, and it's simple

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.