I am having some trouble coding a PHP script to update only when the value in a .txt file changes. What happens is that a VB form can change the value in the .txt file and the PHP script displays an image based on the value in the .txt file. Currently the script will show the updated files if I manually hit refresh on the browser, but I would like to eliminate that. I have tried using the meta refresh tag but because I am rendering images the page is very "jolty" when it refreshes every 2 or so seconds and makes viewing the page unbearable. What I have tried to do is create a loop between the $string and $string2 variables so that when they are not equal it redirects the page to a page that redirects back to this page and does a "back-door" refresh. $string is what is the direct value in the .txt file and it defined in the loop whereas $string2 is defined outside of the loop. I would the loop to see the difference between them once a different value is inputted to the .txt file.
Thank you all in advance.
<?php
{
$file=fopen("counter.txt","r");
$string=fgetc($file);
fclose($file);
}
$string2 = $string;
if ($string == '1') {
$files = glob('tg.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
elseif ($string == '2') {
$files = glob('tr.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
elseif ($string == '3') {
$files = glob('wua.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
elseif ($string == '4') {
$files = glob('wur.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
elseif ($string == '5') {
$files = glob('stop.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
elseif ($string == '6') {
$files = glob('base.jpg');
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}}
while ($string == $string2) {
$file=fopen("counter.txt","r");
$string=fgetc($file);
echo $string;
fclose($file);
}
header('Location: redirect.php');
exit;
?>
PHP only runs in runtime, so there is no chance you can do what you just described.
Your best bet is to create a very small PHP script that does the check and have an AJAX script on the frontend check every second and update the image if PHP returns that it has been updated.
You are trying to use a server-side script to perform something on the client side.
Have your client page include a small Javascript snippet which posts back to your server to a script which can perform the check. Then, if your script indicates that a change had occured, refresh the page using Javascript.
That way, the page will only refresh when needed, and you can easily separate your client and server code.
Related
I have got a directory which contains several folders with images. What I want to do is to read all the images using php script and showing them with div in a browser. The code I have tried to implement is the following:
<?php
function listFolderFiles($dir)
{
echo '<ol>';
foreach (new DirectoryIterator($dir) as $folder) {
if (!$folder->isDot()) {
// echo '<li>' . $folder->getFilename() . '<br>';
if ($folder->isDir()) {
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if ($file != '.' && $file != '..') {
// echo '<li>' . $file->getFilename();
$image = $user . '/' . $file;
echo $image . '<br>';
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
}
}
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('images');
The command echo $image.'<br>'; prints the names of all images. With the following command I manage to create div, however without the images been displayed inside them:
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
Am I doing something wrong? The $image paths are correct.
EDIT: I move the folder of images in the same folder with the php file in the wamp folder. Now for example the image file is the following images/01virarias/1_.jpg. I change the call of my function as listFolderFiles('images');. However I am getting the same empty divs.
You need to use an URL. It seems you are using local files. Your browser is the 'problem'. Although it is a matter of security.
The images should be in your server environment.
And it seems you forgot a <li> start tag.
if your path is correct do a inspect element on your browser i think it's quoting issue try
echo '<img src="'. $image .'" width="500" height="500" alt=""/>';
You can use images path relative/absolute like
relative :- ../images/your_image
absolute :- http://localhost/your_image_path
Sometimes, I have to delete multiple files in different folders on the server. It's a tedious job to to this one by one via FTP. I wrote a little script that does the job. I can import the list of files in Excel and copy and paste them in the script. However, my approach is not elegant:
$file1 = "some-file.php";
$file12 = "some-file2.php";
...
if (!empty($file1)) {
if ( #unlink ( $_SERVER[DOCUMENT_ROOT]."/".$file1 ) )
{
echo 'The file <strong><span style="color:green;">' . $file1 . '</span></strong> was deleted!<br />';
}
else
{
echo 'Couldn't delete the file <strong><span style="color:red;">' . $file1 . '</span></strong>!<br />';
}}
if (!empty($file2)) { ...
I would rather like to do this with a foreach and an array, but don't know how. Any help would be appreciated!
Just put your files into an array and loop it.
$files = array('some-file.php', 'some-file2.php');
foreach ($files as $file) {
if ( #unlink ( $_SERVER[DOCUMENT_ROOT]."/".$file ) ) {
echo 'The file <strong><span style="color:green;">' . $file . '</span></strong> was deleted!<br />';
} else {
echo 'Couldn\'t delete the file <strong><span style="color:red;">' . $file . '</span></strong>!<br />';
}
}
also, i think its better if you use file_exists()
if (file_exists($file1))
{
unlink ( $_SERVER[DOCUMENT_ROOT]."/".$file1 );
clearstatcache();
}
I want to show my visitors the images in a folder and after then have seen it, I want all those files deleted!
This is what I tried, but It won't work. I think it's because PHP is generating a html file which tells the browser it must first get an image from a different place but the html file was already removed.
<?php
foreach (glob("files/*.*") as $prevpic) {
echo '<img src="' . $prevpic . '" />';
}
foreach (glob("files/*.*") as $file) {
unlink($file);
}
move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]);
?>
You can do something like so ...
<?php
foreach (glob("files/*.*") as $file) {
echo '<img src="data:image/' . pathinfo($file, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($file)) . '" />';
unlink($file);
}
?>
... which is basically writing the image data into the html, and then discarding the image.
I would handle this by simply managing your images through a download script (php).
You track sessions and simply don't display the images requested, but fail gracefully with a response, or let your app handle it via session based tracking.
That way no images are deleted 'onview'.
i have this code to display images, where each user has his own, i'll comment it to save your time
<?php
session_start();
$name=$_SESSION['valid_user']; //saved current username in variable
$loc="./uploads/"; //location of image directory
$path=$loc.$name; //current user's folder to save his images
echo $path."<br>"; //i used this to make sure the path is ok, only for testing
if(is_dir($path)) //if directory exists, show it exists,otherwise show it
{ //doesnt exists
echo "<br>exists";
}
else
{
echo "<br>not exists";
}
$files = glob($path."/");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i]; //picture number
print $num."<br />";
echo '<img src="'.$path.'" alt="random image" height="100" width="100"/>'."<br /><br />";
} //shows the picture till the last one
?>
the output that i get is this this
./uploads/user_name
exists
but it does not show the images, even though the folder is not empty (upload script works fine).
EDIT; solved it (low rep, cant answer my own question).
got it. For anyone who cares, this line here
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
wasn't working because i added $files, which already contained the path, and it was giving input to img src as
/uploads/username/uploads/username
so that was two times the same path.Upon removing $path, and using just
<img src="' . $files[$i] . '"
did the trick. Thank you all for your help.
I think you need to pass a wildcard path to glob: glob($path . '/*'). You are also not printing the filename in the image source attribute:
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
Also, your $num is actually the filename, not the picture number - that is $i. You could really simplify that loop using the foreach construct:
foreach($files as $filename) {
// etc
}
you need to add a pattern for using glob afaik
$files = glob($path."/*.*"); // all files
$files = glob($path."/*.jpg"); // all jpgs etc.pp
foreach($files as $idx => $file)
{
$num = $idx+1; //idx starts with 0 so we add one here
print $num."<br />";
echo '<img src="'.$path.'/'.$file'" alt="random image" height="100" width="100"/>'."<br /><br />";
}
I have a text file in the same folder as the script I'm trying to run. It has several URL links each on a new line like so:
hxxp://www.example.com/example1/a.doc
hxxp://www.example.com/example2/b.xls
hxxp://www.example.com/example3/c.ppt
I'm trying to link these files but it only lists the last file in the list.
Here is my code:
<?php
$getLinks = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/links.txt');
$files = explode("\n", $getLinks);
foreach ($files as $file) {
if (substr($file, 0, 23) == 'hxxp://www.example.com/') {
$ext = pathinfo(strtolower($file));
$linkFile = basename(rawurldecode($file));
if ($ext['extension'] == 'doc') {
echo '<img src="images/word.png" /> ' . $linkFile . '<br />';
} elseif ($ext['extension'] == 'xls') {
echo '<img src="images/excel.png" /> ' . $linkFile . '<br />';
} elseif ($ext['extension'] == 'ppt') {
echo '<img src="images/powerpoint.png" /> ' . $linkFile . '<br />';
}
}
}
?>
*note: I've tried using the file function as well with the same results.
You can improve this code in many ways:
Use file instead of file_get_contents to have the lines put into an array automatically
Use strpos instead of substr -- more efficient
Use strrpos to get the file extension -- faster and foolproof, as know exactly how it behaves
You should be using rawurlencode instead of rawurldecode because you are creating urls, not reading them
The if conditionals for the extensions should be replaced by an array lookup
Making all these changes, we have:
$lines = file($_SERVER['DOCUMENT_ROOT'] . '/links.txt');
$extensions = array(
'doc' => 'word.png',
'xls' => 'excel.png',
'ppt' => 'powerpoint.png',
);
foreach ($lines as $file) {
if (strpos($file, 'hxxp://www.example.com/') !== 0) {
continue;
}
$ext = strtolower(substr($file, strrpos($file, '.') + 1));
if (empty($extensions[$ext])) {
continue;
}
printf('<img src="images/%s" /> %s<br />',
$file, $extensions[$ext], rawurlencode(basename($file)));
}
$getLinks = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/links.txt');
$files = explode("\r\n", $getLinks);
I'm assuming you're on windows, the same as me.
\n isn't the whole windows new line character Use \r\n
When I replaced \n with \r\n it worked as expected