Skip to main content

Posts

Showing posts from January, 2024

How to upload image and PDF file using PHP

Here is a PHP script that allows users to upload image and PDF files with a maximum size of 5 MB. The uploaded files will be renamed using the current timestamp: <?php if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" && isset ( $_FILES [ "file" ])) { $allowedExtensions = array ( "jpg" , "jpeg" , "png" , "pdf" ); $maxFileSize = 5 * 1024 * 1024 ; // 5 MB in bytes $targetDirectory = "uploads/" ; $timestamp = time (); $targetFileName = $timestamp . "_" . basename ( $_FILES [ "file" ][ "name" ]); $targetPath = $targetDirectory . $targetFileName ; $fileExtension = strtolower( pathinfo ( $targetFileName , PATHINFO_EXTENSION)); if ( in_array ( $fileExtension , $allowedExtensions ) && $_FILES [ "file" ][ "size" ] <= $maxFileSize ) { if ( move_uploaded_file ( $_FILES [