Pergunta

I am using Uploadify v2.1.4 to upload and resize image files. So far So good. My files are saved with random filenames using time().

I need to return the image path url to index.php from uploadify.php and i thought i can use ajax. So my uploadify settings are:

<script type="text/javascript">            
$(document).ready(function() {                    

$('#img_upload').uploadify({                            
'uploader'  : 'uploadify/uploadify.swf',
'script'    : 'uploadify.php',
'cancelImg' : 'uploadify/uploadify-cancel.png',
'folder'    : 'uploads/',
'multi'     : false,
'auto'      : true,
'wmode'     : 'transparent',
'fileDesc'  : "Only (*.jpg, *.jpeg)",
'fileExt'   : "*.jpg;*.jpeg",
'sizeLimit' : 8000000,  
'onSelect': function (evt, queueID, fileObj) {              
},                      
'onComplete': function (evt, queueID, fileObj, response, data) {                           

var mcommand = "resend";
var dataString = 'mcommand='+ mcommand;
$.ajax({
type: "POST",
url: "uploadify.php",
data: dataString,
cache: false,
success: function(data) { //ALSO I USED AND function(html) with alert(html) 
alert(data);                   
}
});         
}       

});         
}); 
</script> 

and my uploadify modifided script is:

<?php
// Define a destination
$targetFolder = 'uploads/';

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $targetFolder;
    $targetFile = $targetFolder . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

        $image = $_FILES["Filedata"]["name"];
    $uploadedfile = $_FILES['Filedata']['tmp_name'];


    if ($image) 
    {

        $filename = stripslashes($_FILES['Filedata']['name']);

        $extension = getExtension($filename);
        $extension = strtolower($extension);


 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) 
{

echo '<div class="msgdiv">Unknown Image extension </div> ';

}
else
{

$size=filesize($_FILES['Filedata']['tmp_name']);

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['Filedata']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['Filedata']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);

$newwidth=640;
$newheight=480;
//$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=180;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$image_name=time().'.'.$extension;

$sext = "small_";

$filename = $targetFolder . $image_name;
$filename1 = $targetFolder . "small_". $image_name;

imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}  

if ($_POST['mcommand'] == "resend"){
echo $filename;
}else{
echo "some error";  
}


}
?>

The problem is that i don't get the results (echo $filename) back to index.php

Foi útil?

Solução

Read more carefully documentation (basic example http://www.uploadify.com/documentation/uploadify/customizing-the-server-side-upload-script/).

In uploadify.php you do checking:

if ($_POST['mcommand'] == "resend"){
   echo $filename;
}else{
   echo "some error";  
}

But you dont send param command in:

$('#img_upload').uploadify({                            
   'uploader'  : 'uploadify/uploadify.swf',
   // ...

You include param command to ajax-request on Uploadifys event'onComplete'`:

'onComplete': function (evt, queueID, fileObj, response, data) {                       

   var mcommand = "resend";
   var dataString = 'mcommand='+ mcommand;
   //...

And now ( ajax-request from 'OnComplete') $_FILES is empty => $filename is empty,too. You need change method 'OnComplete' to 'onUploadSuccess' (rename like in basic example).Then add next:

$('#img_upload').uploadify({    
  //....
  data : "commad=resend";
  //..

At in remove echo src; from uploadify.php;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top