PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: $_FILES['file']['tmp_name']
$_FILES['file']['tmp_name'] - phponwebsites.com
Showing posts with label $_FILES['file']['tmp_name']. Show all posts

1 Jun 2015

Upload multiple files/images using PHP

                        You can upload single file and image easily in PHP. Now we are going to know how to upload multiple files and images using PHP. You can upload more than one images or files at the time using PHP. It can done using by array concepts.

Form for upload multiple images:

               
                     First you need to make form for upload more images. Your form should be like following:

<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" id="file" multiple="multiple">
<input type="file" name="file[]" id="file" multiple="multiple">
 <input type="submit" id="submit" name="submit" value="submit">
 </form>

                  You should add enctype="multipart/form-data" for upload files. You can see above where the name of file tag should be array like name="file[]" for store multiple values.

Upload multiple images using PHP:


                       Similarly upload single image, you will get image values by $_FILES['file']['name'] in PHP while submitting a form. You need temporary name of image to pass one of the parameter in move_uploaded_file() in order to upload files. If you've a single image, then you can upload easily by move_uploaded_file($_FILES['file']['tmp_name'],$name). But you need to upload multiple images. So you've to get values as an array. Then upload each image using loop. Consider the following example. Here it describes how to get temporary name of each image one by one using loop in PHP.


<?php
if(isset($_POST['submit']))
{
 $name=$_FILES['file']['name'];
 foreach($name as $val => $name1)
 {
   $file_name=$name1;
   $tmp=$_FILES['file']['tmp_name'][$val];
  move_uploaded_file($tmp,$_SERVER['DOCUMENT_ROOT'].'img/'.$file_name);
 }
}

?>
<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<table>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="submit" id="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>

             
     
     Where,
                move_uploaded_file() - to upload files to desired location.
                $_SERVER['DOCUMENT_ROOT'] gives the root directory of current script executing.
You can upload multiple files as well as images using this PHP script.

Related Posts:


30 Oct 2014

Upload images to mysql database using php

                         In this method, You won't need to store images in mysql database. Just store image name in mysql database and store image in particular directory. Then you can retrieve image from particular directory using image name in database. You can done it using php.

Create table using mysql query:


                create table image(
                     no int(4) AUTO_INCREMENT, img_name varchar(30), 
                     PRIMARY KEY(no) )
Now the table 'image' is created. Then you can store values to mysql database using php.

Upload images using php and mysql:


                  The php script for upload images to mysql database is:


<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
</form>
<?php
 if(isset($_POST['submit']))
 { 
   mysql_connect('localhost','root','');
   mysql_select_db('new');
   $name=$_FILES['file']['name'];
   $type=$_FILES['file']['type'];
   if($type=='image/jpeg' || $type=='image/png' || $type=='image/gif' || $type=='image/pjpeg')
   {
if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name))
     {
      echo'file is already present';
     }
     else
     {
      $up=move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name);
      $q=mysql_query("insert into image values('','".$name."')");
  if($up && $q)
  {
   echo'image uploaded and stored';
  }
  elseif(!$up) 
  {
   echo'image not uploaded';
  }
  elseif(!$q)
  {
   echo'image not stored';
  }
 }
   }
   else
   {
    echo'Invalid file type';
   }
 }
?>
</body>
</html>


        where,
                   $_FILES['file']['type'] is used to return the type of uploaded file.
                   move_uploaded_file() is used to upload the file.
You want to know about upload files in php.
                    if($type=='image/jpeg' || $type=='image/png' || $type=='image/gif' || $type=='image/pjpeg' ) is used to check the file type whether it is image or not. If it is image, then it is stored. Otherwise, it is not upload and also stored.
                   Now you can see the directory, the image is stored here and also stored in mysql database.

29 Oct 2014

Upload files using PHP

                       You can see upload files at all social media sites to upload your photos. You can upload files using php. You can upload both text and binary files in php. You can upload any files such as .txt, .png, .jpg, .gif, .doc, .csv. At the same time you control the file extensions and file size while uploading files using php. For example, you allows the user can upload only .doc and docx file with particular file size.

PHP script for upload file:


                       Your form should be like this:

<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>

                where,
                          enctype="multpart/form-data  -  must be present in form. Otherwise file does not upload.
                         type="file" in <input>  tag is used to take input as a file.

The php script for upload file is:

<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
</form>
<?php
 if(isset($_POST['submit']))
 {
   $name=$_FILES['file']['name'];
   if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name))
   {
    echo'file is already present';
   }
   else
   {
    move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name);
     echo'file added';
}
 }
?>
</body>
</html>

  where,

  

$_FILES:


                   It is the php global variable. It keeps information about uploaded file like below.
  $_FILES['file']['name'] - name of uploaded file.
  $_FILES['file']['type'] - type of uploaded file.
  $_FILES['file']['size'] - size of uploaded file.
  $_FILES['file']['tmp_name'] - temporary name of uploaded file which is in server.
  $_FILES['file']['error'] - errors while uploadeding file.
 ' file' is the name of <input> tag
  $_SERVER['DOCUMENT_ROOT'] - return the directory.
  move_uploaded_file()
 - uploaded file to specified directory.

                  Now you can upload any file without any restriction using php. The file is stored in '/upload/file/' directory already if it is not present in the directory. Otherwise you'll get error message ' file is laready present'.