With an HTML form and PHP you can upload files to your web server. When you use $_FILE in PHP you are able to access information about the file stored in the $_FILE array.
Prerequisites:
- Basic LAMP, or Web Server Administration
- Verify folder has WRITE permission for OTHER
- Basic PHP
- Basic HTML
basename()
- https://www.php.net/manual/en/function.basename.php
- https://www.w3schools.com/php/func_filesystem_basename.asp
move_file_upload()
- https://www.php.net/manual/en/function.move-uploaded-file.php
- https://www.w3schools.com/php/func_filesystem_move_uploaded_file.asp
$_FILE
- https://www.php.net/manual/en/reserved.variables.files.php
- https://www.w3schools.com/php/php_file_upload.asp
LAMP Server
- Create a “pics” directory in the Public Web Directory (/var/www/html)
- Verify folder has at least a r– permission.
PHP.ini
- Location on demo system: /etc/php/7.2/apache2/php.ini
- Set File Uploads to: file_uploads = On
- Modify Max File size if needed: upload_max_filesize =2M
fileUpload.php
<?php
$dir = "./folder/";
$filename = $dir.basename($_FILES['Upload']['name']);
var_dump($_FILES);
echo "<br><br>";
if (move_uploaded_file($_FILES['Upload']['tmp_name'], $filename)){
echo "<p>File was uploaded --> ".$_FILES['Upload']['name'];
} else {
echo "Upload failed".$_FILES['Upload']['name'];
}
echo "<p>Information about file from $ FILE array</p>";
echo "File Name: ".$_FILES['Upload']['name']."<br>";
echo "File Type: ".$_FILES['Upload']['type']."<br>";
echo "File Size: ".$_FILES['Upload']['size']."kB<br>";
?>
fileUpload.php (with Timestamp Name)
<?php
$dir = "./folder/";
$timestamp = time();
$filename = $dir.$timestamp.basename($_FILES['Upload']['name']);
var_dump($_FILES);
echo "<br><br>";
if (move_uploaded_file($_FILES['Upload']['tmp_name'], $filename)){
echo "<p>File was uploaded --> ".$_FILES['Upload']['name'];
} else {
echo "Upload failed".$_FILES['Upload']['name'];
}
echo "<p>Information about file from $ FILE array</p>";
echo "File Name: ".$_FILES['Upload']['name']."<br>";
echo "File Type: ".$_FILES['Upload']['type']."<br>";
echo "File Size: ".$_FILES['Upload']['size']."kB<br>";
?>
fileUpload.html
<html>
<head></head>
<body>
<h1>File Upload Form</h1>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
<input type="file" name="Upload">
<input type="submit">
</form>
</body>
</html>
Be the first to comment