Please help. Is there any way to add a hyperlink to an image using a form before it's uploaded to a webpage? I would like to use a form to add links to my images so I don't have to add links to them manually because it would get time consuming. I also want to display a text caption link under the image. The image and the text caption will link to the same place. I want to use a form to add a link to an image and add a text link under the image before it's uploaded. I am using php to write the code.Thank you for your time.
This is photo of form before image is uploaded: upload form image
This is the uploaded image with text caption link under it:The upload image with link under it
This is the code:
<html>
<head>
<title>Upload and display images with PHP</title>
</head>
<?php
if(isset($_POST['upload_img'])) {
$file_name = $_FILES['image'] ['name'];
$file_type = $_FILES['image'] ['type'];
$file_size = $_FILES['image'] ['size'];
$file_tmp_name = $_FILES['image'] ['tmp_name'];
if($file_name) {
move_uploaded_file($file_tmp_name,"img/$file_name");
}
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label>upload Image</label><br><br>
<input type="file" name="image"><br><br>
<label>Add hyperlink to image:</label><br>
<input type="text" name="add_link"><br><br>
<input type="submit" value="Uplad Image" name="upload_img">
</form>
<?php
$folder = "img/";
//$dir = "img/";
if(is_dir($folder)) {
if($handle = opendir($folder)) {
while(($file=readdir($handle)) != false) {
if($file ==='.' || $file ==='..') continue;
echo '<img src="img/'.$file.'" width="150" height="150" alt="">';
}
closedir($handle);
}
}
?>
</body>
</html>