上传完成uploadify后获取图片路径

时间:2013-12-31 00:35:43

标签: php uploadify

我正在使用Uploadify v2.1.4上传和调整图像文件的大小。到现在为止还挺好。 我的文件使用time()随机文件名保存。

我需要将图像路径url从uploadify.php返回到index.php,我想我可以使用ajax。所以我的uploadify设置是:

<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> 

我的uploadify修改过的脚本是:

<?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";  
}


}
?>

问题是我没有得到结果(echo $ filename)回到index.php

1 个答案:

答案 0 :(得分:1)

仔细阅读文档(基本示例http://www.uploadify.com/documentation/uploadify/customizing-the-server-side-upload-script/)。

uploadify.php进行检查:

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

但你不发送参数command

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

您在上传command'onComplete'`上的ajax-request中加入了参数s event

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

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

现在(来自'OnComplete'的{​​ajax-request})$_FILES为空=&gt; $filename也是空的。 您需要将方法'OnComplete'更改为'onUploadSuccess'(在基本示例中重命名)。然后添加下一个:

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

echo src;移除uploadify.php;