Showing link of which random swf was opened - php

I'm new to PHP and this seems like I'm just asking for code but in reality I want to know how to implement a link along with the random selector.
My code:
<?php
$imglist='';
//$img_folder is the variable that holds the path to the swf files.
// see that you dont forget about the "/" at the end
$img_folder = "../files/flash/";
mt_srand((double)microtime()*1000);
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, ad them to a list
while ($file = $imgs->read()) {
if (preg_match("/\.swf$/i", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
//display random swf
echo '<embed src="'.$img_folder.$image.'" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="650"
height="450"></embed>';
?>
This basically grabs a random flash file from my site's directory. Since it won't affect the URL bar, they won't be able to send something they like to someone (which is an unintentional security feature). I want them to be able to send a link or possibly direct click to download.
Also, I was thinking of as well as the random generator, to make a left and right arrow that they can scroll through all the .swf's in the chosen directory, if possible.
the site is www.nsgaming.us and it's the 'Random' button under the text logo.

What you are asking for is reading parameters from the URL so people can link their friends to this SWF embed, you can do that by using $_GET
Look at this example I coded.
$swfs = array();
$swf_location = '../files/flash/';
if($handle = opendir($swf_location)) {
while(false !== ($entry = readdir($handle))) {
if(strtolower(pathinfo($path, PATHINFO_EXTENSION)) == 'swf') {
$swfs[] = $entry;
}
}
closedir($handle);
}
if(isset($_GET['swf']) && in_array($_GET['swf'], $swfs)) {
$swf = $_GET['swf'];
} else {
$swf = $swfs[rand(0, count($swfs))];
}
echo '<embed src="' . $swf_location . $swf . '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="650" height="450"></embed><br />Send this to a friend: <input type="text" value="http://yourwebsite.com/thisfilesname.php?swf=' . $swf . '" length="55">';
You're previous script was very messy and did a lot of useless stuff, this is secure and faster

Related

PHP save the file using the link found by the block ID on the page

On the page https://data.mos.ru/opendata/61241/ the first url with parameter "export/get?id=" contains the last actual link to download the open data csv file //op.mos.ru/EHDWSREST/catalog/export/get?id=989116 .
The problem is that the digital ending of the url after each update is different and is not known in advance.
I have a script that works and allows me to save a file at a pre-known file url (but it only saves the old version of the file, not the current one):
<?php
function downloadJs($file_url, $save_to)
{
$content = file_get_contents($file_url);
file_put_contents($save_to, $content);
}
downloadJs('https://op.mos.ru/EHDWSREST/catalog/export/get?id=989116', realpath("./img/feeds") . '/61241.zip');
$zip = new ZipArchive;$zip->open('./img/feeds/61241.zip');$zip->extractTo('./img/feeds/61241');$zip->close();
$directory = './img/feeds/61241/'; if ($handle = opendir($directory)) { while (false !== ($fileName = readdir($handle))) { $dd = explode($fileName); $newfile = '61241.csv'; rename($directory . $fileName, $directory.$newfile); } closedir($handle); }
echo "Ok!";
?>
I need to change this PHP script so that on the page https://data.mos.ru/opendata/61241/ first determined the first link to the download file by the parameter "export/get?id=", where the link is located.
I'm not sure if you understand what you mean.
we have:
<a target="_blank" href="//op.mos.ru/EHDWSREST/catalog/export/get?id=989116" onclick="yaCounter29850344.reachGoal('download_csv')...
Perhaps we will use a little regex to get that id.
Let's say you already have its html with file_get_contents:
preg_match('#get\?id=(\d+)".* onclick="[^"]+csv[^"]+"#', $html, $matches);
echo $matches[1]; // 989116

Randomize full-screen background image in WordPress

I haven't seen this asked yet, so if it is, can someone re-direct me?
I'm having an issue creating a full screen background for my WordPress theme that uses random images from the image library. I want to write a PHP function that can be used on multiple sites, so I can't simply use the direct path in the code. It also needs to work on MultiSite in such a way that it only pulls images that are uploaded to that site. Here's the code I'm working with:
HTML for my Background Div
<div id="background" class="background" style="background-image:url(<?php displayBackground();?>);">
</div>
PHP to randomize my image folder
<? php
function displayBackground()
{
$uploads = wp_upload_dir();
$img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
$cnt = 0;
$bgArray= array();
/*if we can load the directory*/
if ($handle = opendir($img_dir)) {
/* Loop through the directory here */
while (false !== ($entry = readdir($handle))) {
$pathToFile = $img_dir.$entry;
if(is_file($pathToFile)) //if the files exists
{
//make sure the file is an image...there might be a better way to do this
if(getimagesize($pathToFile)!=FALSE)
{
//add it to the array
$bgArray[$cnt]= $pathToFile;
$cnt = $cnt+1;
}
}
}
//create a random number, then use the image whos key matches the number
$myRand = rand(0,($cnt-1));
$val = $bgArray[$myRand];
}
closedir($handle);
echo('"'.$val.'"');
}
I know that my CSS markup is correct because if I give the DIV a fixed image location, I get a fullscreen image. Can anyone tell me what to do to fix it?

Random flash object page PHP

FIXED!
Feel free to use my code, no need to cite my work. There was no problem, just that the image size was too small and some weren't showing up. duh.
I will change the size from 100px to 500px for anyone who wants to use this code.
Have fun
I'm trying to use some open code that's available on the internet to make a random flash generator.
The orignal code:
<?php
$imglist='';
//$img_folder is the variable that holds the path to the swf files.
// see that you dont forget about the "/" at the end
$img_folder = "images/";
mt_srand((double)microtime()*1000);
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, ad them to a list
while ($file = $imgs->read()) {
if (eregi("swf", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
//display random swf
echo '<embed src="'.$img_folder.$image.'" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="100"
height="100"></embed>';
?>
My modified code:
<?php
$imglist='';
//$img_folder is the variable that holds the path to the swf files.
// see that you dont forget about the "/" at the end
$img_folder = "../files/flash/";
mt_srand((double)microtime()*1000);
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, ad them to a list
while ($file = $imgs->read()) {
if (preg_match("/(swf)/i", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
//display random swf
echo '<embed src="'.$img_folder.$image.'" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="500"
height="500"></embed>';
?>
I was at first having problems with ergi on line 11, I was told to replace it with preg and I went through and figured that out.
I loaded up my random page, (http://www.nsgaming.us/random/) and it flashed for a fraction of a second a random flash object I had in the folder but now it just shows nothing.
help please?
my index shows as follows:
<html>
<head>
<title>test</title>
<?php
include("../menu.php");
include_once('random2.php');
?>
</head>
<body>
<p>This is the random page.</p>
<p>I am planning on having random flash objects load here but still working on it.</p>
</body>
</html>
Keep in mind I am new to moderate at web design, HTML and PHP.
If you could try and not yell at me for doing something stupid which is probably what happened.
instead of
if (preg_match("/(swf)/i", $file))
try
if (preg_match("/\.swf$/i", $file))
Also the following line is going to break your code if any filename has space in your filename.
$imglist = explode(" ", $imglist);
Instead of
while ($file = $imgs->read()) {
if (preg_match("/(swf)/i", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
The following code will help you serve the filenames with spaces also. This will also exclude the two dot directories.
$imglist=array();
while (false !== ($file = $imgs->read())) {
if (($file==".")||($file=="..")) continue;
if (preg_match("/\.swf$/i", $file))
$imglist[]= $file;
} $imgs->close();
The flash objects are returning 404.
One of them actually works.
http://www.nsgaming.us/files/flash/anon_partyhard007.swf
Based on this, I think you might be looking at the wrong files folder. I'm assuming you have a public files folder, and then a files folder one level below root?
Perhaps this might work better.
$img_folder = "./files/flash/";

php array keyword counter

I am pushing image file information into an array.
It is pretty simple except the keywords are sometimes an array also.
This works great for what I am doing now.
Here is a sample of my array
$list[]=array(filename=>$file,width=>$w,height=>$h,caption=>$iptc["2#120"],keywords=>$iptc["2#025"]);
I can use this array to output the html needed for a javascript slideshow.
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
//if this file is a valid image
$path = $dirname . "" . $file ;
$path2 = $dirname ."JPEG/" . $file ;
$size = getimagesize($path, $info);
$w = $size[0]; $h = $size[1];
$iptc = iptcparse($info['APP13']);
if(in_array($key,$iptc["2#025"])){
$list[]=array(
filename=>$file,
width=>$w,
height=>$h,
caption=>$iptc["2#120"],
keyw‌​ords=>$iptc["2#025"]
);
}
}
}
closedir($handle); }
I would like to be able to have another variable in the array which would count up one number as each unique keyword is added. This will allow me to go directly to the middle of a slideshow as the js plugin I am using a js slideshow only have direct links if referenced by a number
I imagine I would need to create a unique array of all the keywords and then have some type of complicated if statement to count for each of the unique variables.....
however I have no Idea how to do this
Help Please
thanks
Jeremy
Im not sure if I understand what you're looking for but it sounds like:
while(loop_conditions){
$list[]=array(filename=>$file,width=>$w,height=>$h,caption=>$iptc["2#120"],keywords=>$iptc["2#025"]);
foreach($iptc["2#025"] as $keyword){
$list_map[$keyword]++;
}
}

How can I add captions to this auto-create php gallery?

<?php
/* settings */
$image_dir = 'gallery/';
$per_column = 3;
$count=0;
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'-thumb'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<a class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo '<p>There are no images in this gallery.</p>';
}
?>
How can I add captions to each of the images?
Thank you very much for the answer!
Your code is traversing the files within the specified directory looking for files containing ...-thumb... in the filename, appending them to an array, and then looping over the array to generate HTML for displaying the thumbnail image gallery.
Adding additional information to something like this, given the very limited code you've provided, can be done in any number of ways
You could implement a database with a column for each filename and a
related column containing captions/descriptions. This might be more
trouble than it's worth depending on what you're trying to achieve.
You could use a flat file which you could parse line by line (instead of traversing the
folder for ...-thumb... images), containing some format like
file1-thumb.png|some caption here
file2-thumb.png|some caption here
file3-thumb.png|some caption here
...
You could includ a small caption in the filename itself, and parse/format the caption from the filenames. This would probably be the quickest route, but most limiting in terms of flexibility in the length/characters allowed for the captions.
- file1-thumb--some_caption_here.png
- file2-thumb--some_caption_here.png
- file3-thumb--some_caption_here.png
To actually add the caption to the generated HTML, you can use a title attribute as #rockerest suggested, however I'd personally add such a caption to to the image itself, as that is what the caption is describing (not the link)
<img src="..." title="..." ... />
UPDATE
To answer your comment (this provides better formatting for code), Let's say we have a file with name file1-thumb--some_description_here.jpg, you can parse and format the caption with preg_replace
$filename = 'file1-thumb--some_description_here.jpg';
$caption = preg_replace(array('/^.+-thumb--/', '/\.(jpg|jpeg|gif|png|bmp)$/', '/_/'), array('','',' '), $filename);
$caption is now some description here
The html title offers a "caption" when the mouse is hovered over the image.
foreach($files as $file)
{
$count++;
echo '<a title="',$caption,'" class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}

Categories