PHP Upload PDF file

 To dynamically upload a PDF file in a folder using PHP, you can use the following program:

php
<?php $target_dir = "uploads/"; // specify the directory where you want to upload the file $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // get the name of the uploaded file $uploadOk = 1; $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Allow only pdf files if($fileType != "pdf") { echo "Sorry, only PDF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>

This program first specifies the target directory where the uploaded file will be stored. It then checks if the file already exists in the target directory and whether the file type is a PDF. If everything is okay, it moves the uploaded file from its temporary location to the target directory using the move_uploaded_file() function. Finally, it outputs a message indicating whether the file was uploaded successfully or not.

Note: Don't forget to create the "uploads" directory in your web server and make sure that the directory has the appropriate read and write permissions

Comments