Jump to content

Image Redirect To Other Image


Solar

Recommended Posts

<?php 
$req_user_info = $database->getUserInfo($req_user);
echo "<img src=\"http://URLHERE/photo/".$req_user_info['username']."\"\width=\"400\" height=\"300\" >";
?>

 

I have this script up and running, it shows the User's Profile Picture IF they have uploaded one, is there a code that could find in my folder "photo" the username's photo "Doesn't even have an extension", but if they don't have a PHOTO, redirect to a photo like called unknown.png.

 

I've got the start and don't know where to be pointed to, I don't ask anyone to write the code for me as to I would like to learn, but is greatly appreciated for help.

 

Solar

Link to comment
https://forums.phpfreaks.com/topic/174953-image-redirect-to-other-image/
Share on other sites

easy way forward

 

<?php 

$req_user_info = $database->getUserInfo($req_user);

if($req_user_info){

echo "<img src=\"http://URLHERE/photo/".$req_user_info['username']."\"\width=\"400\" height=\"300\" >";

}else{
echo "no pic";
}
?>

Works like a charm, I added a Exclamation mark because it wasn't working and re-arranged some wording;

 

Soultion:

<?php 

$req_user_info = $database->getUserInfo($req_user);

if(!$req_user){

echo "<img src=\"http://URLHERE/photo/".$req_user_info['username']."\"\width=\"400\" height=\"300\" >";

}else{
echo "no pic";
}
?>

 

Thanks again bro. I didn't like to do with mysql, thats why I needed coding like this.. Perfect.

The above only displays a "no pic" if ther is no user... what if there is a user, but no pic?

 

<?php
// open the specified directory and check if it's opened successfully
$dirPath = 'photo/';
if ($handle = opendir($dirPath)) {
// keep reading the directory entries 'til the end
	$npg='1';
	while (false !== ($file = readdir($handle))) {
	// just skip the reference to current and parent directory
		if ($file != "." && $file != "..") {
			if (is_dir("$dirPath/$file")) {
				// found a directory
				// do not think about it 
			} else {
				// found an ordinary file
				// use $file
				$req_user_info = $database->getUserInfo($req_user);
				if($req_user_info['username']===$file || !$req_user){
					echo "<img src=\"http://URLHERE/photo/".$file."\" width=\"400\" height=\"300\" >";
				}else{
					echo "no pic";
				}
			} 
	      }
	}
// ALWAYS remember to close what you opened
closedir($handle);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.