Return to Snippet

Revision: 73412
at July 8, 2018 05:21 by devquora


Initial Code
<?php
if($_FILES['photo']['name'])
{
	//if no errors...
	if(!$_FILES['file']['error'])
	{
		//now is the time to modify the future file name and validate the file
		$new_file_name = strtolower($_FILES['file']['tmp_name']); //rename file
		if($_FILES['file']['size'] > (1024000)) //can't be larger than 1 MB
		{
			$valid_file = false;
			$message = 'Oops!  Your file\'s size is to large.';
		}
		
		//if the file has passed the test
		if($valid_file)
		{
			//move it to where we want it to be
			move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$new_file_name);
			$message = 'File uploaded successfully.';
		}
	}
	//if there is an error...
	else
	{
		//set that to be the returned message
		$message = 'Something got wrong while uploading file:  '.$_FILES['file']['error'];
	}
}
 
  ?>

Initial URL
https://www.onlineinterviewquestions.com/code-to-upload-file-in-php/

Initial Description
Code to upload a file in PHP

Initial Title
Code to upload a file in PHP

Initial Tags
php, file, code

Initial Language
PHP