How to access contents of a file from the local folder?

Asked By 0 points N/A Posted on -
qa-featured

I am implementing online Database collection for a project on birds. I want the users to take photos around the birds and send them to me. In order to avoid email communication, I want to allow the users to upload the files to server themselves. I can make use of Google drive or drop box but since I have a space of decent GB in mu server itself, I want to know how to enable the end user of html page to upload a file into my server? Are there any built in functions for that in dream viewer?

SHARE
Answered By 590495 points N/A #194171

How to access contents of a file from the local folder?

qa-featured

If the space in your server is big enough to accommodate the contents a user can upload, you can add an upload function to your page using HTML and PHP. The upload form is handled by HTML while the actual uploading process is handled by PHP. The upload form will look something like this:

Here is the HTML code for the upload form:

<html>
<head>
<title>Upload File Using PHP and Save in Folder</title>
</head>
<body>
<h1>Upload File</h1>
<form name="form" method="post" action="" enctype="multipart/form-data" >
<input type="file" name="upload_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>
<p style="color:#FF0000;"><?php echo $uploadstatus; ?></p>
</body>
</html>

This is only the HTML upload form. When you click Browse and select a file and then click Upload, the form will call the PHP code to handle the upload process and checks if the file already exists. If the file already exists, it will display the message “Sorry, file already exists”. Here is the PHP code:

<?php
$uploadstatus = "";
if(isset($_POST['submit']) && ($_FILES['upload_file']['name']!=""))
{
$target_dir = "upload/"; // Where the file is going to be placed
$file = $_FILES['upload_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['upload_file']['tmp_name'];
$pathfilenameext= $target_dir.$filename.".".$ext;

// Check if file already exists
if (file_exists($pathfilenameext)) {
 $uploadstatus = "Sorry, file already exists.";
 }
else
 {
 move_uploaded_file($temp_name,$pathfilenameext);
 $uploadstatus = "File Uploaded Successfully.";
 }
}
?>

Here is the complete code for the PHP and HTML. Create an index.php file and put this code:

<?php
$uploadstatus = "";
if(isset($_POST['submit']) && ($_FILES['upload_file']['name']!=""))
{
$target_dir = "upload/"; // Where the file is going to be placed
$file = $_FILES['upload_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['upload_file']['tmp_name'];
$pathfilenameext= $target_dir.$filename.".".$ext;

// Check if file already exists
if (file_exists($pathfilenameext)) {
 $uploadstatus = "Sorry, file already exists.";
 }
else
 {
 move_uploaded_file($temp_name,$pathfilenameext);
 $uploadstatus = "File Uploaded Successfully.";
 }
}
?>
<html>
<head>
<title>Upload File Using PHP and Save in Folder</title>
</head>
<body>
<h1>Upload File</h1>
<form name="form" method="post" action="" enctype="multipart/form-data" >
<input type="file" name="upload_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>
<p style="color:#FF0000;"><?php echo $uploadstatus; ?></p>
</body>
</html>

To make this work, create a folder called “upload” in the same directory where index.php is located. The uploaded file will be saved in the “upload” folder.

Related Questions