Sample upload file code
The following code is a sample to upload a file.
Create a folder name "upload".
Save the following codes into "upload.php".
- <?php
- //create 'upload' directory if not yet created
if (!is_dir('upload')) {
mkdir('upload', 0777, true);
}
$target_file = "upload/".$_FILES["fileToUpload"]["name"];
- //$target_file can be saved into database table to relate to specific user data
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- echo "<a href='$target_file'></a>";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- ?>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
Last modified: Saturday, 23 February 2019, 10:53 AM