Previous Classes in Series:
Prerequisites:
- Basic LAMP, or Web Server Administration
- Verify pictures and folder have READ/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 pictures and folder have at least a rw- permission.
PHP.ini
- Location on demo system: /etc/php/7.2/apache2/php.ini
- Set File Uploads to: file_uploads = On
upload.php
<?php
$saveDir = "./pics/";
$saveFile = $saveDir.basename($_FILES['fileToUpload']['name']);
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $saveFile)){
echo "<h1>Picture Uploaded</h1><br>".$_FILES['fileToUpload']['name']." was saved<br>";
echo "<img src='".$saveFile."'><br><br>";
} else {
echo "Upload Did Not Work<a href='./index.php'> Go Back</a>";
}
echo "<h3>Diagnostic Info:</h3>";
echo "<br>Tmp File Name: ".$_FILES['fileToUpload']['tmp_name']."<br>";
echo "saveFile Variable Valuable: ".$saveFile;
?>
<h1><a href="./index.php">Back to Gallery</a></h1>
uploadForm.html
<html>
<head>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Image you want to upload:
<br>
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<input type="submit">
</form>
</body>
</html>
Be the first to comment