Upload image through iframe magento
My phtml file
<form id="file_upload_form" method="post" enctype="multipart/form-data" action="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'catalog/product/myupload';?>">
<input name="file" id="file" size="27" type="file" /><br />
<input type="submit" name="action" value="Upload" /><br />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
<div id="image_details"></div>
<script type="text/javascript">
function init() {
document.getElementById('file_upload_form').onsubmit=function() {
document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
document.getElementById("upload_target").onload = uploadDone; //This function should be called when the iframe has compleated loading
// That will happen when the file is completely uploaded and the server has returned the data we need.
}
}
function uploadDone() { //Function will be called when iframe is loaded
var ret = frames['upload_target'].document.getElementsByTagName("body")[0].innerHTML;
document.getElementById("uploadaddtocart").disabled = false;
document.getElementById("deleteimage").style.display = "block";
jQuery("#image_details").html("<img src='http://xaprio.com/Client/demos/tfasigns/media/customertemplates/" + ret + "' />");
}
window.onload=init;
</script>
My controller action
public function myuploadAction()
{
/*echo "hello";
print_r($_POST);
print_r($_FILES);*/
$path = '/home/xaprioc/public_html/Client/demos/tfasigns/media/customertemplates';
$imgarr = $this->upload('file',$path,'txt,jpg,jpeg,gif,png');
echo $imgarr[0];
$imagewithpath = $path."/".$imgarr[0];
Mage::getSingleton('core/session')->setUploadimage($imagewithpath);
}
public function upload($file_id, $folder="", $types="")
{
if(!$_FILES[$file_id]['name']) return array('','No file specified');
$file_title = $_FILES[$file_id]['name'];
//Get file extension
$ext_arr = split("\.",basename($file_title));
$ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension
//Not really uniqe - but for all practical reasons, it is
$uniqer = substr(md5(uniqid(rand(),1)),0,5);
$file_name = $uniqer . '_' . $file_title;//Get Unique Name
$all_types = explode(",",strtolower($types));
if($types) {
if(in_array($ext,$all_types));
else {
$result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
return array('',$result);
}
}
//Where the file must be uploaded to
if($folder) $folder .= '/';//Add a '/' at the end of the folder
$uploadfile = $folder . $file_name;
$result = '';
//Move the file from the stored location to the new location
if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
$result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
if(!file_exists($folder)) {
$result .= " : Folder don't exist.";
} elseif(!is_writable($folder)) {
$result .= " : Folder not writable.";
} elseif(!is_writable($uploadfile)) {
$result .= " : File not writable.";
}
$file_name = '';
} else {
if(!$_FILES[$file_id]['size']) { //Check if the file is made
@unlink($uploadfile);//Delete the Empty file
$file_name = '';
$result = "Empty file found - please use a valid file."; //Show the error message
} else {
chmod($uploadfile,0777);//Make it universally writable.
}
}
return array($file_name,$result);
}