PHP Upload File


PHP File Upload :– In this tutorial you will see how to upload a file on remote server.You can upload any kind of file like images, videos, ZIP files, Microsoft Office documents, PDFs, as well as executables files and a wide range of other file types.

We have given the best suitable example for understanding file uploading approach in detail.


PHP $_FILES:

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.

For example $_FILES[‘filename’][‘tmp_name’] returns temporary file’s folder where it will store i.e. c://xampp?temp.

move_uploaded_file() function:

Most oftenly when we upload file ;it will go into temp folder in c://xampp//temp and now move_uploaded_file() function comes into picture and sends file from temp folder to remote server.

In below example one thing to remember is that we have created a folder named “uploads” where our jellyfish.jpg file will be uploaded.

PHP File Upload | Example

Example

<html>
<body>
<form action="uploadfile.php" method="POST" enctype="multipart/form-data">
Select File:  
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>  
</form>
</body>
</html>

Example

<?Php
$target_path = "./uploads";  
$temp_dir=$_FILES['fileToUpload']['name'];
echo $temp_dir;
echo "<br>";
$tempdir=$_FILES['fileToUpload']['tmp_name'];
echo $tempdir;
echo "<br>";
echo $target_path = $target_path.'/'.basename( $_FILES['fileToUpload']['name']); 
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) { 
    echo "File uploaded successfully!";  
	
} else{  

    echo "Sorry, file not uploaded, please try again!"; 	
}  
?>

Advertisements

Add Comment

📖 Read More