How to call image url from directory using php - php

I want to display the image and the image url of a directory
I found this code, but there is a dot before the filename.
<?php
$files = glob("./images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
print "<textarea>";
print "http://" .$_SERVER['SERVER_NAME'].$image;
print "</textarea>"."<br />";
echo '<img src="'.$image .'" />'."<br /><br />";
}
?>
the result will be like this :
<textarea>http://domain.com./images/filename.png<textarea>
<img src="./images/filename.png" />
please help to fix this. thanks.

I solved the problem by using the following:
<?php
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
echo '<img src="'.$image .'" />'."<br /><br />";
print "<textarea>";
print "http://" .$_SERVER['SERVER_NAME'].$image;
print "</textarea>"."<br /><br /><br />";
}
?>

Related

PHP issues displaying image gallery

I'm trying to make a gallery with PHP. I want to get all of the images out of a folder and then display them in rows of 3. I kind of have it working but the first 2 images mess up.
This is what I've tried:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE);
echo '<table width="100%>';
$count="-1";
foreach($images as $image) {
if ($count%3 == 1) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery", "", $image);
echo '<td width="33%"><div class="gallery">';
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
if ($count%3 == 3) {
echo '</tr>';
}
//echo $count;
$count++;
//echo "|".$count;
}
if ($count%3 != 1) {
echo ',</tr>';
}
echo '</table>';
//echo print_r($images);
This works kind of but it makes this:
(These are just stock photos, the real photos are a bit.. offensive)
I know I'm doing something wrong but I don't know what!
There were some errors in your code (see comments). Maybe try this:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img/*.{png,jpg,gif}", GLOB_BRACE);
echo '<table style="width:100%">'; // error was here (missing ")
$count = 0; // error was here (counter = "-1")
foreach ($images as $image) {
// start <tr> on 0
if ($count == 0) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery/", "", $image);
echo '<td style="width:33%"><div class="gallery">'; // alternative
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
// end tr at 3
if ($count == 3) {
echo '</tr>';
// reset counter
$count = -1;
}
$count++;
}
echo '</table>';
I think you have trouble with your $count initial value.
Try this:
$count="3";
foreach($images as $image) {
if ($count%3 == 0) {
echo '<tr>';
}
$count++;
...

How to echo picture using php?

Please help me with this guys,
I have this code:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
echo '<center><img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}
else
{
echo "photo not uploaded";
}
the problem here is when i echo it and use its tmpname i am having an error in this section
<img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" //....
the userfile is underlined in red. Is it wrong to use its tempname in echoing it?
help me please...
Change your echo statement like this,
echo '<center><img src="admin/gallery/'.{$_FILES['userfile']['name']}.'" width="100" height="100"/>'.'</center>';
Also your if checks showing incorrectly.
May be like this,
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']")
{
.....
}
try with proper quoting and if()
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/".$_FILES['userfile']['name']))
{
echo '<center><img src="admin/gallery/'.$_FILES['userfile']['name'].'" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}else
echo "photo not uploaded";
}
you just have a problems with your ' and "
i would do it like this
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
this should do it
total source is
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
}
else
{
echo "photo not uploaded";
}
Try this. You can use php in html attributes and tags
<img src="admin/gallery/<?php {$_FILES['userfile']['name']} ?>" width="100" />
You should use:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
And
echo "<img src='admin/gallery/{$_FILES['userfile']['name']}' width='100'...";
or better close php with an ?>then
<img src='admin/gallery/<?php echo $_FILES['userfile']['name'];?>' width='100'...

How to put mysql data into popup window?

I have a web page with images and when user clicks on any of the image, it has to derive data of that particular image from MYSQL database. What I am doing is using a simple JavaScript popup and putting the data from database. However I am just getting the first item from database on all images.
This is the code:
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
while($row = mysql_fetch_array($result))
{
if ($colCnt%4==0)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
$colCnt++;
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
$row['name'] is just giving out the first name as it is in a while loop. I am not being able to get other names for other images. How can this be done. Any help would be appreciated.
Does one iteration in your while fetch single image data? And what I can understand according to your code is that you are displaying 4 image in a row.
Can you please format your code a bit..its looking too ugly.
I need to know which statement is calling your modal window.
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
echo '<tr>';
while($row = mysql_fetch_array($result))
{
$num = $files[$i];
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br>
<div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow"><div><p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>Ok</div>
</div></td>';
$colCnt++;
if ($colCnt % 4 == 0)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
Try this.
Also see how beautiful the code looks if its properly formatted..
try this
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=4;
while($row = mysql_fetch_array($result))
{
for ($i = 0; $i < $colCnt; $i++) {
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
}
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
mysql_close($con);
include 'footer.php';
?>

Displaying all Images within a Folder using php

I've used this php code within my html in order to display all pictures within a folder called uploads. The only problem is that icons images are the only type of image being displayed. What have I done wrong?
<?php
$files = glob("uploads/*.*");
$colCnt=0;
echo '<table border="1" style="width:590px;">';
for ($i=1; $i<count($files); $i++)
{
$colCnt++;
if ($colCnt==1)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
$num = $files[$i];
echo '<img src="'.$num.'" align="absmiddle" /> ';
print substr(substr($num,6,100),0,-4);`
echo '</td>';
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
echo '</table>';
?>
The loop seems to be ok.
You have to focus on the result coming from
$files = glob("uploads/*.*");
Try print_r($files) to get the list and then see if it is selecting all the images properly or not.

Image not showing

I have used this code for accessing image from database to web page. But after debugging there is no image in image tag.
This code prints the correct path....but fails to return correct image in image tag...
echo $_SESSION['user_image'];
How do I get the image in image tag?
plz help me.
while($row = mysql_fetch_assoc($result))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['user_level'] = $row['user_level'];
//$_SESSION['user_image'] = $row['image'];
$image=$row['image'];
$user_image= trim('C:/xampp/htdocs/source/img/' . $image);
//$user_image = imagecreatefromstring($user_image);
$_SESSION['user_image']=$user_image;
echo $_SESSION['user_image'];
}
echo "<img src='".$row['image']."'>";
echo 'Welcome, <img src= "'.$_SESSION['user_image'].'" alt="" width="50"
height="40" /> ' . $_SESSION['user_name'] . '. <br /><a href="index.php">Proceed to
the forum overview</a>.';
You are using the local path in the server.
'C:/xampp/htdocs/source/img/' . $image
Should be
'/source/img/' . $image

Categories