...

View Full Version : get file name and image and writes to a file



ska_defender
07-31-2012, 08:49 AM
Hello,
The following script writes to a file include.php

<?php
$path = "./files/";
$path2="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/";
//echo $path2;
$folder = opendir($path);
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$result="{\nlevels: [\n{ file: \"$path2$file\" }\n],\n\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
fclose($inF);
?>

It searches the folder and look for any file and store it in $path2$file
so the result posted in include.php is (If the folder has one file)


{
levels: [
{ file: "path to/files/filename" }
],
title: "filename"
},

and if the folder has 2 files result is


{
levels: [
{ file: "path to/files/filename1" }
],
title: "filename2"
},
{
levels: [
{ file: "path to/files/filename2" }
],
title: "filename2"
},

and so on...

What I need is to search for another folder /images and write the same image file in include.php just below: { file: "path to/files/filename1" }

So the result will be


{
levels: [
{ file: "path to/files/filename1" }
],
image: "path to/images/imagefilename1",
title: "filename2"
},

and it will post the same image path for every image file in images folder

I have tried to incorporate the same code with slight variable changes but I am stuck at using while

Any help ?

Arcticwarrio
07-31-2012, 05:01 PM
will the image name in the image folder be the same as the filename in the files folder except for the extension?

Arcticwarrio
07-31-2012, 05:13 PM
this will do multiple folders just add them into the arrays at the top


<?php
$path = array("./files/","./images/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");

foreach($path as $key => $dir) {
$folder = opendir($dir);
$filename = strstr($file, '.', true);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
}

fclose($inF);
?>

ska_defender
07-31-2012, 07:59 PM
will the image name in the image folder be the same as the filename in the files folder except for the extension?
Yes its the same

ska_defender
07-31-2012, 08:12 PM
this will do multiple folders just add them into the arrays at the top


<?php
$path = array("./files/","./images/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");

foreach($path as $key => $dir) {
$folder = opendir($dir);
$filename = strstr($file, '.', true);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
}

fclose($inF);
?>

The result is the same and error log shows

PHP Warning: Wrong parameter count for strstr() in /index.php on line 11

Line 11:

$filename = strstr($file, '.', true);

Arcticwarrio
08-01-2012, 02:02 AM
just put an if in to see if the files names match

ska_defender
08-01-2012, 08:38 AM
just put an if in to see if the files names match

you mean before line 11 ?

Arcticwarrio
08-01-2012, 09:05 AM
you need to suppress warnings in your PHP ini file

this should do what you want:


<?php
$path = array("./files/","./images/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");

$folder = opendir($path[1]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[2]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (strstr($file2, '.', true) == strstr($file, '.', true)){
$imagename = $file2;
}
}
$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\nimage: \"$imagename\"\n\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);

fclose($inF);
?>

ska_defender
08-01-2012, 09:14 AM
It gives the following result

{
levels: [
{ file: "sintel_trailer3.jpg" }
],

image: ""

title: "sintel_trailer3.jpg"
},
{
levels: [
{ file: "big_buck_bunny_trailer.jpg" }
],

image: ""

title: "big_buck_bunny_trailer.jpg"
},


image is empty and image file is written in file and title:
It should be like

{
levels: [
{ file: "path to/files/filename1" }
],
image: "path to/images/imagefilename1",
title: "filename2"
},

Arcticwarrio
08-01-2012, 11:34 AM
think i've fixed it now :)


<?php
$path = array("./files/","./images/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");

$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (strstr($file2, '.', true) == strstr($file, '.', true)){
$imagename = $file2;
}
}
closedir($folder2);
$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);

fclose($inF);
?>

gives me this:


{
levels: [
{ file: "on.html" }
],
image: "on.gif"
title: "on.html"
},

ska_defender
08-01-2012, 01:09 PM
Yes getting close now but the image file is same for evry other file
in files folder I have two video files
sintel_trailer1.mp4
sintel_trailer2.mp4

and in images folder I have two images
sintel_trailer1.jpg
sintel_trailer2.jpg

and its giving me this result



{
levels: [
{ file: "sintel_trailer1.mp4" }
],
image: "sintel_trailer2.jpg"
title: "sintel_trailer1.mp4"
},
{
levels: [
{ file: "sintel_trailer2.mp4" }
],
image: "sintel_trailer2.jpg"
title: "sintel_trailer2.mp4"
},



The image file is same for every loop "sintel_trailer2.jpg"
whereas file name is changing accordingly

Arcticwarrio
08-01-2012, 01:25 PM
i made those files to test and i get



{
levels: [
{ file: "sintel_trailer1.mp4" }
],
image: "sintel_trailer1.gif"
title: "sintel_trailer1.mp4"
},
{
levels: [
{ file: "sintel_trailer2.mp4" }
],
image: "sintel_trailer2.gif"
title: "sintel_trailer2.mp4"
},

Arcticwarrio
08-01-2012, 01:26 PM
you could try changing


$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
fwrite($inF,$result);

to:


$result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
$imagename ='';
fwrite($inF,$result);

ska_defender
08-01-2012, 02:42 PM
Are you sure you are getting the exact result that I need ?

because I changed the code you specified as well but still the same result

what could be the issue ?
my index.php(which contains the code) is not in the root
root/dir/index.php
root/dir/files/
root/dir/images

this is the structure

ska_defender
08-01-2012, 08:51 PM
Can u please recheck the codes you tried and you pasted here ?
I am getting the same result

Arcticwarrio
08-02-2012, 12:36 AM
Can u please recheck the codes you tried and you pasted here ?
I am getting the same result

i tried it on my home pc and got the same result:


{
levels: [
{ file: "sintel_trailer1.mp4" }
],
image: "sintel_trailer1.gif"
title: "sintel_trailer1.mp4"
},
{
levels: [
{ file: "sintel_trailer2.mp4" }
],
image: "sintel_trailer2.gif"
title: "sintel_trailer2.mp4"
},

ska_defender
08-02-2012, 02:20 AM
error log shows

Warning: Wrong parameter count for strstr() in index.php on line 15

Line 15

if (strstr($file2, '.', true) == strstr($file, '.', true)){

AndrewGSW
08-02-2012, 05:25 AM
The third parameter was added in PHP version 5.3.0. Perhaps you are using an earlier version.

ska_defender
08-02-2012, 09:33 AM
The third parameter was added in PHP version 5.3.0. Perhaps you are using an earlier version.

so is there no alternative for older php version ?
My php version is 5.2.17

ska_defender
08-02-2012, 09:57 AM
instead of strstr I am using the following for line 11

if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){

ska_defender
08-02-2012, 10:06 AM
AndrewGSW,
The above modified code works and gives me the following result

{
levels: [
{ file: "b.mp4" }
],
image: "b.jpg"
title: "b.mp4"
},

{
levels: [
{ file: "c.mp4" }
],
image: "c.jpg"
title: "c.mp4"
},

But I need complete path to the file and image with url
like this

{
levels: [
{ file: "http://mysite.com/files/b.mp4" }
],
image: http://mysite.com/images/b.jpg"
title: "http://mysite.com/files/b.mp4"
},

{
levels: [
{ file: "http://mysite.com/files/c.mp4" }
],
image: "http://mysite.com/images/c.jpg"
title: "http://mysite.com/files/c.mp4"
},

Thanks for the help so far

Arcticwarrio
08-02-2012, 11:58 AM
try



<?php
$path = array("./files/","./images/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");

$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
$imagename = $file2;
}
}
closedir($folder2);
$result="{\nlevels: [\n{ file: \"$path2[0]$file\" }\n],\nimage: \"$path2[1]$imagename\"\ntitle: \"$path2[0]$file\"\n},\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);

fclose($inF);
?>

this gives me:
i stared out my server


{
levels: [
{ file: "http://***/files/sintel_trailer1.mp4" }
],
image: "http://***/images/sintel_trailer1.gif"
title: "http://***/files/sintel_trailer1.mp4"
},
{
levels: [
{ file: "http://***/files/sintel_trailer2.mp4" }
],
image: "http://***/images/sintel_trailer2.gif"
title: "http://***/files/sintel_trailer2.mp4"
},

ska_defender
08-02-2012, 03:16 PM
Yes finally its working according to the specs
Thanks
great!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum