I am using the following php code to echo out the names (and extensions) of all the files in a folder.
This works fine and I am producing the results in a table like so:
code:
<?php
$dir = new DirectoryIterator("data/uploads/");
foreach ($dir as $fileinfo) {
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}?>
result:
. Existing File Delete
.. Existing File Delete
Filename1.jp Existing File Delete
Filename2.jp Existing File Delete
Filename3.jp Existing File Delete
The problem is in my results I am also getting those weird dots at the top. I do not know why these are being shown as I only have 3 files in the directory and don't know where the dots have come from?
Can anyone please help with this? thanks
Add Condition to check that is it file or not.. try this code
if ($fileinfo->isFile()) { // add this condition
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}
Add condition and check if it is . or .. if it satisfies the condition continue your looop
Related
PHPmailer will not let me add an embedded image inside a foreach loop the path is correct but for some reason it is not adding the image i have checked other sorces on here but with no luck the images are embedding fine when out of the loop but it is just when i call them by using variable names eg the way i need to inside a for each loop as the images will be dynamic it will not embed any help appreciated.
The path looks all good but it just will not run in the loop?
Please let me know if you need to see any more code i just did not want to include irrelevant code as i know the message is already sending fine it is just this bit that i have a problem with. I have the table working in html that is not being sent via email if that is of any use outputting the images correctly in the foreach loop
<tbody>'.
'<span style="display:none">'. $i = 1;' .</span>'.
$total_quantity_count = 0;
$grand_total = 0;
foreach($items_purchased_array as $item){
if (!$mail->addEmbeddedImage(getcwd() . '/' . $item['image'],'product_pic'.$i)) {
echo 'Failed to attach '. getcwd() . '/' . $item['image'],'product_pic'.$i;
}
</tbody>
Here was what i did to fix this
if (!$mail->addEmbeddedImage(getcwd().'/'.trim($item['image']),trim('product_pic'.$i))){
echo 'Failed to attach '.getcwd().'/'.$item['image'],('product_pic'.$i);
I'm new with programming and learning now php. I have installed xamp and have write a little bit code. I have some images on my external hd and I want to show some pictures.
The problem is I can get a list of path of my images with this code:
echo "<html><body>";
$outerDir = "x:\map\maps\more\\";
$total = count (array_diff (scandir ($outerDir), Array (".", "..")));
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
foreach ($dirs as $d) {
if (!is_dir($outerDir . $d)) {
echo $d . "<br>";
}
}
but if I want to show it as an image, i can't show the image. I tried this in the foreach:
if (!is_dir($outerDir . $d)) {
$file = $outerhDir . $d;
echo 'img src="' . $outerDir . $d . '> <br>';
echo "<img src='" . $outerDir . '\\' . $d . "'alt='" . $d . "'> <br>";
}
But none of them work. I also tried the header and file_get_content like this:
if (!is_dir($outerDir . $d)) {
$file = $outerDir . $d;
header('Content-type: image/jpeg');
header('Content-Length:' . filesize($file));
$image = file_get_contents('$file');
echo $image . "<br>";
}
Even a simple html code wont work! Like this:
echo '<img src="x:\map\maps\more\needwonder.jpg"> <br>';
BUTTTT!! When I store one of the image in the same folder as my php file it will work! Like this:
echo '<img src="needwonder.jpg"> <br>';
But I dont want to put all my files in the same folder as the php file, because of security, technicaly, comfortable reasons AND I want to learn programming not to avoid issues or problems with my code.
So I hope I defined my problem good and hope that one of you guys and/or girls know the solution to help me out
If your image files aren't in a folder that's set up to be accessible by the web server, you can't display them on a web page. Simple as that. Put them in a web directory to use them.
It is possible to write a PHP script that proxies image files (kind of like what you're trying with file_get_contents()), but doing that requires a separate PHP file for the images, and it's best avoided unless absolutely necessary, as it can easily introduce security vulnerabilities, and performs poorly. If you do want to do that, though:
Link to the PHP script in place of an image, and pass it an identifier for the image as a parameter (e.g, <img src="img.php?image=blah">).
In that script, use that identifier to send the appropriate Content-Type, then use readfile() to output the image. (It's equivalent to echo file_get_contents(...), but more concise and performs better in some situations.)
MAKE ABSOLUTELY SURE that the parameter to that script cannot be manipulated to load a file that you didn't intend to make available. This includes trickery like ?image=../../wrong/file.
I've got a question I can see being asked all around the internet but I haven't seen an answer that would help me.
Long story short; I've got a PHP script that lists files in a directory (file name, ctime). Both file name & creation date work fine, but I'm struggling with file-size.
This is the code that lists the files in a specific folder:
//show only files with 'xml' extension
if ($files[$b] != "." && $files[$b] != ".." && $ext == 'xml') {
echo "<tr style=\"border: 1px solid gray; background-color:";
if ($even) {
echo "#fff5cc;\">";
} else {
echo "white;\">";
}
echo "<td>$files[$b]";
echo "(" . filesize($files[$b]) . ")";
//echo filesize($files[$b]);
echo "</td>";
echo "<td>" . date("F d Y H:i:s.", filectime(UPLOAD_FOLDER . "/" . $files[$b])) . "</td>";
}
$even = !$even;
}
?>
Key stuff happens in the line code that shows the filesize; PHP error is 'Stat Failed...'. I have to admit I have no idea why because I'm able to open the file (one of the functions actually opens the XML file and places its contents to a text-area).
As most answers are about permissions, I actually tried adding 777 permissions to a test file and 777 to the folder where the file is saved, but to no avail. Thanks in advance!
I believe you have forgotten to link full path to the file. Have a look what do you few lines below:
filectime(UPLOAD_FOLDER . "/" . $files[$b])
So, the same should go everywhere:
filesize(UPLOAD_FOLDER . "/" . $files[$b])
Commentts below:
Also, I suppose $b is an iterator. If it is, you don't need $even variable, because you can use modulo operator:
if($b%2==0)
//event
Using other variable is not problem here, however it's good to be aware of other possible solutions.
I'd also let you know that there's a glob function. You could use it to get the XML file list:
$list = glob(UPLOAD_FOLDER . "/*.xml");
I have a PHP code that displays the file contents of a directory in a list. Each file is linked so that if clicked, it will download or open. The files inside the directory will be files uploaded by customers. If the file name contains spaces, the link is broken and will not open, so I would like to have the spaces replaced with underscores.
I know that str_replace does what I'm looking for, but I'm not sure how to apply it to this code (which I did not write).
// Define the full path to your folder from root
$path = "uploads/artwork";
// Open the folder
$dir_handle = #opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "index.php" )
continue;
echo "<a href=uploads/artwork/$file>$file</a><br />";
}
// Close
closedir($dir_handle);
All help is greatly appreciated. Thank you!
You'd have to replace the filenames with underscores when you save them to the server, also.
Since you don't have the code on where you save the files, you can urlencode() the link URL so it will not be broken by hazardous characters. Note that originally it was broken by spaces because you didn't have the href value enclosed in quotes, which I'm doing here:
echo "<a href='" . urlencode("uploads/artwork/$file") . "'>$file</a><br />";
Otherwise, to replace spaces with underscores, you'd do:
echo "$file<br />";
But again, that would likely require you changing the file names on upload.
Note that you'll also want to call htmlentities() on the $file portion of that link, to prevent characters such as < from breaking the HTML page. So, the final result would be:
echo "<a href='" . urlencode("uploads/artwork/$file") . "'>" . htmlentities( $file) . "</a><br />";
I have a piece of php that if a specified folder has a file in, it will display the filename, date created and present a download button, if the folder is empty it will show nothing. This works very well but if I have more than one file in the folder it bunches all the filenames together - what I want is the separate information displayed for every file.
To help you understand the problem here is an image showing the problem and the code. I got very far on my own but its way above my head, I just cant see a simple way to correct the problem. The code may look very awkward and odd as I'm totally new at this but it looks visually right on the browser. I would really appreciate any help thank you.
Here is an image of the problem: http://i46.tinypic.com/m79cvs.png
<?php if (!empty($thelist)) { ?>
<p class="style12"><u>Fix</u></p>
<p class="style12"><?=$thelist?><?php echo " - " ?> <?php $filename = '../../customers/client1/client1.fix.exe';
if (file_exists($filename)) {
echo "" . date ("m/d/Y", filemtime($filename));
}
?> <?php echo " - <a href='download.php?f=client1/client1.fix.exe'><b>Download</b></a> <a href='download.php?f=client1/client1.fix.exe'>
<img src='../css/images/dlico.png' alt='download' width='35' height='32' align='absmiddle' /></a>" ?>
</p>
<?php } ?>
The list ($thelist) contains your files, yes?
You are not working on the $thelist, but on the $filename which is a hardcoded string.
Why? Currently you are outputting <?=$thelist?> and it looks like concatenated string from filenames. I would suggest that $thelist should be something like an array of your files. Then you could iterate over the files and output html dynamically for each entry.
<?php
// define your directory here
$directory = xy;
// fetches all executable files in that directory and loop over each
foreach(glob($directory.'/*.exe') as $file) {
// output each name and mtime
echo $file . '-' . date ("m/d/Y", filemtime($file));
// or you might also build links dynamically
// $directory needs to be added here
echo ''.$file.' - Size: '.filesize($file).'';
}
?>