Check If A Users Picture Exists Using PHP

Today We will use PHP to check if a users picture exists.



Let’s get started!
First We set the users and default picture location into a variable. Next We check if the users picture exists. If the users picture exists We will display it. If not We show the default picture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*The location to the default picture*/
$no_picture="default.jpg";

/*The location to the users picture*/
$picture="$user/picture.jpg";

/*We will use the PHP file_exists function*/
if(file_exists($picture)){
/*If users picture exists show it*/
$pic='<img src="'.$picture.'" alt="" />';
}else{
/*If users picture dose not exists shoe default picture instead*/
$pic='<img src="'.$no_picture.'" alt="" />';
}

/*Usage*/
echo $pic

 

Used in a function
We can also put it in a Function. Like this!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function UserPic($user){
/*The location to the default picture*/

$no_picture="default.jpg";
/*The location to the users picture*/
$picture="$user/picture.jpg";

if(file_exists($picture)){
$pic='<img src="'.$picture.'" alt="" />';
}else{
$pic='<img src="'.$no_picture.'" alt="" />';
}
return $pic;
}

/*Usage*/
UserPic($user)

ADVERTISEMENT

Tags: , , ,

Enjoyed this Post?

Subscribe to our RSS Feed, Follow us on Twitter or simply recommend us to friends!

Discussion