Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created February 28, 2018 12:49
Show Gist options
  • Save rintoug/4d16735a1765f45248c1b5e7f79a133b to your computer and use it in GitHub Desktop.
Save rintoug/4d16735a1765f45248c1b5e7f79a133b to your computer and use it in GitHub Desktop.
Uploading files in PHP
<?php
//Here is our upload script resides
//Check whether the form really submits
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(!empty($_FILES['image']['name'])) {
$uploadDir = './uploads/';
$uploadFilename = $uploadDir.basename($_FILES['image']['name']);
//Check extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,array('jpg','png'))) {
echo "Oops. Invalid file type";
exit;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
echo "File Uploaded successfully!";
}
else {
echo "Oops!. something went wrong!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment