Check if file is of type image PHP

Check if file is of type image PHP
0

#1

still working on the free code camp nightlife co-ordination app,
So i want to be able to allow users to post images to the conversation threads in each bar/location. Dont worry if using the filesystem is worse off than using a database for this purpose, for now im following the curriculum of a book and the chapter is teaching the filesystem.
The exercise in the book teaches a simple file upload, I added in my own code to store all the files in said directory into an array and print them out, its working however its trying to output all the files and not just images (because right now its just checking if its of type file and that its not the current directory or hidden directory, this i understand).
I saw some things on the web saying to use finfo_file() and another saying to use mime_content_type(), and other people say to use glob()
what is the difference between these two functions?
so I would add something like this in the foreach loop? AND mime_content_type($item)== image?
the code should validate all types of images.

edit: I just realized this should be posted in the general section and not the help section

<!doctype html>

<html lang="en">

<head>

	<meta charset="utf-8">

	<title>Upload a File</title>

</head>

<body>

<?php // Script 11.4 - upload_file.php

/* This script displays and handles an HTML form. This script takes a file upload and stores it on the server. */



if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.



	// Try to move the uploaded file:

	if (move_uploaded_file ($_FILES['the_file']['tmp_name'], "../uploads/{$_FILES['the_file']['name']}")) {

//added code below
		print '<p>Your file has been uploaded.</p>';
		echo '<pre>';
		print_r($_FILES);
		echo '</pre>';
	
	$search_dir = '../uploads';

		$contents = scandir($search_dir);
		echo '<pre>';
		print_r($contents);
		echo '</pre>';
	
//added code above

	} else { // Problem!



		print '<p style="color: red;">Your file could not be uploaded because: ';

		

		// Print a message based upon the error:

		switch ($_FILES['the_file']['error']) {

			case 1:

				print 'The file exceeds the upload_max_filesize setting in php.ini';

				break;

			case 2:

				print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';

				break;

			case 3:

				print 'The file was only partially uploaded';

				break;

			case 4:

				print 'No file was uploaded';

				break;

			case 6:

				print 'The temporary folder does not exist.';

				break;

			default:

				print 'Something unforeseen happened.';

				break;

		}

		

		print '.</p>'; // Complete the paragraph.



	} // End of move_uploaded_file() IF.

	

} // End of submission IF.



// Leave PHP and display the form:

?>



<form action="upload_file.php" enctype="multipart/form-data" method="post">

	<p>Upload a file using this form:</p>

	<input type="hidden" name="MAX_FILE_SIZE" value="10000000">

	<p><input type="file" name="the_file"></p>

	<p><input type="submit" name="submit" value="Upload This File"></p>

</form>

//kravmaguy added code below this line
<?php
foreach ($contents as $item) {

	if ( (is_file($search_dir . '/' . $item)) AND (substr($item, 0, 1) != '.') ) {
		print "<img src=\"../uploads/$item\"
		
		
		width='100'>";
	} 
}
	?>


  <img src="../uploads/<?php echo $_FILES['the_file']['name']?> " alt="content_img" width="100">


</body>

</html>