What are the best practices for integrating images and URLs in a PHP forum?

When integrating images and URLs in a PHP forum, it is important to properly sanitize and validate user input to prevent security vulnerabilities such as cross-site scripting attacks. Additionally, it is recommended to store images in a secure directory outside the web root to prevent direct access. Finally, use HTML image and anchor tags to display images and URLs in the forum posts.

// Sanitize and validate user input for image and URL
$image = filter_var($_POST['image'], FILTER_SANITIZE_URL);
$url = filter_var($_POST['url'], FILTER_SANITIZE_URL);

// Store images in a secure directory outside the web root
$upload_dir = '/path/to/secure/directory/';
$upload_file = $upload_dir . basename($_FILES['image']['name']);

// Display images and URLs using HTML image and anchor tags
echo '<img src="' . $image . '" alt="Image">';
echo '<a href="' . $url . '">Link</a>';