PHP Project (Simple) – Image Upload App

Previous Classes in Series:

  1. PHP Project (Simple) – Dynamic Photo Gallery Stream
  2. PHP Project (Simple) – Dynamic Photo Gallery

Prerequisites:

  • Basic LAMP, or Web Server Administration 
    • Verify pictures and folder have READ/WRITE permission for OTHER
  • Basic PHP
  • Basic HTML

basename()

move_file_upload()

$_FILE

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

Leave a Reply