Friday, 17 May 2013

PHP file upload and Saving

PHP file upload and Saving:

                PHP file upload and saving in server is simple. We need to have a HTML form and PHP script to get the file and upload to server. Here i am going to tell you the step by step guidance to do this.

  HTML form:

<form action="file.php" method="post" enctype="multipart/form-data">
//First start aform tag, Method may be POST or GET. POST method is recommended since it is secured, Then encryption type
<label>File Name:</label>
//this is jus a label
<input type="file" name="myfile" id="myfile"><br>
//file is the tag to upload files
<input type="submit" name="submit" value="Submit">
//this is normal submit button to submit the form
</form>
//that's it

Now we have to write PHP script to do further things.

PHP Script(file.php); 

The following code is to just verify whether file is uploaded correctly or not. Later i will show you how to store the uploaded file into server.
<?php
if ($_FILES["myfile"]["error"] > 0)
//first check whether errors are there or not. If not then proceed
  {
  echo "Sorry Have some Error: " . $_FILES["myfile"]["error"] . "<br>";
//Displays error message and exact reason for error
  }
else
  {
//if no error then display details of file
echo "Well your file has been uploaded successfully in to server";
  echo "File name: " . $_FILES["myfile"]["name"] . "<br>";
  echo "Your File Type: " . $_FILES["myfile"]["type"] . "<br>";
  echo "Your File Size: " . ($_FILES["myfile"]["size"] / 1024) . " kB<br>";
  echo "Your file stored : " . $_FILES["myfile"]["tmp_name"];
  }
?>
That is it. Now we move on to PHP script to to store files in server Location

 PHP Script:(to store in file)

  As everyone do, We also Store files in Upload folder. Here is the code for that. Just copy and paste it after above code. If you do like this then you can able able to see uploaded file information and success message if uploaded correctly.
if (file_exists("upload/" . $_FILES["myfile"]["name"]))
//we must check whether file is Already exist or not. We must do it for hassle free operation
      {
      echo $_FILES["file"]["name"] . " already exists. ";
     //if it exist then we have to say exist
    // we can upload even if it is already exist by renaming it. I showed you the codes below for that
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["myfile"]["name"]);
//it is PHP function to move files to server
      echo "Stored in: " . "upload/" . $_FILES["myfile"]["name"];
//final message to echo stored location
      }

PHP script:(to rename before Store in folder)

 Always we need to rename the files before saving to server. Because users may upload files with same name. If that happens then the old file with same name gets replaced with new one. So we need to rename all files before uploading to server. For that use the below piece of code:  $randomdigit=rand(0000,9999);
//generate a randome number. You may use letters or alphanumeric also
$filename=$_FILES["myfile"]["name"];
//get the filen ame in to $filename
$newfilename=$randomdigit.$filename;
//Now combine random number and filename to get ne file name
$path= "resumes/".$newfilename;
$path variable holds the location of folder
copy($_FILES["file"]['tmpname'], $path);
//this php function can also upload files to server like move_uploaded_file

Check one more time whether new file name is exist in upload/ folder. If it exists the generate random number one more time and then copy to server.

That is it.

 

4 comments:

  1. Thanks for sharing wonderful information about PHP. The post was really interesting to read.
    PHP Web Development Company in Indore | PHP Website Design in Indore

    ReplyDelete

  2. Hi, probably our entry may be off topic but anyways, I have been surfing around your blog and it looks very professional. It’s obvious you know your topic and you appear fervent about it. I’m developing a fresh blog plus I’m struggling to make it look good, as well as offer the best quality content. I have learned much at your web site and also I anticipate alot more articles and will be coming back soon. Thanks you.

    Yii Development Company India

    ReplyDelete
  3. Reinforce Software Solutions is one the fastest growing website and software development company in Indore, India. Our team of expert developers work really hard to provide desires web and software solutions on time. We are also offering digital marketing services at affordable costs. for more information just explore our website or contact us.

    ReplyDelete

  4. Interesting blog. It would be great if you can provide more details about it. Thanks you
    PHP Developer Software in India

    ReplyDelete