A PHP newbie's first project
Moderator: General Moderators
A PHP newbie's first project
Okay, I'm an absolute PHP newbie, been doing tutorials, reading about everything related to PHP.
The main reason why I got interested in PHP was to complete a little goal I set myself - and because I'm partly lazy.
Well, the deal is - I got about... 1300 musicvideos of different sorts, that all lie in my E:\Musicvideos\A, \B, \C, and so forth, according to the first name of the artist.
Now I've gotten a program to create about 1300 screengrabs, one from each video, and putted them in /videoimages on my webserver.
The musicvideo's are mapped with the alias /musicvideo which points to E:\Musicvideos
What I now want to, is to make a PHP-script that randomly loads up any of the preloaded images from f.ex http://localhost/videoimages/A/Adema - Giving In.gif, and makes a link to http://localhost/musicvideos/A/Adema - Giving In.mpg.
Since this is a bunch of videos and images, I want to make the PHP-script load them all in automaticly, without having to know anything else than the folders.
As I said, I'm a complete and utter newbie, so I could really use some hints and pointers on how to get started on this.
-hellboy-
The main reason why I got interested in PHP was to complete a little goal I set myself - and because I'm partly lazy.
Well, the deal is - I got about... 1300 musicvideos of different sorts, that all lie in my E:\Musicvideos\A, \B, \C, and so forth, according to the first name of the artist.
Now I've gotten a program to create about 1300 screengrabs, one from each video, and putted them in /videoimages on my webserver.
The musicvideo's are mapped with the alias /musicvideo which points to E:\Musicvideos
What I now want to, is to make a PHP-script that randomly loads up any of the preloaded images from f.ex http://localhost/videoimages/A/Adema - Giving In.gif, and makes a link to http://localhost/musicvideos/A/Adema - Giving In.mpg.
Since this is a bunch of videos and images, I want to make the PHP-script load them all in automaticly, without having to know anything else than the folders.
As I said, I'm a complete and utter newbie, so I could really use some hints and pointers on how to get started on this.
-hellboy-
Ok first make a script that reads the directory of the .gif's and places them in a string.
Like so:
after this you should have each .gif in the string $filelist[] and also $count is the variable that counted the nr of images in the list.
Look up the rand (random) function on http://www.php.net and see how you can get a random nr between 1 and $count. And make that the variable $random
Then print the random image from the list on your page.
that should get you started 
Like so:
Code: Select all
if ($handle = opendir("Your_imagedir")){
while (false !== ($file = readdir($handle))){
if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){
$filelistї] = "$file";
}
}
closedir($handle);
}
sort($filelist);
reset($filelist);
$count = count($filelist);Look up the rand (random) function on http://www.php.net and see how you can get a random nr between 1 and $count. And make that the variable $random
Then print the random image from the list on your page.
Code: Select all
print "<img src="directory\$filelistї$random]">";Something's wrong somewhere, the img src is now "http://mymachine.mydomain/directory$filelist[23]", where 23 changes randomly, so it have counted the number of images in the specified folder, but it seems like it's unable to get the imagenames...
I've probably messed up somewhere :p
I've probably messed up somewhere :p
Code: Select all
<?php
if ($handle = opendir("videoimages")){
while (false !== ($file = readdir($handle))){
if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){
$filelistї] = "$file";
}
}
closedir($handle);
}
sort($filelist);
reset($filelist);
$count = count($filelist, - 1);
$random = rand (1, $count);
print "<img src="directory\$filelistї$random]">";
?>oops, my bad
Code: Select all
print "<img src="videoimages/$filelistї$random]">";Now, for the linking to the musicvideos I'm a bit curious - the image-script wasn't able to fetch images that was in subdirectories of "videoimages", which would probably have mad it easier, as the musicvideos are sorted into folders, artists sorted by name.
The gif's names are now the original filename, just with an added .gif at the end, giving filenames like Aphex Twin - Windowlicker.mpg.gif.
So all I now need to do, is find a way to...
1) grabbing filenames from subfolders
2) making the a-href use the original location, just switching "videoimages" with "musicvideos" and removing .gif
Well actually... what you want to do is this.
In the function where you get the contents of videoimages.. you want to say that if it encounters a directory, it should also add that directory/the_contents in $filelist.
Sorry... I personally haven't done that yet, so I'm not much help there. I think you need to check the isdir function (if that's it.. am not sure)
The trick is... assuming that you have exactly the same filenames (apart from mpg/gif) and subfolder names for the video files and .gif's.
Well ok here goes.... see the sort($filelist) function? that sorts the filenames in $filelist alphabeticly.
So say you create a link like so.
print "<a href=\"$the_view_mpg_page?id=$random\"><img src=\"videoimages/$filelist[$random]\"></a>";
Now by doing this you submit the random number as $id to the next page.
So then you again let the script put the filenames in the $filelist string, but this time from the vieo directory, and just have it print out $filelist[$id] which is the random number from the page before. So you don't need the random function on this page.. since you know which file.
And since both strings have the same alphabetical order $id should get you the corresponding file.
In the function where you get the contents of videoimages.. you want to say that if it encounters a directory, it should also add that directory/the_contents in $filelist.
Sorry... I personally haven't done that yet, so I'm not much help there. I think you need to check the isdir function (if that's it.. am not sure)
The trick is... assuming that you have exactly the same filenames (apart from mpg/gif) and subfolder names for the video files and .gif's.
Well ok here goes.... see the sort($filelist) function? that sorts the filenames in $filelist alphabeticly.
So say you create a link like so.
print "<a href=\"$the_view_mpg_page?id=$random\"><img src=\"videoimages/$filelist[$random]\"></a>";
Now by doing this you submit the random number as $id to the next page.
So then you again let the script put the filenames in the $filelist string, but this time from the vieo directory, and just have it print out $filelist[$id] which is the random number from the page before. So you don't need the random function on this page.. since you know which file.
And since both strings have the same alphabetical order $id should get you the corresponding file.
Ok right of course you don't want to see the video inside a paeg.. you jsut want to access it through the image's link.
Even simpler.Just try this.
See just create 2 strings, one for the image, 1 for the video, use the video one for the link and the image one for the image.
Now you must make sure of course that for each image there is a correspondnig video file with basicly the same spot when all is placed in alphabetical order.
I think that's what you want, but I also think you could have figured this last step out yourself
LC
Even simpler.Just try this.
Code: Select all
<?php
if ($handle = opendir("videoimages")){
while (false !== ($file = readdir($handle))){
if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){
$imagelistї] = "$file";
}
}
closedir($handle);
}
sort($imagelist);
reset($imagelist);
$count = count($imagelist, - 1);
$random = rand (1, $count);
if ($handle = opendir("musicvideos")){
while (false !== ($file = readdir($handle))){
if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){
$videolistї] = "$file";
}
}
closedir($handle);
}
sort($videolist);
reset($videolist);
print "<a href="musicvideos\$videolistї$random]"><img src="videoimages\$imagelistї$random]"></a>";
?>See just create 2 strings, one for the image, 1 for the video, use the video one for the link and the image one for the image.
Now you must make sure of course that for each image there is a correspondnig video file with basicly the same spot when all is placed in alphabetical order.
I think that's what you want, but I also think you could have figured this last step out yourself
LC
make life easy...
here u go man, this should work for u...it gets all the mpgs stored in the folder and subfolers and puts it in array, gets a radom mpg from the array, and adds a .gif to the end...since ur files are name thisfile.mpg and thisfile.mpg.gif...

Code: Select all
<?php
$fileArray = array();
function get_files($location) {
global $fileArray;
$hook = opendir($location);
while (($file = readdir($hook)) !== false) {
if ($file != "." && $file != "..") {
$path = $location . "/" . $file; // set up full path
if (is_dir($path)) get_files($path);
else array_push($fileArray, $path);
}
}
closedir($hook);
}
$location = "musicvideos";
get_files($location);
$random = rand(1, sizeof($fileArray)) - 1 ;
echo "<a href='" . $fileArrayї$random] . "'>";
$img = "videoimages" . str_replace($location, "", $fileArrayї$random]) . ".gif";
echo "<img src='$img' border='0'></a>";
?>