LAMP HTML/PHP can't load images [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm creating a movie database and I have a Python script that downloads movie posters (and information) and stores them in a folder. For example, /var/www/html/images.
I also have a PHP file that displays a unique page for each movie, according to the ID. So for example there is a list page, the user clicks on a film and it goes to localhost/movielist.php?id=3 etc.
So far, the movie information is displaying perfectly. However, the images won't load.
In my python script, I save the movie poster image as the movie's name, so say if I had Finding Nemo's poster, the file name would be Finding Nemo.jpg. It saves it fine and I can look at the image in a folder.
The problem is that the image won't display on my PHP page.
This is the PHP code in my file:
<?php
$username="root"; $password="12345"; $database="videos";
$con = mysqli_init();
mysqli_options($con, MYSQLI_OPT_LOCAL_INFILE, true);
mysqli_real_connect($con, "localhost", $username, $password, $database);
mysqli_select_db($database);
if(isset($_GET["id"])){
$id = $_GET["id"];
} else {
require("listFull.php");
}
$res = mysqli_query($con, "SELECT id, title, yr, genre, plot, rating FROM films WHERE id = " . $id);
$movItems = array("Title" => "title", "Year" => "yr", "Genre" => "genre", "Plot" => "plot", "Rating" => "rating");
while($row = mysqli_fetch_array( $res )){
foreach($movItems as $k => $el){
echo "<h3>" . $k . "</h3>";
echo "<p>" . $row[$el] . "</p>";
}
echo "<img src='/var/www/html/images/".$row['title'].".jpg'>";
}
?>
This gets the row/movie's information according to the ID of the page and displays it. The image's name in the image folder is the same as the movie name in the database.
How can I get my images to display?

The issue is likely to be with your image path, this should be relative to the web root directory, not the actual path on the web server. So for example if your web root directory is /var/www/html and your images are stored in /var/www/html/images then your image path should be:
echo "<img src='/images/".$row['title'].".jpg'>";

Your problem is the img tag. First of all, the tag is malformed, the <at the beginning is missing.
Secondly, you shouldn't use /var/www/html/ as your image path because that directories are visible only in localhost. If you want to make them public you have to provide the correct url like www.mysite.com/images/Finding%20Nemo.jpg.
Finally, pay attention at the files' name: in http requests you can use spaces and other special characters (in the example above I substituted the space with %20) so make sure to substitute them.

Related

Problem with adding image files to my server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Basically, I am trying to put an image file into a folder in my server and add the dir in MYSQL BD. It successfully puts the file into the folder and adds the path link to the row in the table.
Now, the problem: when I try to download the file and show it, it downloads but nothing shows, so I check the file in the FTP and it turns out the file size is 0.1kb. What am I doing wrong and how do I fix it?
I've done some research on the issue and I might be crazy but I think I'm the only one having this issue because I couldn't get help anywhere.
here's my code;
$ImageData = $_POST['image_data'];
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageData));
// file_put_contents($UploadPath,$ImageData);
echo "Your Image Has Been Uploaded.";
}
N/B: I use base64_decode because I encoded it from android.
I expect the file to save to the server dir with the actual size.
Files are stored in $_FILES not $_POST.
Assuming that the field in your form is called image_data
$ImageTmpPath = file_get_contents($_FILES['image_data']['tmp_name']);
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageTmpPath));
echo "Your Image Has Been Uploaded.";
}
So thanks to #RiggsFolly and #Rust, files are actually stored with $_FILES not $_POST
but what actually solved my issue was the
file_put_contents($UploadPath,base64_decode($ImageData));
it is supposed to be
move_uploaded_file(base64_decode($ImageTmpPath),$UploadPath);

Adding images to phpword with cloneblock

Please assist me. See code below. I am adding data to my phpword document, using a template processor, and creating a new document. I have just resolved the adding text issue, as the data in the clone block was being duplicated throughout. But now I am having the same issue with images.
My observations.
My file path is media\123456789.jpg (as an example), but I notice to troubleshoot I instead said setvalue instead of setimage, to see if it gets the file name correct. But it adds media3456789.jpg. So it leaves out the backslash and the following 2 characters. But I don't think this is the root cause. My point below describes why.
It adds the first clone block/ loop interaction images fine.....so I don't think the file name is a problem....by the way. If I setvalue the file name, it displays the correct name in the word document, although the name is truncated, the ending of the file name is correct, so why question why isn't it populating the block with the correct image instead of duplicating the first set of images.
$group_key=1;
do {
//echo "";
$repeatgroup = $id."/"."trailer_repeat_group"."[".$group_key."]";
//echo "repeatgroup before query___ ".$repeatgroup;
//echo "";
// inserting data from trailer table
$trailer_repeat_grouping = mysqli_query($connect, "SELECT * FROM trailer_repeat_group LEFT JOIN odkmain on trailer_repeat_group.PARENT_KEY = odkmain.metainstanceID WHERE trailer_repeat_group.KEY_id = '$repeatgroup'");
$templateProcessor->cloneBlock('CLONEME', $trailer_count);
while ($row1 = mysqli_fetch_array($trailer_repeat_grouping))
{
$templateProcessor->setValue("Left_trailer_tyre_condition",$row1['Left_trailer_tyre_condition'],2);
$templateProcessor->insertImage("img:right_handtrailer_tyres_image",$row1['right_handtrailer_tyres_image'],2);
$templateProcessor->setValue("right_trailer_tyre_condition",$row1['right_trailer_tyre_condition'],2);
$templateProcessor->insertImage("img:left_handtrailer_tyres_image",$row1['left_handtrailer_tyres_image'],2);
}
//echo mysqli_num_rows($trailer_repeat_grouping);
    //    endwhile;
$group_key++;
} while ($group_key <= $trailer_count)

Update files images

I have code where I upload 6 images in one part of my site, that code worked fine, now, I need to update or change any image that was uploaded, I mean I need to change the image number 3 and 6, for example, so I wrote this code
$i=0;
while ($i<=5){
if (!empty($_FILES['ufile']['name'][$i]) and ($_FILES['ufile']['name'][$i]<>"")){
$path[$i] = "../slider_new/".$_FILES['ufile']['name'][$i];
$path[$i] = str_replace(' ', '_',$path[$i]);
copy($_FILES['ufile']['tmp_name'][$i], $path[$i]);
echo "Ruta :".$path[$i]."<BR/>";
echo "File Name :".$_FILES['ufile']['name'][$i]."<BR/>";
echo "este es $i ",$i;
$sql="UPDATE accommo_main_images SET name='".$_FILES['ufile']['name'][$i]."',ruta='".$path[$i]."' where num='$num'";
$res=mysqli_query($cnx,$sql);
}
$i=$i+1;
}
The idea is that check the name and when it is different to "" so..... update the image, that is the idea but I don´t know why the code update all the 6 images with the image that I selected.
What cam be the problem ?
Thank you for you help
where num='$num'";
You use a SQL statement WHERE NUM=$num. Yet, you did not set $num anywhere in the code you gave us here. Most likely you never set the value of $num. You probally ment $i.
I'm guessing you also possibly made the same mistake in your upload code (initial upload) and therefor all images have number=0.
You are now updating images which have number 0, or empty string (depending on database field type) which would match every image you have in there.
Try running this code and check the table entries.
$res = mysqli_query($cnx,"SELECT num,name FROM accommo_main_images");
var_dump(mysqli_fetch_all($res,MYSQLI_ASSOC));

Loading whole pictures instead of URL's inside a database from an XML file

I have a simple php code which loads an external XML file and loads the pictures URL's into my database.
Then I take the URL's and display them on my site.
The problem is that I end up loading pictures from other websites on my site, which affect loading time - loading 20 pictures per page now.
So I am thinking, Is there a way to store the image completely into my database, instead of just the URL?
Here is the code:
$myfeed = 'xmlfeed.xml';
$myfeed_xml = simplexml_load_file($myfeed);
foreach($myfeed_xml->info as $myinfo){
$pic0 = $myinfo->picture_url[0];
$pic1 = $myinfo->picture_url[1];
$pic2 = $myinfo->picture_url[2];
$pic3 = $myinfo->picture_url[3];
$pic4 = $myinfo->picture_url[4];
if($pic0 != ''){
mysql_query("INSERT INTO ".$table." (pic0, pic1, pic2, pic3, pic4) VALUES ('$pic0', '$pic1', '$pic2', '$pic3', '$pic4')", $dbh);
}
}
Thank you!
Why not download them all on your server, and update the DB with the links from your server? As long as copyright policie(s) are okay with it...
// do your DB fetching here
//loop through all current db links
foreach($sqlResult as $result)
{
// build up file path to store newly downloaded image
$fPath="someFolder/pictures/";
// get/generate a name for the pics (I'll just use a radom number here, but you should avoid doing so if you are working with lots of urls as dups may happen)
$iName=mt_rand();
//join path and url together
$pAndURL=$fPath.$iName;
// get and put data
file_put_contents($pAndURL,file_get_contents($result['collumnWhereURLIsStored']);
//now update your DB with the new link ($pAndURL)
}//end of foreach
So what the code above does is simply goes through all the 3rd party links in your DB and downloads their content (images) to your server. Then you can simply update the DB with your own link to the specific image. Simple. But as I previously mentioned, check copyright licenses first as I'm sure you don't want to be getting into trouble now, hm?

Use PHP to display thumbnails as links to images organized in MySQL database

I have a folder of images and a MySQL database to manage them. The database has one table: images, and 7 fields: imgID, imgTitle, imgURL, imgDate, imgClass, imgFamily, & imgGender. The primary key is imgID and the Index Key is imgDate.
I wish to display thumbnails of these images on a web page as links to the large versions, using the imgDate to organize them with newest first, and oldest last. The images have the same file names as their thumbnails. The large images are located in new_arrivals_img/ relative to the site root, and the thumbnails are located in new_arrivals_img/thumbnails/ again relative to the site root.
As I'm new to both MySQL and PHP, I was hoping someone could help me with the code. Everything I have tried has failed.
Right now I'm building the site, and thus using MAMP to host it locally. I have had some problem figuring out relative paths for my images. Is there a way to set new_arrivals_img/ as the root?
given no code, I think this is what you are looking for
//connect to mysql database
$query = mysql_query("SELECT `imgURL` FROM `images` ORDER BY `imgDate` DESC") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'> <br/>";
}
}
this would show the images with line break from the folder new_arrivals_img/thumbnails

Categories