single.php
First of all create a file open dialog in the front end with the name as file
.
<input id="name" name="file" type="file" accept="image/*" class="form-control w-50"/>
$target_dir = "../uploads/";
$filnm = rand(1, 1000);
$file_name = basename($_FILES["file"]["name"]);
$target_file12 = $target_dir . basename($_FILES["file"]["name"]);
$ext1 = pathinfo($target_file12);
$ext = $ext1['extension'];
if ($file_name != null) {
$target_file = $target_dir . $filnm . $file_name;
} else {
$target_file = $target_dir . basename($_FILES["file"]["name"]);
}
if (($ext == 'jpeg') || ($ext == 'png') || ($ext == 'gif') || ($ext == 'bmp') || ($ext == 'jpg') || ($ext == 'tiff') || ($ext == '')) {
move_uploaded_file($_FILES['file']["tmp_name"], $target_file);
} else {
echo “Not an image file”;
}
Here the variable target_dir
is the folder where you want to copy the file. Mention the directory here.
move_uploaded_file
is the function to move the file from source to destination. This function accepts two parameters, first one is source file and the second one is destination directory.
If you want to move a file from one place to another, No need to write the whole code. You can just write,
move_uploaded_file(“source_file”,”destination_folder_address”);