Jump to content

Can't get file


11Tami

Recommended Posts

Hello, I'm trying to get a file per second or minute on a web site without sessions or cookies if possible. For testing I am trying per second. No file in the array is being included on a second. Can I number the items listed in the array and still keep them inside the array()? So it will grab one of them depending on what second it is? If so, how is that done? Thanks very much!

 

<?php
$currentfile = date("s");
$this=array("file1.txt","file2.txt");
$files=count($this);
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/
Share on other sites

Here it is again, I had to put a "0" after file for seconds but still not working.

 

$currentfile = date("s");
$this=array("file01.txt","file02.txt");
$files=count($this);
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303948
Share on other sites

use file_get_contents

 

Note: Type the full path if the file is not inside the root directory.

<?php
$file1= file_get_contents('file1.txt');
$file2= file_get_contents('file2.txt');

$currentfile = date("s");
$this=array($file1,$file2);
$files=count($this);
$parts = explode(",", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303952
Share on other sites

It looks like you're trying to include "2[01]" (if the clock is at 01 seconds).

You $files variable is the count of array elements. If you have a file for each second, I think that code is a bit more complicated than it needs to be. How about the following?

<?php

$file = "file" . date('s') . ".txt";

include($file);

?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303957
Share on other sites

Thanks a lot evil, I'm getting this on the include:

It says $currentFile in this line is undefined

$parts = explode(",", $files[$currentFile]);

which is keeping this from opening. {include($files[$currentFile]);}

 

I added this to the top of the script to help with errors:

ini_set('error_reporting', 8191);

ini_set('display_startup_errors', 1);

ini_set('display_errors', 1);

 

Reason I'm trying to do it this way Markjoe is because I need the items in the array because I'll be working with more stuff for the array. Right now just trying to get the seconds or minutes working with the items listed in an array. Anyone know why $currentFile here is undefined in deadevils code? Thanks

 

$parts = explode(",", $files[$currentFile]);

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303969
Share on other sites

I don't think it likes it because the explode is trying to sort the items in the previous array and they are not there to sort.

 

Anyone know how to add

$file1 = file_get_contents('file1.txt');

$file2 = file_get_contents('file2.txt');

for the array instead, without using the explode and count()... Either that or put them back in the array as they were before, and just get the file to open? Thanks

 

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$file1 = file_get_contents('file1.txt');
$file2 = file_get_contents('file2.txt');
$currentfile = date("s");
$files=count($file);
$parts = explode(",", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303977
Share on other sites

The count() is counting the items in the array and the explode() is sorting them all at the end. But I'll do it another way if it works. The error this gives says it doesn't like this

$currentfile = date("s"); 

And I don't know why, all it is saying is get something by seconds and it usually works. If I have files on my own site and not anybody elses, do I still need

file_get_contents 

for the files? Thanks, the following code says it doesn't like this 

$currentfile = date("s"); 

 

<?php 
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$currentfile = date("s");
$this=array("file01.txt","file02.txt");
$files=count($this);
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-303980
Share on other sites

Thanks for trying, it almost worked and it tried to open the correct text files, but this 

$parts = explode(".", $files[$currentFile]);

still says undefined $current file and I don't know why.

Here's where its at now.

 

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1); 
$currentfile = date("s");
$file1 = file_get_contents('file1.txt');
$file2 = file_get_contents('file2.txt');
$this = array($file1,$file2);
$files=count($this);
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304002
Share on other sites

Really appreciate all you wonderful help deadevil, this is very interesting.

 

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1); 
$currentFile = date("s");
$file1 = file_get_contents('file01.txt');
$file2 = file_get_contents('file02.txt');
$file3 = file_get_contents('file03.txt');
$file4 = file_get_contents('file04.txt');
$file5 = file_get_contents('file05.txt');
$file6 = file_get_contents('file06.txt');
$file7 = file_get_contents('file07.txt');
$file8 = file_get_contents('file08.txt');
$file9 = file_get_contents('file09.txt');
$file10 = file_get_contents('file10.txt');
$this = array($file1,$file2,$file3,$file4,$file5,$file6,$file7,$file8,$file9,$file10);
$files=count($this);
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}
?> 

This is now working with no error messages, as long as I have all those .txt files uploaded to the root, it takes away all error messages. But, now there are no error messages, but also no files appearing. I uploaded more files to make sure I could refresh the page for several seconds and see if anything appeared. But nothing appearing. It seems like it should be working now but nothing at all there. Not sure why. I only get an error code that says the files are missing if I remove any of them online, other than that no errors at all. Thanks

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304012
Share on other sites

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1); 
$currentFile = date("i");
$file1 = file_get_contents('file01.txt');
$file2 = file_get_contents('file02.txt');
$file3 = file_get_contents('file03.txt');
$file4 = file_get_contents('file04.txt');
$file5 = file_get_contents('file05.txt');
$file6 = file_get_contents('file06.txt');
$file7 = file_get_contents('file07.txt');
$file8 = file_get_contents('file08.txt');
$file9 = file_get_contents('file09.txt');
$file10 = file_get_contents('file10.txt');

$allfiles = array($file1,$file2,$file3,$file4,$file5,$file6,$file7,$file8,$file9,$file10);
$file = explode(",", $allfiles);
#echo $file[0]; // $file1
#echo $file[1]; // $file2
#....
foreach($file as $data):
$ext = substr($data, -3);
#$ext = array_pop($file);

if ($ext == "txt") include($data[$currentFile]);
break;

endforeach;

/*$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);
}*/
?> 

 

tell me what will happen..

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304033
Share on other sites

I was wondering if it was having a hard time finding each file, but what does the echo do, should I move the echo up right above $all files or should they stay where they are? I added them all to it, it makes it all bigger.

 

The error message says unexepected Telse here

elseif ($ext == "html")include($data[$currentFile]);

The reason I was including the explodes and pops, so that I could include an html file with it, unless there's another way.

 

<? php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1); 
$currentFile = date("i");
$file1 = file_get_contents('file01.txt');
$file2 = file_get_contents('file02.txt');
$file3 = file_get_contents('file03.txt');
$file4 = file_get_contents('file04.txt');
$file5 = file_get_contents('file05.txt');
$file6 = file_get_contents('file06.txt');
$file7 = file_get_contents('file07.txt');
$file8 = file_get_contents('file08.txt');
$file9 = file_get_contents('file01.html');
$file10 = file_get_contents('file10.txt');
$allfiles = array($file1,$file2,$file3,$file4,$file5,$file6,$file7,$file8,$file9,$file10);
$file = explode(",", $allfiles);
#echo $file[0]; // $file1
#echo $file[2]; // $file2
#echo $file[3]; // $file3
#echo $file[4]; // $file4
#echo $file[5]; // $file5
#echo $file[6]; // $file6
#echo $file[7]; // $file7
#echo $file[8]; // $file8
#echo $file[9]; // $file9
#echo $file[10]; // $file10
foreach($file as $data):
$ext = substr($data, -3);
#$ext = array_pop($file);
if ($ext == "txt") include($data[$currentFile]);
break;
elseif ($ext == "html")
include($data[$currentFile]);
break;
endforeach;
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt")
{include($files[$currentFile]);}
elseif ($ext == "html")
{include($files[$currentFile]);}?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304324
Share on other sites

Seriously, I am still not sure I get the purpose, but I can be OK with that.

It just makes it harder to solve.

I believe you said earlier that you just need to include a file based on the current second or minute returned by the clock. And the include files had to be listed in an array.

Here's what I got:

<?php
$currentFile = date('s');

echo "current second according to server clock: <b>$currentFile</b><br>\n"; //for debugging

$allfiles = array('file01.txt','file02.txt','file03.txt','file04.txt','file05.txt','file06.txt','file07.txt','file08.txt','file09.txt','file10.txt');

foreach($allfiles as $x){
if(substr($x,4,2)==$currentFile){
	include($x);
	break;
}
}
?>

 

The files in the array are in the same directory as the script file.

The script relies on the seconds/minutes matching with the 5th and 6th character of the file name.

I didn't understand if you wanted to have some txt and some html, but only 1 file per clock result.

Or, if there may be a txt and html per clock result, and you need to be able to specify which to include.

 

Is this at least close to what your trying to do? A base to start from perhaps?

 

The script is hosted at:

http://nascentdv.com/includetest/includetest.php

Remember, it is currently only set up for seconds 1-10.

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304524
Share on other sites

Yes, I saw you were trying to include html files also. In my example you could easily substitute some of the txt files for html files.

The include statement does care what type of file it is, it will just include whatever is in the array and matches the 5th and 6th characters to the clock.

 

BTW, DeadEvil's code earlier $currentfile was undefined because it was defined as $currentFile.

 

If you use file_get_contents(), you now have the contents for the file in a string variable. It doesn't make sence to then include it. You already have the contents of the file, you are now trying to issue the contents of the file to the include statement.

Either use file_get_contents, then echo the contents. Or dont use file_get_contents() and just include it.

 

When you were checking the extention in previous code, it was flawed. You were setting $ext with substr($data,3), but then testing it for "html". $ext was only set to 3 characters, it will never be "html". Also you were looping through the array of files, checking for $ext to match. The 3 vs 4 characters thing aside, that foreach will match the first file of each type and stop. It also does the same thing for both branches, this defeats the purpose of conditional branching.

 

I am really not trying to be a jerk, but the code you've been posting is flawed in theory, not just in syntax. Basically, it will never work. I hope this makes some sence to you.

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-304804
Share on other sites


<? php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1); 
$currentFile = date("i");
$file1 = file_get_contents('file01.txt');
$file2 = file_get_contents('file02.txt');
$file3 = file_get_contents('file03.txt');
$file4 = file_get_contents('file04.txt');
$file5 = file_get_contents('file05.txt');
$file6 = file_get_contents('file06.txt');
$file7 = file_get_contents('file07.txt');
$file8 = file_get_contents('file08.txt');
$file9 = file_get_contents('file01.html');
$file10 = file_get_contents('file10.txt');
$allfiles = array($file1,$file2,$file3,$file4,$file5,$file6,$file7,$file8,$file9,$file10);
$file = explode(",", $allfiles);
#echo $file[0]; // $file1
#echo $file[2]; // $file2
#echo $file[3]; // $file3
#echo $file[4]; // $file4
#echo $file[5]; // $file5
#echo $file[6]; // $file6
#echo $file[7]; // $file7
#echo $file[8]; // $file8
#echo $file[9]; // $file9
#echo $file[10]; // $file10
foreach($file as $data) {
$ext = substr($data, -3);
#$ext = array_pop($file);
if ($ext == "txt") {
include($data[$currentFile]);
break; 
}
else if ($ext == "html") {
include($data[$currentFile]);
break;
}
}
$parts = explode(".", $files[$currentFile]);
$ext = array_pop($parts);
if ($ext == "txt") {
include($files[$currentFile]);
}
elseif ($ext == "html") {
include($files[$currentFile]);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-305189
Share on other sites

Ok this code is tested unfortunately there's a little issue about the file name

you can get not greater than 60 files and the file name must be named as 01.ext,02.ext,03.ext ... .

 

Hope it will help..

 

<?php
$currentFile = date('s');

echo "current second according to server clock: <b>$currentFile</b><br>\n";

$allfiles = array('01.txt','02.css','03.html');

foreach($allfiles as $file):

if(preg_match("/".$currentFile."\.[A-Za-z]/","/".$file."\.[A-Za-z]/")):
	$file_content = file_get_contents($file);
	break;
endif;
endforeach;

echo $file_content; exit();
?>

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-306088
Share on other sites

Thanks again for the help deadevil and finding that other code Markjoe, looks like its working better for all of us. 60 is great for seconds.

 

I studied all day today about this: if(preg_match("/".$currentFile."\.[A-Za-z]/","/".$file."\.[A-Za-z]/")):

and I'm stumped.

 

Deadevil can you tell me a little more about it? I don't want to have to rename all my files.

The part I am not understanding is all these "/" is that to avoid parsing errors for pulling the web site adress properly or something? At any rate, its getting very close now. Once I understand why you set up preg_match that way, maybe I can help find a solution for the file names.

 

http://www.devpapers.com/article/46

 

 

Link to comment
https://forums.phpfreaks.com/topic/61064-cant-get-file/#findComment-307574
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.