Link to home
Start Free TrialLog in
Avatar of leronj23
leronj23

asked on

Upload multiple Images from Flash to PHP

I need Help Fast. The Problem that im having is that i want people to upload 4 images in Flash 8 at the same time and those image's "PATH" be sent to PHP and place into "PictureA,  PictureB, PictureC, PictureD". The Flash upload works fine for 4 images. The PHP works fine for one image. The image path shows up in sql and the image is placed into my server.

$b = the name of the path that will be save to sql, but i need it to be "PictureA,  PictureB, PictureC, PictureD".

Please Help 500 points and a Grade A

-----------------Flash 8 Actionscript---------------

//import the FileReference Object
import flash.net.FileReference;

//initial settings
upload_butn.enabled = false;

//the fileReference object
var file_fa:FileReference = new FileReference();
var file_fb:FileReference = new FileReference();
var file_fc:FileReference = new FileReference();
var file_fd:FileReference = new FileReference();

//object for listening to for FileReference events
var list_obj1:Object = new Object();
var list_obj2:Object = new Object();
var list_obj3:Object = new Object();
var list_obj4:Object = new Object();

list_obj1.onSelect = function(){
     upload_butn.enabled = true;
     nameA_txt.text = file_fa.name;}
list_obj2.onSelect = function(){
     upload_butn.enabled = true;    
     nameB_txt.text = file_fb.name;}
list_obj3.onSelect = function(){
     upload_butn.enabled = true;
     nameC_txt.text = file_fc.name;}
list_obj4.onSelect = function(){
     upload_butn.enabled = true;
     nameD_txt.text = file_fd.name;}

list_obj1.onComplete = function(){
     nameA_txt.text = "All Done";
          rec_mc.clear();
     upload_butn.enabled = false;}
list_obj2.onComplete = function(){
     nameB_txt.text = "All Done";
          rec_mc.clear();
     upload_butn.enabled = false;}
list_obj3.onComplete = function(){
     nameC_txt.text = "All Done";
          rec_mc.clear();
     upload_butn.enabled = false;}
list_obj4.onComplete = function(){
     nameD_txt.text = "All Done";
     rec_mc.clear();
     upload_butn.enabled = false;}

list_obj1.onProgress = function (bytesTotal, bytesLoaded){
     var percent = bytesLoaded/file_fa.size;
     var percent = bytesLoaded/file_fb.size;
     var percent = bytesLoaded/file_fc.size;
     var percent = bytesLoaded/file_fd.size;
     drawRec(percent);
}
//if a user selects cancel
list_obj1.onCancel = function(){
     nameA_txt.text = "Cancel was selected";}
list_obj2.onCancel = function(){
     nameB_txt.text = "Cancel was selected";}
list_obj3.onCancel = function(){
     nameC_txt.text = "Cancel was selected";}
list_obj4.onCancel = function(){
     nameD_txt.text = "Cancel was selected";}

//if there is an IO error
list_obj1.onIOError = function(fileRef){
     nameA_txt.text = "IO error with " + fileRef.name;}
list_obj2.onIOError = function(fileRef){
     nameB_txt.text = "IO error with " + fileRef.name;}
list_obj3.onIOError = function(fileRef){
     nameC_txt.text = "IO error with " + fileRef.name;}
list_obj4.onIOError = function(fileRef){
     nameD_txt.text = "IO error with " + fileRef.name;}

//security error problem
list_obj1.onSecurityError = function(fileRef, error){
     nameA_txt.text = "Security error with " + fileRef.name + ":" + error;}
list_obj2.onSecurityError = function(fileRef, error){
     nameB_txt.text = "Security error with " + fileRef.name + ":" + error;}
list_obj3.onSecurityError = function(fileRef, error){
     nameC_txt.text = "Security error with " + fileRef.name + ":" + error;}
list_obj4.onSecurityError = function(fileRef, error){
     nameD_txt.text = "Security error with " + fileRef.name + ":" + error;}

//httpError
list_obj1.onHTTPError = function(fileRef:FileReference, error:Number){
     nameA_txt.text += "HTTP error: with " + fileRef.name + ":error #" + error;}
list_obj2.onHTTPError = function(fileRef:FileReference, error:Number){
     nameB_txt.text += "HTTP error: with " + fileRef.name + ":error #" + error;}
list_obj3.onHTTPError = function(fileRef:FileReference, error:Number){
     nameC_txt.text += "HTTP error: with " + fileRef.name + ":error #" + error;}
list_obj4.onHTTPError = function(fileRef:FileReference, error:Number){
     nameD_txt.text += "HTTP error: with " + fileRef.name + ":error #" + error;}


//attach the listener
file_fa.addListener(list_obj1);
file_fb.addListener(list_obj2);
file_fc.addListener(list_obj3);
file_fd.addListener(list_obj4);

//the event for the browse button
browseA_butn.clickHandler = function(){
     file_fa.browse([{description: "JPEGs", extension: "*.JPG;*.jpg"}]);}
browseB_butn.clickHandler = function(){
     file_fb.browse([{description: "JPEGs", extension: "*.JPG;*.jpg"}]);}
browseC_butn.clickHandler = function(){
     file_fc.browse([{description: "JPEGs", extension: "*.JPG;*.jpg"}]);}
browseD_butn.clickHandler = function(){
     file_fd.browse([{description: "JPEGs", extension: "*.JPG;*.jpg"}]);     }

//the event for the upload button
upload_butn.clickHandler = function(){
     file_fa.upload("upload.php");
     file_fb.upload("upload.php");
     file_fc.upload("upload.php");
     file_fd.upload("upload.php");
     }

-------------------PHP that i Use-------------------------------------------

<?php

$a= "/files/";
$b= $a.$_FILES['Filedata']['name'];

copyResizeImage($_FILES['Filedata']['tmp_name'],$b);

$d= chmod($b,0777);
function copyResizeImage($oldName, $newName, $maxWidth = 365, $maxHeight = 365)
{
    if (!is_file($oldName)) return false; /* No input */
    if (is_file($newName)) return false; /* Output exists */
    if ($maxWidth <= 0 || $maxHeight <= 0) return false; /* invalid size */
    list($oldWidth, $oldHeight, $type, $attr) = getimagesize($oldName);
    if ($type > 3) return false; /* unsupported image type */
    /* Resize if necessary */
    $newWidth = $oldWidth;
    $newHeight = $oldHeight;
    if ($newHeight <> $maxHeight)
    {
        $newWidth = ($newWidth / ($newHeight / $maxHeight));
        $newHeight = $maxHeight;
    }
    if ($newWidth <> $maxWidth)
    {
        $newHeight = ($newHeight / ($newWidth / $maxWidth));
        $newWidth = $maxWidth;
    }
    /* Increase memory limit to support larger files */
    ini_set('memory_limit', '32M');
    /* Read source file */
    if ($type == 1) $src = ImageCreateFromGif($oldName);
    if ($type == 2) $src = ImageCreateFromJpeg($oldName);
    if ($type == 3) $src = ImageCreateFromPng($oldName);
    /* Convert to JPEG */        
    $dst = ImageCreateTrueColor($newWidth, $newHeight);
    ImageCopyResized($dst, $src, 0, 0, 0, 0,
        $newWidth, $newHeight,
        $oldWidth, $oldHeight);
    /* Write destination file */        
    return(imagejpeg($dst, $newName));
}

DEFINE ('DB_USER', '********');
DEFINE ('DB_PASSWORD', '********');
DEFINE ('DB_HOST', 'mysql');
DEFINE ('DB_NAME', 'temp');
// Make the connnection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

$sql = "INSERT INTO test

(date,topic,companyname,address,city,states,zipcode,contact,phone,fax,email,price,messages,pictureA,pictureB,pictureC,pictureD) VALUES(NOW(),'$topic','$companyname','$address','$city','$states','$zipcode','$contact','$phone','$fax','$email','$price','$messages','$pictureA','$pictureB','$pictureC','$pictureD')";

$result = @mysql_query($sql);

?>

 
 i need to be able to make the uploaded images separate to place them in pictureA, pictureB, pictureC, pictureD.
 
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

You should NOT access the uploaded files directly.

Instead, move the uploaded files ...

if (is_uploaded_file($_FILES['Filedata']['tmp_name']) && move_uploaded_file($_FILES['Filedata']['tmp_name'], '/uploaded/file_destination/' . basename($_FILES['Filedata']['name']))
 {
 // You can now use the file that exists in '/uploaded/file_destination/' . basename($_FILES['Filedata']['name']) as the REAL file.
 }

Avatar of leronj23
leronj23

ASKER

I understand what your are saying, but i need the image path to be place in "pictureA,pictureB,pictureC,pictureD" so it can be downloaded to my sql database.
Ah. You are uploading 4 images but using 1 connection per image rather than multiple images in 1 connection.

So, you will need to add a POST variable indicating which image it is.

So ...

<?php
if (isset($_POST['image_name']) && is_uploaded(...) and move_uploaded_file(..., '/final/destination/for/uploaded/image/' . $_POST['image_name']))
 {
 // Image is now in the right place.
 }
?>
Or set the name of the image in the ...

If this was HTML then ...

<input type="file" name="PictureA">
<input type="file" name="PictureB">
<input type="file" name="PictureC">
<input type="file" name="PictureD">


and ...

<?php
foreach($_FILES as $s_Picture_x => $a_Uploaded_file)
 {
 if (is_uploaded($a_Uploaded_file['tmp_name']) && move_uploaded_file($a_Uploaded_file['tmp_name'], '/final/' . $s_Picture_x))
  {
  // Picture_x is now set based upon the name of the image you used in the <input> tag (or your Flash equivalent).
  }
 }
here is what going on. in flash i have 4 seperate upload fields. when you hit the upload botton this is how there are sent

file_fa.upload("upload.php"); // pictureA
file_fb.upload("upload.php"); // pictureB
file_fc.upload("upload.php"); // pictureC
file_fd.upload("upload.php"); // pictureD

Now i need "File_fa" to = "pictureA" in my "upload.php" and so on.

Do you think i need 4 seperate PHPs to do this?
I don't know flash, but can you show me the properties for file_fa? Is there a name? PHP is getting Filedata as the name. If this was PictureX then that would be OK.

Alternatively ...

file_fa.upload("upload.php?id=A"); // pictureA
file_fb.upload("upload.php?id=B"); // pictureB
file_fc.upload("upload.php?id=C"); // pictureC
file_fd.upload("upload.php?id=D"); // pictureD

Now, in PHP ...
<?php
if (isset($_GET['id']) && in_array($_GET['id'], array('A', 'B', 'C', 'D')))
 {
 // Save the file ...
 if (is_uploaded_file(...) & move_uploaded_file(..., "/destination/picture{$_GET['id']}"))
  {
  // All moved and named.
  }
 else
  {
  // Couldn't save.
  }
 }
else
 {
 // No id for picture.
 }
this just might work.

what do "....." in the above code mean. im not to familiar with PHP.

basicly "file_fa" and so on are the variable that are sent to PHP the be converted to images and the path way.



... as in put in the bits here that have already been mentioned.

file_fa looks like an object. Objects have properties. What properties does this object have? Does it have a name? If so is the name currently Filedata?

If so, change the name to the required one.

Is this how i would put what you showed me with my above php code. the php code i use resizes the image to a specific size. if not can you make the neccessary changes. im sorry to be a pest. you are a great help.

<?php
$a= "files";

if (isset($_GET['id']) && in_array($_GET['id'], array('A', 'B', 'C', 'D')))
 {
 // Save the file ...
 if (is_uploaded_file($_FILES['Filedata']['name']) & copyResizeImage($_FILES['Filedata']['tmp_name'], "/$a/picture{$_GET['id']}"))

  {  // All moved and named.  }
 else  {  // Couldn't save.  } }

else { // No id for picture. }

$d= chmod($pictureA,0777);
$d= chmod($pictureB,0777);
$d= chmod($pictureC,0777);
$d= chmod($pictureD,0777);

---------EVERYTHING BELOW HERE WORKS FINE--------------------

function copyResizeImage($oldName, $newName, $maxWidth = 365, $maxHeight = 365)
{
    if (!is_file($oldName)) return false; /* No input */
    if (is_file($newName)) return false; /* Output exists */
    if ($maxWidth <= 0 || $maxHeight <= 0) return false; /* invalid size */
    list($oldWidth, $oldHeight, $type, $attr) = getimagesize($oldName);
    if ($type > 3) return false; /* unsupported image type */
    /* Resize if necessary */
    $newWidth = $oldWidth;
    $newHeight = $oldHeight;
    if ($newHeight <> $maxHeight)
    {
        $newWidth = ($newWidth / ($newHeight / $maxHeight));
        $newHeight = $maxHeight;
    }
    if ($newWidth <> $maxWidth)
    {
        $newHeight = ($newHeight / ($newWidth / $maxWidth));
        $newWidth = $maxWidth;
    }
    /* Increase memory limit to support larger files */
    ini_set('memory_limit', '32M');
    /* Read source file */
    if ($type == 1) $src = ImageCreateFromGif($oldName);
    if ($type == 2) $src = ImageCreateFromJpeg($oldName);
    if ($type == 3) $src = ImageCreateFromPng($oldName);
    /* Convert to JPEG */        
    $dst = ImageCreateTrueColor($newWidth, $newHeight);
    ImageCopyResized($dst, $src, 0, 0, 0, 0,
        $newWidth, $newHeight,
        $oldWidth, $oldHeight);
    /* Write destination file */        
    return(imagejpeg($dst, $newName));
}

DEFINE ('DB_USER', '********');
DEFINE ('DB_PASSWORD', '********');
DEFINE ('DB_HOST', 'mysql');
DEFINE ('DB_NAME', 'temp');
// Make the connnection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

$sql = "INSERT INTO test

(date,topic,companyname,address,city,states,zipcode,contact,phone,fax,email,price,messages,pictureA,pictureB,pictureC,pictureD) VALUES(NOW(),'$topic','$companyname','$address','$city','$states','$zipcode','$contact','$phone','$fax','$email','$price','$messages','$pictureA','$pictureB','$pictureC','$pictureD')";

$result = @mysql_query($sql);

?>
Ah! I see.
NO!!!!!!

You are issuing 4 separate and unrelated requests.

file_fa.upload("upload.php"); // pictureA
file_fb.upload("upload.php"); // pictureB
file_fc.upload("upload.php"); // pictureC
file_fd.upload("upload.php"); // pictureD

is 4 uploads, each upload will have 1 picture. The script will know NOTHING about the other 3 images. You will need to process each image separately.

Unless you can upload the 4 images in 1 request.

I've no idea if you can do that with Flash. You can with HTML.

If your PHP generated some plain text output (debugging info), can you see that in your flash?

Say something like ...

response_a = file_fa.upload("upload.php");
print response_a;

By tagging each request with the image, you can use that to populate the picture manually.
file_fa.upload("upload.php?id=A"); // pictureA
file_fb.upload("upload.php?id=B"); // pictureB
file_fc.upload("upload.php?id=C"); // pictureC
file_fd.upload("upload.php?id=D"); // pictureD

wouldnt this work since im using the same PHP file and when it uploads its using the "id= A" and so on to seperate each image in the php.
Are you thinking that SOMEHOW upload.php will wait until it gets 4 pictures before it process ANY of them?

Each upload from Flash (the client) will send ONE picture to the PHP script upload AND tag the script with a URL parameter indicating the image.

ONE PICTURE!!!!!!!

Not 2, 3 or 4.

ONE!!!!

So the first time upload.php runs it will get image 1 with a param of id=A.
The next line is for the second image and will have a parameter of id=B
etc.

upload.php will run 4 times.

Each time 1 picture with 1 parameter.

The PHP script WILL KNOW NOTHING ABOUT THE OTHER 3 IMAGES!!!!

Imagine a html form. Each of your file_fx's is a form. 4 forms on 1 page with the same action is still 4 forms. Just because you fill in four forms,only 1 is submitted when you click the submit button.

You either have to store one picture at a time.

OR

combine all 4 images into a SINGLE call to upload.php like you would do if you were using HTML

<form enctype="multipart/form-data" action="upload.php" method="POST">
  Image 1<input name="Image[1]" type="file" />
  Image 2<input name="Image[2]" type="file" />
  Image 3<input name="Image[3]" type="file" />
  Image 4<input name="Image[4]" type="file" />
   <input type="submit" value="Send File" />
</form>

The $_FILES array for this form would be ...

array (
  'Image' =>
  array (
    'name' =>
    array (
      1 => 'raq1.psp',
      2 => 'System.pas',
      3 => 'filter_files.php',
      4 => 'bad.txt',
    ),
    'type' =>
    array (
      1 => 'application/octet-stream',
      2 => 'application/octet-stream',
      3 => 'application/octet-stream',
      4 => 'text/plain',
    ),
    'tmp_name' =>
    array (
      1 => 'D:\\Data\\PHP\\File Uploads\\php924.tmp',
      2 => 'D:\\Data\\PHP\\File Uploads\\php925.tmp',
      3 => 'D:\\Data\\PHP\\File Uploads\\php926.tmp',
      4 => 'D:\\Data\\PHP\\File Uploads\\php927.tmp',
    ),
    'error' =>
    array (
      1 => 0,
      2 => 0,
      3 => 0,
      4 => 0,
    ),
    'size' =>
    array (
      1 => 5569,
      2 => 491852,
      3 => 16874,
      4 => 937160,
    ),
  ),
)

If the form was ...

<form enctype="multipart/form-data" action="upload.php" method="POST">
  Image 1<input name="picture_a" type="file" />
  Image 2<input name="picture_b" type="file" />
  Image 3<input name="picture_c" type="file" />
  Image 4<input name="picture_d" type="file" />
   <input type="submit" value="Send File" />
</form>

then $_FILES would be ...

array (
  'picture_a' =>
  array (
    'name' => 'System.pas',
    'type' => 'application/octet-stream',
    'tmp_name' => 'D:\\Data\\PHP\\File Uploads\\php92E.tmp',
    'error' => 0,
    'size' => 491852,
  ),
  'picture_b' =>
  array (
    'name' => 'raq1.psp',
    'type' => 'application/octet-stream',
    'tmp_name' => 'D:\\Data\\PHP\\File Uploads\\php92F.tmp',
    'error' => 0,
    'size' => 5569,
  ),
  'picture_c' =>
  array (
    'name' => 'filter_files.php',
    'type' => 'application/octet-stream',
    'tmp_name' => 'D:\\Data\\PHP\\File Uploads\\php930.tmp',
    'error' => 0,
    'size' => 16874,
  ),
  'picture_d' =>
  array (
    'name' => 'bad.txt',
    'type' => 'text/plain',
    'tmp_name' => 'D:\\Data\\PHP\\File Uploads\\php931.tmp',
    'error' => 0,
    'size' => 937160,
  ),
)


Either upload an array of files - in which case the name, type, tmp_name, error and size will be an array - or use different names in which case the $_FILES[key] will be the input tag's name.



This way, you get all 4 pictures to upload.php in 1 go and can process them all at the same time.

Can i do this?

<? php
$a = "/file/";

   If  (id = A)
   {pictureA = $a.$_FILES['Filedata']['name'];
    copyResizeImage($_FILES['Filedata']['tmp_name'],$pictureA);
    $d= chmod($pictureA,0777);}
else if (id = B)
    {pictureB = $a.$_FILES['Filedata']['name'];
     copyResizeImage($_FILES['Filedata']['tmp_name'],$pictureB);
     $d= chmod($pictureB,0777);}
else if (id = C)
   {pictureC = $a.$_FILES['Filedata']['name'];
    copyResizeImage($_FILES['Filedata']['tmp_name'],$pictureC);
    $d= chmod($pictureC,0777);}
else if (id = D)
   {pictureD = $a.$_FILES['Filedata']['name'];
    copyResizeImage($_FILES['Filedata']['tmp_name'],$pictureD);
    $d= chmod($pictureD,0777);}

function copyResizeImage($oldName, $newName, $maxWidth = 365, $maxHeight = 365)
{
    if (!is_file($oldName)) return false; /* No input */
    if (is_file($newName)) return false; /* Output exists */
    if ($maxWidth <= 0 || $maxHeight <= 0) return false; /* invalid size */
    list($oldWidth, $oldHeight, $type, $attr) = getimagesize($oldName);
    if ($type > 3) return false; /* unsupported image type */
    /* Resize if necessary */
    $newWidth = $oldWidth;
    $newHeight = $oldHeight;
    if ($newHeight <> $maxHeight)
    {
        $newWidth = ($newWidth / ($newHeight / $maxHeight));
        $newHeight = $maxHeight;
    }
    if ($newWidth <> $maxWidth)
    {
        $newHeight = ($newHeight / ($newWidth / $maxWidth));
        $newWidth = $maxWidth;
    }
    /* Increase memory limit to support larger files */
    ini_set('memory_limit', '32M');
    /* Read source file */
    if ($type == 1) $src = ImageCreateFromGif($oldName);
    if ($type == 2) $src = ImageCreateFromJpeg($oldName);
    if ($type == 3) $src = ImageCreateFromPng($oldName);
    /* Convert to JPEG */        
    $dst = ImageCreateTrueColor($newWidth, $newHeight);
    ImageCopyResized($dst, $src, 0, 0, 0, 0,
        $newWidth, $newHeight,
        $oldWidth, $oldHeight);
    /* Write destination file */        
    return(imagejpeg($dst, $newName));
}

DEFINE ('DB_USER', '********');
DEFINE ('DB_PASSWORD', '********');
DEFINE ('DB_HOST', 'mysql');
DEFINE ('DB_NAME', 'temp');
// Make the connnection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

$sql = "INSERT INTO test

(date,topic,companyname,address,city,states,zipcode,contact,phone,fax,email,price,messages,pictureA,pictureB,pictureC,pictureD) VALUES(NOW(),'$topic','$companyname','$address','$city','$states','$zipcode','$contact','$phone','$fax','$email','$price','$messages','$pictureA','$pictureB','$pictureC','$pictureD')";

$result = @mysql_query($sql);

?>
disregard the last posting. i see what you are say. i have to have all the picutures be able to be uploaded at one time from flash to work in php the way im trying to do it.
is there anyway i can call for the id number that i have automaticly incrementing in sql to populate in php. so when i do load image 2, 3 , and  4 i can refer to that number and just update and load the pictures in to that  specific line. i might have 10000 people so when a new person comes along i can make sure their picture show up in their row of information.
ASKER CERTIFIED SOLUTION
Avatar of Khanh Doan
Khanh Doan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep. As bonmat86 says.

I think you have all the info you need.

WOW! Not even a split or a thanks! Gee.
something happen with my computer how do you split the points?
Too late now!!!!

Ha!

If you really want to, go to Community Support and ask a 0pt question asking for the points to be reassigned or to have the PAQ removed back so you can allocate the points yourself.
i'll do that. thanks for your help