Sending $_POST variables

imp4ct's picture

Hi,

I've been searching the forum for a GOOD example how to send extra post variables to the "upload" script, the upload script uploads more then 1 file.

Anyhow, I'll try to give an example of what I mean. Say, you would like to create a photo album. So you would need to add an input field to add the album name, also you want to send the userid of the person who's creating the album also with a hidden input field.

Is it maybe possible to create a working example of that ? I think this will be very useful for a lot of users, since it's really hard to get a solution from the documentation.

Thanks in advance!

imp4ct's picture

I already read that one.. it

I already read that one.. it doesn't work...

gyphie's picture

Summaries Example

Here is a summarized example. Meaning that I didn't include every line of code just the "important" bits that are pertinent to what you are asking. This shows how easy it is to send extra data with a POST.

Note: You have to use the Flash 9 version (swfupload_f9.swf)
PSS: You can't just copy and paste this, it _will not work_. But understanding what this pseudo-code is doing will help you implement the concept in your own site.

index.php

<?php
$album_id = 100;

?>
<html>
<head>
<script>
var swfu;
window.onload = function () {
swfu = new SWFUpload({
post_params: {
album_id : "<?php echo $album_id; ?>" // Send this with all uploads
},
upload_start_handler : uploadStart
upload_success_handler: uploadSuccess
};
};

function uploadSuccess(file, server_data) {
// If there was an error show the message
if (server_data.substring(0, 4) === "Error") {
alert(server_data);
}
}

function uploadStart(file) {
// Add the album name to send specifically with this upload.
this.addFileParam(file.id, "album_name", document.getElementById("album_name").value);
return true;
}
</script>
</head>
<body>
<form>
<input type="hidden" id="album_id" value="<?php echo $album_id; ?>" />
<input type="text" id="album_name" value="" />
<button type="button" onclick="swfu.startUpload()">Upload Files</button>
</form>
</body>
</html>

upload.php

<?php
if (isset($_POST["album_id"]) && $_POST["album_id"] != "") {
$album_id = $_POST["album_id"];
} else {
echo "Error: No album id";
exit();
}

if (isset($_POST["album_name"]) && $_POST["album_name"] != "") {
$album_name = $_POST["album_name"];
} else {
echo "Error: No album name";
exit();
}

if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], "/var/uploads/".$_FILES[$upload_name]["name"])) {
echo "Error: Error saving file";
exit();
} else {
echo "Success: File uploaded: $album_id $album_name";
exit();
}

?>

imp4ct's picture

When I use your example, it

When I use your example, it does work. But only for the first file. When I select e.g. 3 files, he only uploads the first file no problem, but he won't continue to the next one.

When I change the flash version to 8 he uploads perfectly again, but of course he won't send the POST vars... any idea where the problem might be ?

gyphie's picture

More info

Without more specific info on how you have your page setup it is almost impossible to help you.

The way SWFUpload works when you call startUpload it will begin uploading the first file in the queue if a file is not already uploading. Using the Flash 8 or Flash 9 version should not make a difference (but maybe there's a bug).

imp4ct's picture

Ok, I'll show you what my

Ok, I'll show you what my problem is. I hope you have a bit of time to test it.

Surf to this URL : http://www.imagetime.nl/login/
The username and password I've send you with a private message.

After you've logged in, click on the button on the left "upload pictures".
In this screen you'll notice the upload tool. or surf to this url
URL : http://www.imagetime.nl/account/picture/add/

Just select a few files and then see what happens. The upload goes trough, when I check the database and my FTP, everything is executed, the file is uploaded, data is stored in the database, but he won't continue to the next file, that's the problem here.

I hope you can help me, it really would be a life saver.

Thanks in advance!

gyphie's picture

Server Data

I ran some uploads and uploadSuccess is never called. I did a packet sniff and your web server is returning a 200 status but is not return any data (or not enough data to convince flash to call uploadSuccess).

Your upload script must return some data in order for Flash to detect that the file has finished uploading.

imp4ct's picture

That did the trick! Really

That did the trick!
Really THANKS A LOT !!!

imp4ct's picture

For the people who want to

For the people who want to see a full working example.

You can find it at this url
URL : http://www.dev153.net/swfupload/

The files you can download here
URL : http://www.dev153.net/swfupload/SWF_Upload_POST.zip

I've put some comments, but just upload it on your hosting account, change the uploading paths and it should work.
The most important files are :

add_picture.php
add_picture_exe.php
handlers.js

I'll leave the example online for a week, don't upload to much photos please Laughing out loud :D

thanks

thanks imp4ct

doesn't upload all files

I'm using the version that was posted above, and it's not uploading all the files that I select. When I select 9 files, it will upload anywhere from 2-4 files. Its been changing, and with the exception of one file the rest of them differ each time. Is there a setting or something I'm missing here? I kept everything the same exept for the paths.

Thanks,
Jason