Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Last active November 17, 2019 01:48
Show Gist options
  • Save remoharsono/555d05f44a5e4589c00189d6a9f0a83f to your computer and use it in GitHub Desktop.
Save remoharsono/555d05f44a5e4589c00189d6a9f0a83f to your computer and use it in GitHub Desktop.
PHP :: Basic File Upload
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>.</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
define("UPLOAD_FOLDER", "uploads");
$document_root = $_SERVER['DOCUMENT_ROOT'];
$uploadFolder = $document_root . '/' . UPLOAD_FOLDER . '/';
$numFiles= count($_FILES['myfile']['name']);
for ( $counter=0 ; $counter < $numFiles ; $counter++ ) {
$tmpFilePath = $_FILES['myfile']['tmp_name'][$counter];
if ($tmpFilePath != ""){
$newFilePath = $uploadFolder . $_FILES['myfile']['name'][$counter];
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
echo 'Succeed uploading ' . $_FILES['myfile']['name'][$counter] . '<br>';
} else {
echo 'Failed uploading ' . $_FILES['myfile']['name'][$counter] . '<br>';
}
}
}
} else {
?>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="myfile[]" id="myfile" multiple="">
<input name="submit" type="submit" value="Upload">
</form>
<?php
}
?>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>.</title>
</head>
<body>
<?php
function upload($uploadFile) {
$document_root = $_SERVER['DOCUMENT_ROOT']; // C:/xampplite/htdocs
$uploadFolder = $document_root . '/' . UPLOAD_FOLDER . '/';
$numFiles= count($uploadFile);
for ( $counter=0 ; $counter < $numFiles ; $counter++ ) {
$tmpFilePath = $uploadFile['tmp_name'][$counter];
if ($tmpFilePath != ""){
$newFilePath = $uploadFolder . $uploadFile['name'][$counter];
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
echo 'Succeed uploading ' . $uploadFile['name'][$counter] . '<br>';
} else {
echo 'Failed uploading ' . $uploadFile['name'][$counter] . '<br>';
}
}
}
}
if (isset($_POST['submit'])) {
define("UPLOAD_FOLDER", "uploads");
upload($_FILES['myfile']);
} else {
?>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="myfile[]" id="myfile" multiple="">
<input name="submit" type="submit" value="Upload">
</form>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment