Html upload file to directory

Upload files and directories using an input, drag and drop, or copy and paste with HTML5

Many websites have a form to upload a file. For some sites, such as OneDrive or Google Drive, uploading files is a major feature. As time went on, webpages are more and more interactive. So, users expect more interactions such as dragging and dropping files or directories or pasting screenshots.

Let’s see what browsers provide to upload files!

#Basic file input

The basic way is by using a form and an input with the file type:

  • multiple : allows multiple files to be selected
  • accept : choose expected mime types, for instance: image/* , application/pdf

#Drag and drop files

Most users expect to be able to drag and drop files on the page. This functionality is well-supported by browsers and very easy to implement. Once you get the files, you need to do something such as uploading them to an API or read their content using the FileReader api.

document.body;  dropZone.e) < e.classList.); dropZone.e) < e.classList.); dropZone.e) < e.classList.); dropZone.e) < e.classList.dataTransfer.files); console.  >); >

You can easily upload files to an API using the fetch API :

length > 0) < ) .() => console.)) .reason => console.

Google Chrome and Microsoft Edge now support dropping directories. This is very useful when you want to upload your files to OneDrive or Google Drive. The API is not very convenient as it uses callback functions with a recursive tree, but it’s not that hard. Let’s see some code:

 dropZone.e) < e.classList.console.dataTransfer: DataTransfer) < 0; i < dataTransfer.items.length; i++) < items[i]; kind === webkitGetAsEntry ===  > >  entry: FileSystemEntry) < (resolve, reject) => < 0; entry: FileSystemEntry) < file => < reading--; contents.0) < >); > > reader: FileSystemDirectoryReader) < reading++; reader.entries) < reading--; 0) < >); > >); >  entry: FileSystemEntry): entry is isDirectory; > entry: FileSystemEntry): entry is isFile; >

The webkitGetAsEntry function also works in Microsoft Edge.

#Paste file

Currently, you can only paste images, not image files nor any other types of file. This may change in the future, but at the moment this is enough to upload screenshots. I often paste images when creating an issue on JIRA or VSTS 😃

Supporting image pasting is a quick win because you can reuse the previous code. Indeed, you can simply handle the paste event and use the previous getFilesAsync function:

e) < e.clipboardData); console.You are now masters in uploading files and directories using all the possible ways your browser support (at the time of writing)!

Do you have a question or a suggestion about this post? Contact me!

Источник

How to upload a file using PHP.

This is a tutorial on how to upload a file using PHP. In this article, we will create two things:

  1. The HTML form that will allow the user to select a file to upload.
  2. The PHP script that will receive the uploaded file, validate it, and then move it to a directory of our choosing.

Let’s start off by creating the HTML form.

The HTML upload form.

Take a look at the following HTML form:

There are a few important things to note about the form above:

  • The enctype attribute on our form element has been set to multipart/form-data. This is extremely important, as our upload form will not work without it. By setting this attribute to multipart/form-data, we are telling the browser that it should notencode the form data.
  • The action attribute has been set to upload-script.php. As a result, our HTML form will submit the uploaded file to upload-script.php. i.e. This is the PHP script that we will need to create in order to manage our file upload.
  • We set the method attribute to post. This is also extremely important, as it tells our browser that we want to send a POST request. If we fail to set this attribute, the form will send the data as a GET request and our file will not be uploaded.
  • We created an input element with type=”file” and named it user_file. This is the form element that the user can click on in order to select a file from their file system.
  • Finally, we created a simple submit button.

If you view this form in your browser, you will see that it looks pretty basic:

HTML upload form

A screenshot of our upload form.

If you choose a file and submit it, you will probably receive a 404 Not Found error for upload-script.php. This is because the PHP file that will handle our upload doesn’t exist yet.

The PHP upload script.

In our HTML form above, we set the action attribute to upload-script.php. Now we will need to create that file and insert some PHP code to handle our upload.

Take a look at the following script, which I have heavily commented:

//The folder that our file should be moved to //once it is uploaded $uploadDirectory = 'uploads/'; //If our uploaded file exists. if(isset($_FILES['user_file'])) < //The extensions that are allowed to be uploaded. $allowedExtensions = array( 'png', 'jpg', 'jpeg', 'csv', 'xls' ); //Get our file details from the $_FILES array. $file = $_FILES['user_file']; //The name of the file that has been uploaded. //Example: image.png or data.csv //This is what the user named it. $name = $file['name']; //Get the extension of the file. $extension = pathinfo($name, PATHINFO_EXTENSION); //Force the file extension into a lower case. $extension = strtolower($extension); //If it isn't a valid extension. if(!in_array($extension, $allowedExtensions))< //Print out a message to the user. echo 'That extension is not allowed!'; >else < //If it is a valid extension, move it to our uploads directory. //The current / temporary location of the uploaded file. //We will need to move it from here to our uploads directory. $tmpLocation = $file['tmp_name']; //The location that we want to move the uploaded file to. $newLocation = $uploadDirectory . $name; //Move the file using PHP's move_uploaded_file function. $move = move_uploaded_file($tmpLocation, $newLocation); >>

There are a few important things to note about the PHP code above.

The upload directory.

At the top of our PHP script, we set the $uploadDirectory to uploads/. This is the location where our uploaded files will be moved to if the upload is successful. Note that this can be a relative path or an absolute path.

An example of an absolute path:

//Absolute path to our uploads directory. $uploadDirectory = '/var/www/html/uploads/';

You can also use a relative path to reference a folder in the parent directory:

//Relative path to a folder called uploads in //the parent directory $uploadDirectory = '../uploads/';

Currently, our upload-script.php assumes that it is located in the same directory as the uploads folder.

Checking the file extension.

Using a PHP array called $allowedExtensions, we created a whitelist of valid file extensions. This is for security purposes. For example, what if a hacker decided to upload a PHP file?

That would lead to disastrous results, as you’d be giving them free reign to execute their PHP code on your server.

Note that you should feel free to add or remove extensions from this array to suit your own needs. However, I would strongly suggest that you do not remove this file extension check completely.

The $_FILES array.

The $_FILES array is a superglobal array that contains information about any files that have been uploaded to the server. In the code above, we were able to get the details of our uploaded file by using the name of the file input in our HTML form:

//Get our file details from the $_FILES array. $file = $_FILES['user_file'];

If you take another look at our HTML form, you will see that we gave our upload field the name user_file. As a result, you will need to use that name in order to retrieve the file’s details from the $_FILES array.

Below is a screenshot of a var_dump of the file’s details:

<img decoding=

FILES: name

The name field contains the name of the file, as it appeared on the user’s file system. For example: In the screenshot above, you can see that the PNG on my file system was called html-upload-form.png.

FILES: type

The type field represents the MIME type of the file that has been uploaded. It is important to note that the MIME type field can be easily spoofed by the client. For example: An attacker could upload a PHP file and manually set the MIME type to a JPEG image.

As a result, you cannot trust this field.

FILES: tmp_name

This is the temporary location of the file. When a file is first uploaded, it is renamed with a .tmp extension and saved in your server’s temporary directory. Therefore, it is up to you to move that file into your chosen directory.

FILES: error

If no errors occur during the upload process, then the error field will be set to 0. Here is a quick drill-down of what each number means:

  • 0: No error occurred. The file was uploaded successfully.
  • 1: The file was too big and it exceeded the size limit set in the upload_max_filesize directive in your php.ini file. If this is the case, then you might need to increase PHP’s max upload size limit.
  • 2: You used the MAX_FILE_SIZE directive in your HTML form and the uploaded file exceeded this limit.
  • 3: The file wasn’t fully uploaded. For example: The client’s connection with the web server timed out mid-upload or they navigated away from the page before the upload was complete.
  • 4: The user pressed the submit button on your HTML form without choosing a file to upload.
  • 6: Your PHP installation has not specified a temporary directory.
  • 7: PHP was unable to save the uploaded file to your server’s temporary directory. This could be because the directory isn’t writable or worse, your file system is full or has run out of inodes.
FILES: size

This field represents the size of the file in bytes.

The move_uploaded_file function is safe.

I have seen a few examples where developers have wrapped their move_uploaded_file function inside an is_uploaded_file check:

if(is_uploaded_file($tmpLocation))

Although the above code will work, it is redundant. This is because PHP’s move_uploaded_file function already carries out this check for you. As a result, there really is no need to use the is_uploaded_file function in this case.

PS: If you run into issues with this example not working, then take a look at my article on how to fix a HTML form that is not uploading.

Hopefully, you found this guide useful!

Источник

Html upload file to directory

Gray Pipe

Answered by:

Question

User219039814 posted
I have an HTML file type tag. I want to save the images in a specific folder in the client system. How can i do it.

Answers

User1724605321 posted
Hi vijaylakshmi, Because of security restrictions we can’t save any files client side. The user needs to actively save the file (allowing with clicking «Save As» button in browser) . I assume you want to upload the files to server side folder . With Html file tag , you can try :

protected void btnUploadClick(object sender, EventArgs e) < HttpPostedFile file = Request.Files["myFile"]; //check file was submitted if (file != null && file.ContentLength >0) < string fname = Path.GetFileName(file.FileName); file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname))); >>

User1724605321 posted
Hi vijaylakshmi, In webforms you can use ajax to call server side function and use httphandler on server side as shown in @vahid bakkhi’s link . And here is mvc version : https://www.c-sharpcorner.com/UploadFile/manas1/upload-files-through-jquery-ajax-in-Asp-Net-mvc/ Best Regards, Nan Yu

User-369506445 posted
in below code, I highlighted the newFilename that you can define your custom name Html:

input type="file" id="uploadFile" name="uploadFile"/> input type="button" id="submit" value="Submit" onClick="UploadFile();"/> 
 script type="text/javascript"> $(document).ready(function ()  function UploadFile()  var fileName = $('#uploadFile').val().replace(/.*(\/|\\)/, ''); if (fileName != "")  $.ajaxFileUpload( url: 'AjaxFileUploader.ashx', secureuri: false, fileElementId: 'uploadFile', dataType: 'json', success: function (data, status)  if (typeof (data.error) != 'undefined')  if (data.error != '')  alert(data.error); > else  alert('Success'); > > >, error: function (data, status, e)  alert(e); > > ) > > >);
 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Text.RegularExpressions; using System.Text; namespace MyProject  public class AjaxFileUploader : IHttpHandler   public void ProcessRequest(HttpContext context)  if (context.Request.Files.Count > 0)  string path = context.Server.MapPath("~/UploadImages"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); var file = context.Request.Files[0]; string fileName; if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")  string[] files = file.FileName.Split(new char[]  '\\' >); fileName = files[files.Length - 1]; > else  fileName = file.FileName; > string newFilename = Guid.NewGuid().ToString(); FileInfo fInfo = new FileInfo(fileName); newFilename = string.Format("", newFilename, fInfo.Extension); string strFileName = newFilename; fileName = Path.Combine(path, newFilename); file.SaveAs(fileName); string msg = "; msg += string.Format("error:'',\n", string.Empty); msg += string.Format("msg:''\n", strFileName); msg += ">"; context.Response.Write(msg); > > public bool IsReusable  get  return true; > > > >

Источник

Читайте также:  Java outofmemoryerror heap dump
Оцените статью