I was able to get some help earlier today, but I didn't have everything from the original script for it to work. Basically I have a list of image file names in a .txt file. They each load in a slideshow, and change with the pagination on the page.
What I would like to do, is if I have a file that has a .mov extension, for example, the php script will load a movie player instead.
Here is the original slideshow script
<div id='jessslide'>
<?php
echo"
<div id='slider-wrapper'>
<div id='slider' class='nivoSlider'>";
$photos = file("work.txt");
foreach ($photos as $image) {
$item = explode("|", $image);
if ($item[0] == $fields[0]) {
$photo = trim($item[1]);
echo"<img src='images/work/$photo' alt='' />\n";
}
}
echo"
</div></div>"
?>
</div>
And here is my bad attempt at trying to make this work...
<div id='jessslide'>
<?php
$photos = file("work.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach ($photos as $image) {
$item = explode("|", $image);
if ($item[0] == $fields[0]) {
$photo = trim($item[1]);
$ext = explode(".", $image);
if (in_array($ext[1], $img))
{
echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>";
}
elseif (in_array($ext[1], $vid))
{
echo "<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>";
}
}
}
?>
</div>
I would really appreciate if someone could help me out to finally bring this script to life! :)
The most likely problem I see is the possibility that the assignment to $ext should be tied to $item or $photo instead of $image. If that solves it, then great. Otherwise, read below for a more complete analysis and some suggestions for steps to debug until you narrow in on the cause of the problem.
Assuming all of the data is accurate, the script you've written looks like it should work. I've reformatted it here to conduct some analysis:
<div id='jessslide'>
<?php
$photos=file("work.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
$ext = explode(".", $image);
if(in_array($ext[1], $img))
{ echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>"; }
elseif(in_array($ext[1], $vid))
{ echo "<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>"; }
}
}
?>
</div>
There are three major places in this script for problems to occur that would cause nothing to be output.
The first is in the foreach() loop. If the $photos array has nothing in it, you would skip the entire block of code. You can test for this condition by adding a print_r($photos); before the foreach() and then as the first line in the body of the foreach() add echo $image." "; to verify that all the files are listed as you expect. If that looks correct, remove that debugging code and move on.
The 2nd potential for problems is if the $item[0] is not equal to $fields[0]. To test this, add echo 0; as the first line inside if($item[0]==$fields[0]). If you see zeros as expected when the script is run, then you can remove this debugging code and move on.
The 3rd potential for problems is pull/examination of the extension. One likely candidate for problems here is if the assignment to $ext should be tied to $item or $photo instead of $image but there are definitely other possible issues. To test this, add echo $image." ".$ext[1]."\n"; print_r($img); print_r($vid); before the if(in_array($ext[1], $img)). Then add a temporary else clause with the body of echo 3; as well. Verify that th
Once you figure out which condition in the code is causing problems, you will be well on the way toward solving it. My guess is that you'll get through the tests and either find an obvious mistake in one of the early sections that we are assuming works right, or you will end up with 3 being printed out a lot. In the later case, one possible issue could come from lower/upper-case differences, which could be solved via changing the $item assignment to $item=strtolower(explode("|", $image);
Related
My English Languege is not good. sorry.
I Want Get My Variable Folder Name In mydiv Content and Embed to my php Code (in $flds)?
I do not want to use form.
please help me. thanks
<div id="mydiv">Variable Folder Name</div>
<?php
$flds = $mydivContent; //?????
$all_files = glob("uploads/$flds/*.*");
for ($i=0; $i<count($all_files); $i++) {
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format)) {
echo '<img src="'.$image_name .'" alt="'.$image_name.'" />';
}
else {
continue;
}
}
?>
PHP is server side programming language, PHP executes first when you send request to server there is no way to call specific html element of same page by PHP.
Either you should already put value to variable $mydivContent in PHP
or you could use html <form>(s) to get response from client
I'm using the below code to pull through user uploads into a portfolio. It's working great however one image (out of a list of many) is showing blank - yet displays on the next page, using the same path, but within a conventional <img /> tag.
<?php
$files= glob('./uploads/users/'. $item->user_id .'/'.$item->id.'/public_large/*');
$count = 0;
$user_avatar = '/themes/users/assets/img/noartwork.jpg';
foreach ($files as $file) {
$count++;
if ($count > 0 && is_file($file)) {
$user_avatar = $file;
}
}
?>
<div class="user-img" style="background-image:url('<?php echo site_url($user_avatar); ?>');"></div>
When I inspect element on the image that's not displaying, if I change the single quotes background-image:url('<?php echo site_url($user_avatar); ?>'); to double background-image:url("<?php echo site_url($user_avatar); ?>"); it pulls through, but this is not the case if I make this change in the actual code.
Any help would be great.
Thanks
have you tried removing single quotes? like this :
background-image:url(<?php echo site_url($user_avatar); ?>);
I found the fix for this. It was to do with the users file name containing a special character that I wasn't checking for e.g. my-user's-image.jpg breaks the code as the character needs to be escaped my-user\'s-image.jpg.
I amended the foreach to run a check as below and this is now working.
foreach ($files as $file) {
$count++;
if ($count > 0 && is_file($file)) {
$user_avatar = str_replace("'", "\'", $file);
}
}
Thanks
I'm just a persistent beginner and I've met another obstacle in my way, so I hope that you'll help me one more time... :) I've got that HTML:
<div class='hold'><?php include 'image.php'; ?></div>
<div class='refresh'>Get new image</div>
And that PHP code:
<?php
$dir = 'images/';
$img = array();
if(is_dir($dir)) {
if($od = opendir($dir)) {
while(($file = readdir($od)) !== false) {
if(strtolower(strstr($file, '.'))==='.jpg') {
array_push($img, $file);
}
}
closedir($od);
}
}
$id = uniqid();
$smth = array_rand($img, 1);
echo '<img src=' . $dir.$img[$smth] . '?' . $id . ' width="200px" height="50px" />';
echo '<input type="hidden" value=' . $id . ' />';
?>
So now when I'm looking at my page I see in the <div class='hold'></div> my img from the folder images, and it's allright. BUT when I click on the <div class='refresh'></div> I obviously want to get another img from my folder, but I dunno how to accomplish that trick correctly.
I know that first answer will be USE AJAX, and I know that I can do something like
function loadGraphic() {
$('.hold').load('image.php');
};
loadGraphic();
and then $('.refresh').click(loadGraphic); but when I'm trying to do that in response from server I get TWO THINGS: image.php and, of course, something like car.jpg?573c4e010c7f6... But I very-very wanna get just ONE looking like
car.jpg?573c4e010c7f6
or
image.php?573c4e010c7f6 - I don't care...
So... I hope you've got my concept... maybe I'm asking for miracle - I dunno! Any, absolutely any help will be appreciated :)
Try it the following way:
JS function:
function loadGraphic() {
$.get("image.php", function(data) {
$(".hold").html(data);
});
};
Html:
<div class='refresh' onclick='loadGraphic()'>Get new image</div>
so far I've successfully moved an uploaded image to its designated directory and stored the file path of the moved image into a database I have.
Problem is, however, is that the img src I have echoed fails to read the variable containing the file path of the image. I've been spending time verifying the validity of my variables, the code syntax in echoing the img src, and the successful execution of the move/storing code, but I still get <img src='' when I refer to the view source of the page that is supposed to display the file path contained in the variable.
I believe the file path is stored within the variable because the variable was able to be recognized by the functions that both moved the image to a directory and the query to database.
My coding and troubleshooting experience is still very adolescent, thus pardon me if the nature of my question is bothersomely trivial.
Before asking this question, I've searched for questions within SOF but none of the answers directly addressed my issue (of the questions I've searched at least).
Main PHP Block
//assigning post values to simple variables
$location = $_POST['avatar'];
.
.
.
//re-new session variables to show most recent entries
$_SESSION["avatar"] = $location;
.
.
.
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
//define variables relevant to image uploading
$type = explode('.', $_FILES["avatar"]["name"]);
$type = $type[count($type)-1];
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rdn = substr(str_shuffle($chars), 0, 15);
//check image size
if($_FILES["avatar"]["size"] > 6500000) {
echo"Image must be below 6.5 MB.";
unlink($_FILES["avatar"]["tmp_name"]);
exit();
}
//if image passes size check continue
else {
$location = "user_data/user_avatars/$rdn/".uniqid(rand()).'.'.$type;
mkdir("user_data/user_avatars/$rdn/");
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else {
$location = "img/default_pic.jpg";
}
HTML Block
<div class="profileImage">
<?php
echo "<img src='".$location."' class='profilePic' id='profilePic'/>";
?><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
View Source
<div class="profileImage">
<img src='' class='profilePic' id='profilePic'/><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
Alright, I've finally found the error and was able to successfully solve it!
Simply declare a avatar session variable to the $location variable after updating the table, update the html insert by replacing all $location variables with $_SESSION["avatar_column"] and you are set!
PHP:
$updateCD = "UPDATE users SET languages=?, interests=?, hobbies=?, bio=?, personal_link=?, country=?, avatar=? WHERE email=?";
$updateST = $con->prepare($updateCD);
$updateST->bind_param('ssssssss', $lg, $it, $hb, $bio, $pl, $ct, $location, $_SESSION["email_login"]);
$updateST->execute();
$_SESSION["avatar"] = $location; //Important!
if ($updateST->errno) {
echo "FAILURE!!! " . $updateST->error;
}
HTML:
<div class="profileImage">
<?php
$_SESSION["avatar"] = (empty($_SESSION["avatar"])) ? "img/default_pic.jpg" : $_SESSION["avatar"] ;
echo "<img src='".$_SESSION["avatar"]."' class= 'profilePic' id='profilePic'> ";
?>
.
.
.
Thank you!
try this code
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$location = ""; //path
if($_POST && $_FILES)
{
if(is_uploaded_file())
{
// your code
if(<your condition >)
{
}
else
{
$location = "./user_data/user_avatars/".$rdn."/".uniqid(rand()).'.'.$type;
if(!is_dir("./user_data/user_avatars/".$rdn."/"))
{
mkdir("./user_data/user_avatars/".$rdn."/",0777,true);
}
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else
{
$location = "img/default_pic.jpg";
}
}
?>
Html Code :-
<div>
<?php
$location = (empty($location)) ? "img/default_pic.jpg" : $location ;
echo "<img src='".location."' alt='".."'> ";
?>
</div>
If it helpful don't forget to marked as answer so another can get correct answer easily.
Good Luck..
I am using a simple script that displays images in a jquery slideshow - these image filenames are listed in a .txt file, and change depending on the page you are on (im also using pagination in another script).
If the filename that is listed in the .txt file doesn't exist, I would like the image 'unavailable.jpg' to display instead...
The original script:
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
echo"<img src='images/work/$photo' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
And here is my try at it...but it doesn't work properly- instead of the 'unavailable.jpg' image being displayed, it shows all of the images in the directory... :S Anyone have any ideas of what I might be doing wrong? :S
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
$photo=trim($item[1]);
if (file_exists("images/work/".$photo)) {
echo"<img src='images/work/$photo' alt='' />\n";
}
else{
echo"<img src='images/work/unavailable.jpg' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
Instead of all the images showing, I only want images for that page to display. Here is an example of my text file:
1|image1.jpg
1|image2.jpg
1|image3.jpg
2|image1.jpg
2|image2.jpg
The 1 and 2 are for the pages 1 and 2, and they display the images that are listed. This all works fine in the above original script that I have posted, but it seems to break when I add the if file_exists.
The path to your images uses a relative path. Are you sure the current working directory is the directory you think it is?
To verify do an echo 'Current Working Directory: '.getcwd()."<br />\n" and verify what directory you are in.
Its probably best to use a full file path to your image's directory so the script can be placed anywhere on your server.
Now if that is correct then you need to check that your script has permission to your image directory. Typically php runs as nobody:nobody or apache:apache depending on your configuration.
The directories above as well as the files should have 644 (-rw-r--r--) or at a minimum 444 permission (-r--r--r--).
Try these two things and let us know if that solved your specific problem or not; I hope it does.
You need to re-add the test for $item[0] == $fields[0]:
<?php
echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos = file("photos.txt");
foreach ($photos as $image) {
$item = explode("|",$image);
$photo = trim($item[1]);
if (file_exists("images/work/".$photo)
&& $item[0] == $fields[0]
) {
echo "<img src='images/work/$photo' alt='' />\n";
} else {
echo "<img src='images/work/unavailable.jpg' alt='' />\n";
}
}
echo "</div></div>";
?>
Edit:
I just noticed that Ott pointed this out already in the comments. Are you still having the issue with unavailable.jpg?