Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

File Upload using PHP

To upload a file, we need a upload form with three basic parts.
First one ,a hidden filed to specify maximum size of the file.
Second field allow user to browse or type the name of the file.
And third one a submit button.
An example of such form is given below.

  <form name=”upload_form” method=”post”  action=”upload.php”>
	<input type="hidden" name="MAX_FILE_SIZE" value="1000" />
		<input name="upload" type="file" />
		<input type=”submit” value=”upload”/>
  </form>

After submitting the file php will upload the file to a desired directory.
PHP use $_FILE array, move_uploaded_file and basename functions for uploading.

If we are considering above form , after submitting the form, $_FILE['upload'] would have file information like name of the file and temporary name of the file. Where ‘upload’ is name of the file input filed

<input name="upload" type="file" />

move_uploaded_file function will upload the file, it needs two parameter . 1)temporary name of the file and 2 ) target path. move_uploaded_file returns true on success and false on failure.
A sample php code (upload.php) that uploads a file is given below.

<?php
		$target_path =   “uploads/”. basename( $_FILES['upload']['name']);
		if(move_uploaded_file($_FILES['upload']['tmp_name'],  $target_path))
			echo “File uploaded successfully”;
		else
			echo “Unable to upload the file”;
	?>

Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment