OPENDIR the value from $_REQUEST - php

anybody can help to solve this problem. i have 2 files php :
file list folder which send $_REQUEST as Link
example : http://localhost/sertifikat/bacafolder.php?Link=%20Cipto%2075%20HGB%201159%20207%20M2
file php for execute and OPENDIR that $Link.
but its can't work, even though that Folder is exist
this is the second file php :
<?php
$Link=$_REQUEST['Link'];
$dir= $Link;
echo $dir;
if($opendir=opendir($dir))
{while(($file=readdir($opendir)) !=FALSE)
{
echo "<img src='$dir/$file'><br>";
}
}
?>

Related

Why is file_exists() returning false when the file exists?

I was trying to use google APIs, and on process, I got an error, saying file is missing.
On trying to access the file, which exits, file_exists() returns false, no idea where I'm going wrong.
Note: It is in local host, using Xampp.
The result on execution:
Image of the directory:
File.php:
<?php
clearstatcache();
$dir= __DIR__."\client_secret.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
Please help me find where I have gone wrong.
Do a dir on that folder from dos and check that your file is not named
client_secret.json.json
Windows file explorer displays the name of the file without the extension. There for your file name is client_secret.json with the extension of .json
<?php
clearstatcache();
$dir= __DIR__."\client_secret.json.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
Because your File.php and client_secret.json is located in same file It will work.
try this
<?php
clearstatcache();
$dir= "client_secret.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
As client_secret.json file is in the same path as file.php, you can directly do
if(file_exists("client_secret.json")){ //file name without whole path
echo "Exists";
}else{
echo "Not Found!";
}
where is the file.php path ?
__DIR__will find the file.php directory ,so you can echo the $dir value , and then check the path is ok ?

Calling external php function in <img>

I've created an external php file (sort.php) that sorts the files in a folder on the server by time modified, and returns the most recent file.
<?php
function scanDir ($dir){
$fileTimeArray = array();
// Scan directory and get each file date
foreach (scandir($dir) as $fileTime){
$fileTimeArray[$fileTime] = filemtime($dir . '/' . $fileTime);
}
//Sort file times
var $latestFile = arsort($fileTimeArray);
return($latestFile[0]);
}
?>
I'm attempting to call this function inside of , in another php file, and set the src:
<img <?php echo 'src="'.scanDir("issues/preview").'"';?>/>
I've included sort.php at the top of the page in question.
The image src reads "(unknown)". What am I missing or doing wrong or both?
Thank you!
I would write
<?php $x = scanDir("issues/preview"); ?>
<img src="<?php echo $x; ?>"/>

Why file_exists() function doesn't work correctly?

Here is my code:
<?php $filename = $var.'p_folder/'.sub_replace('?','',$page).'/images/default.png'; ?>
<img src = "<?php echo $filename; ?>"
title= "<?php echo file_exists($filename) ? 'exists' : 'not exist'; ?>"
>
My code shows the image as well, but file_exists() returns false (I mean "not exist" prints).. Why?
Actually that's pretty much odd for me .. because I can see the image on the web, so it means the image exists on the directory, but why file_exists() cannot find it?
file_exists() needs to use a file path on the hard drive, not a URL. So you should have something more like:
$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/abcd.jpg';
if(file_exists($thumb_name)) {
//your code
}
check your image path and then sever name & document root

Exchanging variables between two pages PHP

I am aware that this kind of question has already been asked before, but I have a slightly different case.
foreach($dir as $file)
{
$file = '<li>'.basename($file).'</li>';
echo $file;
}
This is my script to display files in a folder and link to them. The way it is now, I use the $_GET['file'] on the other page to receive the information on the other page. The other page is supposed to display a photo/video with the file that has been linked, however I don't know how to use the $_POST or $_SESSION in this case, since it's a loop and I don't want the information about the file be in the link.
Also, I don't want any forms. I want to click the link with the name of the file and the other website to already have the information about the file and display the video or image.
Use like this by storing the media path in session and use the corresponding index to pass.
First file:
<?php
$i = 1;
session_start();
$dir = array('1.jpg', '2.jpg', '3.jpg');
echo '<ul>';
foreach($dir as $file)
{ //echo $i.'---'.$file."<br />";
echo '<li>'.$file.'</li>';
$_SESSION['media_'.$i] = $file;
$i++;
}
echo '</ul>';
?>
test.php
<?php
session_start();
echo $_SESSION['media_'.$_GET['file']];
?>

PHP - Randomly grab a file from folder and echo

I have a folder on my server called /assets/includes/updates/ with .php files inside featuring static html content.
I'd like to randomly grab a file from this folder and echo it into a div. Here is what I have:
<?php
function random_update($dir = $_SERVER['DOCUMENT_ROOT'].'/assets/includes/updates/')
{
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
?>
<div class="my-div">
<?php echo random_update(); ?>
</div><!--end my-div-->
I am getting 500 errors? Also, my intention is to only echo 1 file at a time. Will the provided code accomplish that?
Php does not recognize the syntax you used. You have to bypass it like this:
<?php
function random_update($dir = NULL)
{
if ($dir === NULL) {
$dir = $_SERVER['DOCUMENT_ROOT'] . '/assets/includes/updates/';
}
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
Also, you might want to enable error dumping in your development environment so you know what went wrong next time.
Aside from another answers spotted issues, for your code to do what you want, you have to replace your following code:
<?php echo random_update(); ?>
for this one:
<?php echo file_get_contents (random_update()); ?>
because your current code will print the filename inside the div, while I think you wanted the actual content of the file to be inserted in the div.
You can't use any expression as "default" function's argument value.

Categories