I'm trying to add a simple random image of the day script. It doesn't point to the right file location though. Here is the code:
<?php
$i=0;
$path="images";
$ext = "jpg";
$extra= "alt=\"Random Image\" float=\"left\"";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if (substr($file,strlen($file)-3,3)==$ext)
{ $imgs[$i++]=$file;
}
}
closedir($handle);
$today=getdate();
srand($today['mday']+$today['month']+$today['year']);
$r=rand(0,$i-1);
echo("<img src=images/\"$imgs[$r]\" $extra>");
}
?>
The image source when I right click and check properties is adding %22 before and after the file. EX: mobile/images/%22image.jpg%22
Therefore, the image isn't showing because of it I presume.
That %22 is actually that superfluous " inside your url source, remove those:
echo "<img src=\"images/{$imgs[$r]}\" alt=\"Photo\" />";
^ opening ^ closing
Related
I am very inexperienced with PHP. I have a folder on a website which will receive files named with a numeric date structure. I need href links to be created for the files which are dropped into this folder. I need the link text to be formatted by with a textual month and numeric day. For example:
File name "01-02-22 BULLETIN.pdf" becomes
<href="bulletins/2022/$file">January 2</a>.
I have been messing with the code below from this post PHP to create Links for files in a folder, but I can't seem to figure it out. The links are appearing but any date formatting I attempt returns "error Call to a member function format()."
Thanks for your help!
<?php
$dir = "bulletins/2022/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "<a href=bulletins/2022/$file>$file</a><br>";
}
closedir($dh);
}
}
?>
The html mark up is missing quotes
Here an example:
$some_path = "bulletins/2022";
$readdir=opendir($some_path);
while ($file = readdir($readdir))
{
if($file != '..' && $file !='.' && $file !='' && $file != ".htaccess")
{
echo "$file<br>"; // yours is missing the quotes which should always be escaped in php with a backslash
}
}
closedir($readdir);
You could also concider using php time(); as the filename which will give you a unix timestamp which can then be sorted and converted into a date and time.
This example will sort the files in a natural order
$some_path = "demo_track_previews";
$readdir=opendir($some_path);
while ($file = readdir($readdir))
{
if($file != '..' && $file !='.' && $file !='' && $file != ".htaccess")
{
if(!empty(file))
{
$all_files[] = $file; //put all files in array ready to sort
}
}
}
closedir($readdir);
if(!empty($all_files))
{
sort($all_files,SORT_NATURAL); //You can also try rsort($all_files,SORT_NATURAL);
foreach ($all_files as $key => $each_file)
{
echo "".$each_file."<br>";
}
}
EDIT: after comment
if you need just the date from the filename "01-02-22 BULLETIN.pdf"
then try this
foreach ($all_files as $key => $each_file)
{
$file_name_parts = explode(" ", $each_file); // seperate the filename where there is a space - $file_name_parts[0] will be 01-02-22
echo "".$file_name_parts[0]."<br>";
}
im making a photo gallery and i want to make a search bar which displays all the images that have the searched keyword in their name. i have stored my images in a folder (NOT DATABASE, IN A FOLDER).
i have made the search bar but i cant get the PHP part working. do i need ajax or jquery ?
this is my current code which returns nothing when i press search. not even the test " eco 'hello' " part.
?php
$dir = "/uploads";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['searching']){
// replace this line with eco <img> line
echo(''. $file .''."\n");
echo "hello" ;
}
}
closedir($dh);
}
}
?>
i want to get a list of the images with keyword (searching) in the name.
It could be as simple as replacing if($file == $_POST['searching']){ with if (false !== strpos($file, $_POST['searching'])){.
You could use scandir() to get an array of files in the directory and then loop through it:
$dir_array=scandir($dir);
foreach($dir_array as $file){
if($file == $_POST['searching']){
// open file etc. here
echo(''. $file .''."\n");
}
}
I'm working on a small script and I want to list the contents of a directory, make them into hyperlinks, and then edit those hyperlinks to look pretty (I.e. not show an ugly super long path name), then limit the number files echoed back to the browser. Also, I need the most recent files echoed back only.
I was thinking about using this:
<?php
$path = "/full/path/to/files";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$files .= ''.$file.'';
}
}
closedir($handle);
}
?>
or this:
<?php
$sub = ($_GET['dir']);
$path = 'enter/your/directory/here/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
if (substr($file, -4, -3) =="."){
echo "$i. $file <br />";
}else{
echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
}
$i++;
}
}
closedir($dh);
?>
But I dont want to list the files like this:
C:/example/example2/Hello.pdf
I want to edit the variable. Is that possible? To make it say something as simple as "Hello."
I want to limit the amount of files listed as well. For example: only list the first 5 files, or last 5, etc. Is there a function or some kind of parameter for that?
I appreciate any help or push in the right direction. Thanks
I'm on my phone so providing a code example will be tough. Why not iterate through the directories, storing the file name in an array, with the absolute path as the value for that key?
EDIT: You can use basename to aid you in doing this.
Every 72 hours I upload a new PHP file to my server. (well actually it is an xml file transformed on the server with php) Is there a method to create a link on an html page that links to the "new" PHP doc automatically everytime a new file is uploaded?
I don't want to manually change the link every 72 hours. I would ultimately like to have an html page with a list of links to every new doc that is uploaded. I found this for images but I need someting like this but for PHP files and links.
http://net.tutsplus.com/articles/news/scanning-folders-with-php/
Any help would be very appreciated.
I found a solution that add links to the xml files. Now I just need to figure out how to add a link to reference the xslt sheet for each new xml file that is upload AUTOMATICALLY. I am not sure how to do this but any help would be very helpful. Thanks for everyones help.
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("".$file."<br />\n");
}
}
echo '<br /><br />Return';
closedir($handle);
}
?>
To read in a directory of files and then sort them by upload time you can just use:
$files = glob("files/*.xml");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
print "link: " . current($files); // make that an actual <a href=
You can do that pretty easily with PHP function readdir:
http://php.net/manual/en/function.readdir.php
Simply loop through the files in the directory where you upload files and have php output a link for each.
ie:
<?php
if ($handle = opendir('/path/to/upload_dir')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo '' . $file . '<br />';
}
}
closedir($handle);
}
?>
You'll need to edit the http:// URL on the href to point to the correct download URL for your server, as well as the server path for opendir.
Hope that helps.
list by filetype
<?php
if ($handle = opendir('/path/to/dir')) {
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php',1)||strpos($file, '.xml',1) ) {
echo "<p>$file</p>";
}
}
closedir($handle);
}
I have created this php script which displays the contents of a designated directory and allows users to download each file. Here is the code:
<?php
if ($handle = opendir('test')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href='test/$file'>$file\n</a><br/>";
}
}
closedir($handle);
}
?>
This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.
What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).
Would you know how to achieve this?
Don't create a link to the directory itself, but to a php page which displays the contents.
Change your php code to somthing like:
if(isset($_REQUEST['dir'])) {
$current_dir = $_REQUEST['dir'];
} else {
$current_dir = 'test';
}
if ($handle = opendir($current_dir)) {
while (false !== ($file_or_dir = readdir($handle))) {
if(in_array($file_or_dir, array('.', '..'))) continue;
$path = $current_dir.'/'.$file_or_dir;
if(is_file($path)) {
echo ''.$file_or_dir."\n<br/>";
} else {
echo ''.$file_or_dir."\n<br/>";
}
}
closedir($handle);
}
PS write you html code with double quotes.
You need your HREF to point back to your PHP script, and not the directory. You will then need to update your PHP script to now which directory it needs to read.