CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 IMAGE based on FILETYPE - (RESOLVED)

Print topic Send  topic

Author Message
SecureCloud


Posts: 51
Posted: 12/06/2012, 9:41 AM

I'm trying to achieve, an image based off file extension, i have searched the forum for hours and looked at examples, but none were similar to mine.
i am using php, and CCS5

  
switch ($Container->file_name->GetValue())    
{    
case ".gif":  
case ".jpg":  
$image = "image/picture.jpg";    
break;    
  
case ".pdf":    
$image = "image/acrobat.jpg";    
break;  
  
case ".doc":    
$image = "image/document.jpg";    
break;  
  
default:    
$image = "image/noFileType.jpg";    
}    
    
$Container->file_name->SetValue($image);   

the code above just displays the default image. it would work like that if i knew the file name exactly, but i dont.

is there a way to use: case " *.jpg ": ... ?? an asterisk to be ALL then ext ??

i know that this is a regular expression for file types, and i can add more...

  
/^.*\.(jpg|jpeg|png|gif)$/i  
or  
preg_match('/^.*\.(jpg|jpeg|png|gif)$/i');  

how could i format the case statement to only check extension, ( i might have about 30 file types)

should i just store the reg expr in a var, then call it in the case statement ??


_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
Lucius

Posts: 220
Posted: 12/06/2012, 10:54 AM

You can use regex, but I think there is simpler way..

Check php.net for explode function, or for strpos and substr functions

To make it even easier I would set it up like so:

1. use explode to get the extension part

2. on server have N amount of files all named so: gif.jpg, jpg.jpg, png,jpg, pdf.jpg

3. Finally just set the name of $image to YOUR_EXPLODED_EXTENSION.jpg

4. Check if file_exists on server and if not use
$image = "image/noFileType.jpg";
View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 11:13 AM

Thanks Lucius, your right, EXPLODE is the way to go.

explode it to 2 vars
1.) filename
2.) extension

then use case->imageType

how would i use EXPLODE in this example, outside the switch or in a case statement ??


_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
Lucius

Posts: 220
Posted: 12/06/2012, 11:31 AM

I correct myself 8-)

Use this:
$ext = pathinfo($filename, PATHINFO_EXTENSION);

Use it before switch and then switch $ext.
View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 11:54 AM

i tried Lucius, what am i doing wrong....

  
//Custom Code @21-2A29BDB7  
  
$checkExt = explode(".", file_name);  // file_name is the name of my label  
switch ($Container->file_name->GetValue($checkExt))    
{   
case ".pdf":    
$image = "images/acrobat.png";    
break;    
    
case ".gif":  
$image = "images/image.png";    
break;    
    
case ".exe":    
$image = "images/application.png";    
break;    
    
case ".docx":    
$image = "images/word.png";    
break;    
   
case ".zip":    
$image = "images/zip.png";    
break;    
   
// if none display this  
    
default:    
$image = "images/info.png";    
    
}    
    
$Container->file_name->SetValue($image);    
  
//End Custom Code  

it only displays the default image


_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 12:20 PM

i looked at the code for get original filename, can i use and modify this code in my event ?

  
//Get Original Filename @21-10353880  
    $control_value = $Component->GetValue();  
    $original_filename = CCGetOriginalFileName($control_value);  
    $Component->SetValue($original_filename);  
//End Get Original Filename  

ok, now i got it to display just the extensions... ?
like this...

  
$control_value = $Component->GetValue();    
$original_filename = CCGetOriginalFileName($control_value);    
$path_parts = pathinfo($original_filename);  
$Component->SetValue($path_parts['extension']);    

_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 1:05 PM

** I DID IT !! - (all by myself.. :-D )

here's my final code and steps...

change label to image control, then BeforeShow->Custom Code

  
//Custom Code @21-2A29BDB7  
  
$control_value = $Component->GetValue();    
$original_filename = CCGetOriginalFileName($control_value);    
$path_parts = pathinfo($original_filename);  
$ext = $Component->SetValue($path_parts['extension']);    
  
switch ($Container->file_name->GetValue($ext))    
{    
  
case "gif":  
case "png":  
$image = "images/image.png";    
break;    
  
case "pdf":    
$image = "images/acrobat.png";    
break;  
  
case "docx":    
$image = "images/word.png";    
break;  
  
case "zip":    
$image = "images/zip.png";    
break;  
  
default:    
$image = "images/page_white_text.png";    
    
}    
    
$Container->file_name->SetValue($image);   
  
//End Custom Code  

now it takes the extension, and displays the correct image.

After i finish up what i am working on, i'm gonna create a thread that will help ALL of you. (You will thank me..)

imma change it to image link, and when clicked open file in new window.

*** my question is, the uploaded files are in: .. files/ $filename
what would the href type and source be ??
_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 1:59 PM

ok, to close this thread...

i converted it to image link.

href type: Datasource Parameter
href source: file_name
target frame: _blank

edited html:
<a href="files/{file_name}"

added ' files/ '
in the href tag.

all done... tah-dah!


_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message
SecureCloud


Posts: 51
Posted: 12/06/2012, 4:05 PM

This may be the long way to do this, but i will share a working version. this is my final code.
these are the ones i needed for project... there are ALOT more extensions.. (so u don't have to tell me that)

here's a image of what i mean... and the images ARE clickable and open in a new window.



here is the working code...

//Custom Code @17-2A29BDB7  
  
$control_value = $Component->GetValue();    
$original_filename = CCGetOriginalFileName($control_value);    
$path_parts = pathinfo($original_filename);  
$ext = $Component->SetValue($path_parts['extension']);    
  
switch ($Container->file_name->GetValue($ext))    
{    
  
case "ai":  
case "bmp":  
case "gif":  
case "jpg":  
case "jpeg":  
case "png":  
case "psd":  
case "svg":  
case "tif":  
case "tiff":  
case "ico":  
$image = "images/image.png";    
break;    
  
case "pdf":    
$image = "images/acrobat.png";    
break;  
  
case "doc":  
case "docx":  
case "odt":  
case "rtf":  
case "wps":  
case "xps":  
$image = "images/word.png";    
break;  
  
case "ppt":  
case "pptx":  
case "ppsx":  
case "pps":  
$image = "images/powerpoint.png";  
break;  
  
case "xsd":  
case "xsl":  
case "xlsx":  
case "xls":  
case "xla":  
case "xlr":  
case "xlsm":  
case "ods":  
$image = "images/excel.png";  
break;  
  
case "7z":  
case "ace":  
case "arc":  
case "bz2":  
case "cab":  
case "cpt":  
case "deb":  
case "dmg":  
case "gz":  
case "iso":  
case "lha":  
case "lz":  
case "lzh":  
case "lzma":  
case "lzo":  
case "pit":  
case "rar":  
case "rpm":  
case "rz":  
case "s7z":  
case "sea":  
case "sit":  
case "sitx":  
case "tar":  
case "tar.gz":  
case "tgz":  
case "zip":  
$image = "images/zip.png";    
break;  
  
case "txt":  
case "csv":  
case "dat":  
case "bak":  
case "tmp":  
case "log":  
$image = "images/text.png";  
break;  
  
case "mp3":  
case "aif":  
case "alac":  
case "m4a":  
case "m4p":  
case "ogg":  
case "wav":  
case "wma":  
case "m4a":  
case "mid":  
case "mpa":  
case "ra":  
$image = "images/audio.png";    
break;  
  
case "mp4":  
case "3gp":  
case "wmv":  
case "avi":  
case "flv":  
case "mov":  
case "mpg":  
case "rm":  
case "vob":  
$image = "images/film.png";  
break;  
  
case "xml":  
$image = "images/code_red.png";  
break;  
  
case "css":  
$image = "images/color_swatch.png";  
break;  
  
case "html":  
case "htm":  
$image = "images/code_blue.png";  
break;  
  
case "swf":  
case "fla":  
$image = "images/flash.png";  
break;  
  
case "exe":  
case "app":  
case "air":  
case "apk":  
case "bat":  
case "bin":  
case "com":  
case "cgi":  
case "ipa":  
case "jar":  
case "prg":  
case "run":  
case "scr":  
case "pkg":  
$image = "images/application.png";  
break;  
  
case "db":  
case "dbf":  
case "mdb":  
case "mdf":  
case "odb":  
case "pdb":  
case "sql":  
case "sqlite":  
case "sqlitedb":  
case "usr":  
case "wdb":  
case "xld":  
$image = "images/database.png";  
break;  
  
default:    
$image = "images/info.png";    
}    
    
$Container->file_name->SetValue($image);   
  
  
//End Custom Code  

this may help someone else...
_________________
-------------------------------------------------------------------------------------------------
If this helped You, Please Donate to SecureCloud Here, ANY amount Accepted.
http://securecloud.biz/donate/
Thank You, Your help Keeps us Going.

View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.