How to while loop images from directory - php

Surprisingly, Google had nothing.
My old method of uploading images was storing the actual image in the database. I'm trying to change my method to storing the photos in directories. But I'm having troubles figuring out how to display them to the users. I'm trying to while loop the files. Much help is greatly appreciated. Also, PDO is not my strong suit, trying to learn it the best I can. Here is what I have.
$query6 = "SELECT * FROM photos WHERE userID=$memberID";
$result6 = $db->query($query6);
while ($row6 = $result6->fetch(PDO::FETCH_ASSOC)) {
$photo = $row6['photo'];
while ($photo == true) {
echo "<img src=\"images/$memberID/$photo\">";
}
}
The page isn't loading at all. Not even an error. Your help is greatly appreciated. Thank You!
The code does insert the photo name in database and properly upload the photo to the directories. I only need assistance with displaying to the user. :)

while ($photo == true)
{?>
<img src="images/<? echo $memberID;?>/<?echo $photo;?>">

close your img tag.like
echo "<img src=\"images/$memberID/$photo\" />";

Related

custom code for how to upload product image from backend to front end like ecommerce in php

I am new to PHP. I want to know how to be able to upload a product image from the back end to the front end of a static website using PHP. Please help me. Thank you in advance.
Select image :
<?php
if(isset($_POST['Submit1']))
{
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
echo "<img src=".$filepath." height=400 width=300 />";
}
else
{
echo "Error !!";
}
}
?>
I have tried the code above. It is running, but the image is displaying only on the same page. However, I need the image to display on another page when I click on submit button.
You can redirect this page when image is uploaded;
header( 'Location:http://anypage.com/?filepath='.$file_path );
and you can use get method;
$filepath=$_GET['filepath']
echo "<img src=".$filepath." height=400 width=300 />";
You can NOT pass the variable from a page to another until you use Session in PHP. But that is not a good practice for product image.
You should store your $filepath in the database and fetch it every where in your application when you need that.

PHP search video files in directory and display single video

I am Justin and new to Stackoverflow. We have a yearly familyweekend and every year someone else creates the entertainment. This year it is my turn. I am creating a game (treasure hunt) with the use of an iPad.
For this I am looking for a script that can search for a filename in a directory and when found, display the file (video) in the page. I am a little familiar with PHP, but not very.
It does not require any database it can be very simple because it will only contain a few videos. But I can't find any scripts that quite do what I need. Every script is either to complicated and advanced or does not show the video but the filestring.
Can anyone help me by pointing me in the right direction? Thanks so much in advance.
Regards,
Justin
to check if a specific file exists in a directory you can use file_exists() method.
Please check the documentation: http://php.net/manual/en/function.file-exists.php
So after trying and trying, I think I found the answer. Below the script for anyone looking for this.
<?php
$dir = 'directoryname';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['formname']))? strtolower($_GET['formname']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
echo "<video width='320' height='240' controls>";
echo "<source src='$dir/$file'>$file type='video/mp4'>";
echo "</video>";
echo "";
echo "<br>";
}
}
closedir($res);
?>
And the form
<form action="search.php" method="get"><input name="formname"
type="text"> <input type="submit"></form>

PHP pull specific sets of images from a directory

I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. i.e. if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$imgnum = $row["id"];
?>
<tr>
<td>
<img src="images/<?php echo $imgnum ?>.jpg"/>
The issue is there may be a series of images all associated with that row, i.e. 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.
Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?
cheers
You can use php's glob() to find similar files. Something along the lines of this:
foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
// do something with $filename
}
Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.
<?php
$imgnum = $id;
foreach (glob("images/" . $imgnum . '-*') as $filename) {
echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
}
?>

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

Links from Array

I kind of have a problem.
Maybe I'm doing the hardest way, I do not really know, if you could I'll appreciate for your help.
I upload photo to FTP and URL was upload to mysql in this format (http:///claim/img/box.png, http:///claim/img/box.png) with specific ID
-----------------------------------
ID - URL
1 - http://*/claim/img/box.png, http://*/claim/img/box.png
-----------------------------------
And later I grab this data, and to generated form by ID.
I used explode to divide URL. And with FOR loop, I would display it perefctly, But here is the glitch. Because there is need to make export this form as a doc file and send email with this generated form. I can't end echo.
Because when I do only first part is send, but not images from array.
For example:
$email = "ID 1<br>(Its the start of the form)";
$photos = explode(',', $claim['photos_url']); $arrlength = count($photos);
for($x=0;$x<$arrlength;$x++) {
echo "<img src=".$photos[$x]."><br>"; }
echo "The end of form";
I similar cases I simply use echo "the start of text".$date."the end";
But now, I have no idea how to do it.
Thank you for your help in advance.
your code looks so incomplete , but i think you have an array some how that includes image urls
and you want to echo each one of them :
$images = array("http://127.0.0.1/img/1.jpg","http://127.0.0.1/img/2.jpg","http://127.0.0.1/img/3.jpg");
foreach($images as $image)
{
echo "<img src='".$image."' />";
}
im not sure if i understand u correctly but hope it helps...

Categories