HTML loaded image goes away after refresh - php

So this is a weird phenomenon I've never experienced in that I have an HTML form on image upload (exactly the one on W3). It does it's job properly when clicked and pushes the user profile picture into a folder while saving the name of the file on a database. Underneath the upload, I have a little image tag that spits out the uploaded profile picture. The end goal is for that profile picture to be displayed on the page constantly while the session exists. When uploaded, it works perfectly fine and the profile picture appears. After one refresh, the image can't be found and the alt="blank" takes over. The location still stays proper in the database so I don't think that's the issue. Is there an error? Do I need to use JS onload? Does the image tag only work once? Please help and thank you for taking the time to read this.
PHP:
echo '<img src="'.$loaded_profile_picture.'"id="HOMEPROFILE" alt="blank" style="width:128px;height:128px">';
//$loaded_profile_picture has the value uploads/photo.jpg
Classes.php:
public function addProfilePicture(){
include_once "conn.php";
$sql=$dbh->prepare("UPDATE users SET UserProfilePicture=:UserProfilePicture WHERE UserName=:UserName;");
$sql->execute(array(
'UserProfilePicture'=>$this->getUserProfilePicture(),
'UserName'=>$_SESSION['UserName'],
));
}
Image Upload (Might be the culprit):
if($uploadsuccess&&move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
$user->setUserProfilePicture($target_file);
$user->addProfilePicture();
if($user->getUserProfilePicture()!=NULL){
echo '<img src="'.$target_file. '" alt="nom">';
}
}
Edit:
While many users have a similar problem (i.e. Image display with <img src=""> not working) I have tried looking at localhost:8888/xampp/LocalPost/pages/uploads/photos.jpg and it displays just fine. I have also changed file permissions on Windows 7 with no luck. Thanks for all your hard work I'll keep searching.
Edit:
I got around the problem by using $_SESSION['UserProfilePicture'] and it sticks after a refresh. This doesn't solve the question. I'd still very much like someone to help me find what went wrong, because later on I would like to display other people's Profile Pictures which you obviously can't do with session variables.

first of all your missing } et the end of your script
if($uploadsuccess && move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
$user->setUserProfilePicture($target_file);
$user->addProfilePicture();
if($user->getUserProfilePicture()!=NULL){
echo '<img src="'.$target_file. '" alt="nom">';
}
}

Check once that after refreshing your page you will be get the value of $loaded_profile_picture variable or not?
If you got the value of $loaded_profile_picture variable then check it is same as your previous path or not?

Related

My image isn't being saved in the folder?

I have an add form, where users can add an image, once they click submit I want the image to be saved into a certain folder located called Event_images (which I use to display the image on another page where I display the image along with other details they inputted. But once I submit the form, the image goes to the MYSQL database, but isn't in the folder where I direct it to. My code and a screenshot are provided. I want the image to be displayed in the Event_images folder I have created. Thank you in advance.
<?php
$event_img = $_FILES['event_img']['name'];
$tempimage = $_FILES['event_img']['tempname'];
move_uploaded_file($tempimage,"Event_images/$event_img");
?>
The screenshot I have provided is what happens when I try to display the information (because nothing is in the folder which I direct to)
['tempname'] isn't an index of $_FILES. See here.
Use $tempimage = $_FILES['event_img']['tmp_name']; instead. I tested locally and it worked fine. If you're using PHP 7 you may need to use { } around $event_img when you move the file so it'll process that variable.
Does this work?
$event_img = rand(1000,100000)."-".$_FILES['event_img']['name'];
$tempimage = $_FILES['event_img']['temp_name'];
$folder="Event_images/";
move_uploaded_file($file_loc,$folder.$event_img);

PHP search folder for user pic set default image if nothing found

Im using a great opensource script called bulletproof allowing users to upload a profile picture.
The profile picture gets stored in a folder as specified. Im not using any mysql.
What I would like to do is display a default user img if user has not uploaded a profile picture.
If he/she has uploaded a profile picture the default img will obviously get overwritten to the pic uploaded by user.
The problem comes in on how would I display the default profile pic, if user has not uploaded anything? Keeping in mind im not using any mysql.
When an image is uploaded it is assigned the name of the user ID, like so
<img src="imgs/users/1.png" style="width:100%" height="125px">
Would you say it is the most efficient way to search the folder for an image match on the name userID?
If something is found display uploaded img else display default img? Or should I rather go a mysql route?
Hope the question is clear, if you need any more info please drop me a comment below. Any advice and/or help appreciated.
EDIT: this is what I eventually came up with, probably not most efficient but does the job
function getProfilePic($userID){
$filename ='imgs/users/'.$userID.'.png';
if(file_exists($filename)){
return $img = $userID.'png';
exit();
}
$filename='imgs/users/'.$userID.'.jpg';
if(file_exists($filename)){
return $img = $userID.'jpg';
exit();
}
else{
return $img = 'default.jpg';
exit();
}
}
<img src="imgs/users/<?php echo getProfilePic($userID) ?>" style="width:100%" height="125px" />
You can use:
PHP file_exists() Function
Reference
https://www.w3schools.com/Php/func_filesystem_file_exists.asp
Or modify this code :)
<?php
function show_image($user_id){
if(file_exists("imgs/users/$user_id.png")){
return "<img src=\"imgs/users/$user_id.png\" style=\"width:100%\" height=\"125px\">";
}
else{
return "<img src=\"imgs/users/default.png\" style=\"width:100%\" height=\"125px\">";
}
}
echo show_image(1);
?>
You can use file_exists to check if the image exists, and then use the default image if that returns false.
At the time of image upload, make user-wise image. Means image name must be the name of username or id.
At the time of showing image put a if condition to check if logged-in user image exist using its id or name in file_exists. If not then load the default image.

Generating a link after uploading a file

edit: To sum it all up, I wanted to see how to make a website generate a link after a file has been uploaded.
First time posting here, I hope I'm not breaking the rules by asking this, haha.
I'm trying to set up an upload function on my website by following the tutorial on tizag, but I can't seem to figure out how to make it that after uploading, the user is presented with a link to the file (like how you would upload something to imgur and receive a short link like this imgur.com/k4FzF6W ).
Thank you for your help! I'm very new to php and I'm not the best at code in general.
This all depends on if you have the button to upload and stuff BUT here is just the basics of it, since I don't know what your code is.
$fileupload = uploadedfilenamehere;
function grabLink() {
echo "site.com/".$fileupload;
}
Which would return:
"site.com/uploadedfilenamehere"
And if you need to have a directiory just change this:
echo "site.com/dir/".$fileupload;

php delete image from server after upload & display

Sending an image in my php form, it gets saved onto my server and at the
action.php page it gets displayed. Now when I try to:
echo '<div id="image"><img src="'.$target_path.'" width="280" height="280"></div>';
it works just fine... but if I add unlink($target_path); at the end of my php code it
will not even display the image even though it gets deleted AFTER displaying the image...
So the question is, how can I display the image and deleting it at the same time so my server does not gets stuffed with user pictures?
Try another thing: output the image base-64 encoded.
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
echo '<div id="image"><img src="data:image/jpg;base64,'.$base64.'" width="280" height="280"></div>';
(instead of move_uploaded_file() etc, use as $file variable the $_FILES[...]['tmp_name'])
You can achieve this by creating a little script that gets the image-filename and will delete it after it has been retrieved:
<?php
$file = image_file_from_parameter($_GET['image']);
headers_for_file($file);
readfile($file);
unlink($file);
In your HTML output you then link to that script:
<img src="path/to/image.php?image=893sudfD983D" />
If you set nice caching headers, the user won't notice that your server did serve the file only once.
When you echo the url of an image with img src you're just sending the browser the url of an image, not the actual image data. The image needs to remain on the server if you want it to be viewable by this approach.
You could use bwoebi's solution to pass the actual image data instead of a link, but a better solution is just to keep the images on the server and periodically delete old files.

phpMyAdmin video not displaying

-Thanks for all the help folks. turns out it was indeed my own stupidity. Table column on local db was called 'url', on web-host it was 'urls'. Apologies for wasting everyone's time!
I'm trying to make a webpage display some videos, the paths to which are in a database.
The following piece of code works fine on the Localhost (I'm using MAMP) but when I upload it to the web-server, it displays the names of the videos OK but I get "No video with supported format and MIME type found." in Firefox. Chrome and Safari both stall while loading.
$result=mysql_query("SELECT * FROM videos");
while($row = mysql_fetch_array($result))
{
echo "<video width=\"600\" height=\"350\" controls=\"controls\">";
echo "<source src='".$row['url'].".mp4' type='video/mp4'/>";
echo "<source src='".$row['url'].".theora.ogv' type='video/ogg'/>";
echo "</video>";
echo "<br>" . $row['name'] ."<br/><br/>";
}
I guess it might be something obvious but I'm stumped.
Any help much appreciated.
thanks,
Robert.
Can you verify the video is in the database(name is one thing, is the file actually there?)? If using upload form, it is necessary to use enctype="multipart/form-data". I assume the videos are the format you are trying to request them as.
In your post you say "but when I upload it to the web-server", so the problem IS with the upload then, not the displaying of the videos? You seem to be saying you have a problem with the upload, but your code is the code retrieving it from the database.
So is the problem with the upload, or the display firstly.

Categories