Code to upload a file in PHP ?

devquora
devquora

Posted On: Apr 15, 2024

 

//PHP code to process uploaded file and moving it on server

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'];
	}
}

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    PHP Interview Questions

    What is T_PAAMAYIM_NEKUDOTAYIM in PHP?

    T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) .Basically, it used to call static methods/variables of a Class...

    PHP Interview Questions

    What is the difference between == and === operator in PHP ?

    In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type...