//This is the minimal code for an image upload for first time learners
//html portion
<!DOCTYPEhtml><html><head><title>ImageUpload</title></head><body><formaction="upload.php"method="post"enctype="multipart/form-data"><label>Username</label><inputtype="text"name="username"><br><label>UploadImage</label><inputtype="file"name='myfile'><br/><inputtype="submit"value="upload"></form></body></html>
//php portion
<?php$user=$_POST['username'];$image=$_FILES['myfile'];echo"Hello $user <br/>";echo"File Name<b>::</b> ".$image['name'];move_uploaded_file($image['tmp_name'],"photos/".$image['name']);//here the "photos" folder is in same folder as the upload.php, //otherwise complete url has to be mentioned?>
<?php// Include the database configuration fileinclude'dbConfig.php';$statusMsg='';// File upload path$targetDir="uploads/";$fileName=basename($_FILES["file"]["name"]);$targetFilePath=$targetDir.$fileName;$fileType=pathinfo($targetFilePath,PATHINFO_EXTENSION);if(isset($_POST["submit"])&&!empty($_FILES["file"]["name"])){// Allow certain file formats$allowTypes=array('jpg','png','jpeg','gif','pdf');if(in_array($fileType,$allowTypes)){// Upload file to serverif(move_uploaded_file($_FILES["file"]["tmp_name"],$targetFilePath)){// Insert image file name into database$insert=$db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");if($insert){$statusMsg="The file ".$fileName." has been uploaded successfully.";}else{$statusMsg="File upload failed, please try again.";}}else{$statusMsg="Sorry, there was an error uploading your file.";}}else{$statusMsg='Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';}}else{$statusMsg='Please select a file to upload.';}// Display status messageecho$statusMsg;?>
<?php// connect to the database$conn=mysqli_connect('localhost','root','','file-management');// Uploads filesif(isset($_POST['save'])){// if save button on the form is clicked// name of the uploaded file$filename=$_FILES['myfile']['name'];// destination of the file on the server$destination='uploads/'.$filename;// get the file extension$extension=pathinfo($filename,PATHINFO_EXTENSION);// the physical file on a temporary uploads directory on the server$file=$_FILES['myfile']['tmp_name'];$size=$_FILES['myfile']['size'];if(!in_array($extension,['zip','pdf','docx'])){echo"You file extension must be .zip, .pdf or .docx";}elseif($_FILES['myfile']['size']>1000000){// file shouldn't be larger than 1Megabyteecho"File too large!";}else{// move the uploaded (temporary) file to the specified destinationif(move_uploaded_file($file,$destination)){$sql="INSERT INTO files (name, size, downloads) VALUES ('$filename', $size, 0)";if(mysqli_query($conn,$sql)){echo"File uploaded successfully";}}else{echo"Failed to upload file.";}}}
<?php// Include the database configuration fileinclude'dbConfig.php';$statusMsg='';// File upload path$targetDir="uploads/";$fileName=basename($_FILES["file"]["name"]);$targetFilePath=$targetDir.$fileName;$fileType=pathinfo($targetFilePath,PATHINFO_EXTENSION);if(isset($_POST["submit"])&&!empty($_FILES["file"]["name"])){// Allow certain file formats$allowTypes=array('jpg','png','jpeg','gif','pdf');if(in_array($fileType,$allowTypes)){// Upload file to serverif(move_uploaded_file($_FILES["file"]["tmp_name"],$targetFilePath)){// Insert image file name into database$insert=$db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");if($insert){$statusMsg="The file ".$fileName." has been uploaded successfully.";}else{$statusMsg="File upload failed, please try again.";}}else{$statusMsg="Sorry, there was an error uploading your file.";}}else{$statusMsg='Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';}}else{$statusMsg='Please select a file to upload.';}// Display status messageecho$statusMsg;?>
if(isset($_FILES['image'])){$img_name=$_FILES['image']['name'];//getting user uploaded name$img_type=$_FILES['image']['type'];//getting user uploaded img type$tmp_name=$_FILES['image']['tmp_name'];//this temporary name is used to save/move file in our folder.// let's explode image and get the last name(extension) like jpg, png$img_explode=explode(".",$img_name);$img_ext=end($img_explode);//here we get the extension of an user uploaded img file$extension=['png','jpeg','jpg','gif'];//these are some valid img extension and we are store them in array.