upload multiple file using for each loop php
Multiple File Upload with PHP and MySQL
In this tutorial we will make a multi-file Upload PHP script with verification for the file extension and size, to make a secure upload and save the file information into a MySQL database.
For this tutorial we will brand an image upload arrangement . This can be used to upload images, PDF's, Medico's, logs or any file types brand sure you do alter in the script.
If are new to file uploading yous tin check our article on Simple File Upload with PHP, to get started with basic.
The HTML
This is a simple HTML
forum, there won't exist whatsoever styles, since nosotros'r focusing on the PHP upload.
<form activity="" method="Mail" enctype="multipart/course-data"> <input blazon="file" name="files[]" multiple="" /> <input type="submit"/> </form>
Make sure to make add enctype="multipart/class-data
,type="file"
and most importantly name="files[]"
to enable multi files pick possible.
PHP
Moving on to the PHP codes. we tin get started with isset($_FILES['
in the if condition to brand sure some file is selected and we are practiced to go with the upload.
$_FILES[' ']
volition contain the information of each file, just when more that one file are selected it will accept the details of each file enclosed inside another array.
To meet how information is stored in array refer our article on Simple File Upload with PHP,.
To access them we will be using foreach
foreach loop.
foreach($_FILES['files']['tmp_name'] equally $key => $tmp_name ){ $file_name = $key.$_FILES['files']['proper noun'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$fundamental]; }
Now to make the verification part. First we will brand an array with the allowed extensions in it.
$extensions = array("jpeg","jpg","png");
As we are making an paradigm upload, nosotros need to permit simply the image extensions i.e. JPEG, PNG ... etc . You can add together the appropriate extensions that y'all need. To get the extension of the uploaded file we will use the file proper name not file Type, to go the extension we will use PHP explode()
& end()
.
Extensions can likewise exist in UPPER case or LOWER case to overcome the problem nosotros will get them converted into lower instance or upper example every bit you mentioned in the $extensions
array.
Using PHP in_array()
to verify if the uploaded file is allowed or non, it'due south most washed.
$file_ext=explode('.',$_FILES['paradigm']['proper noun'][$key]) ; $file_ext=cease($file_ext); $file_ext=strtolower(end(explode('.',$_FILES['paradigm']['proper name'][$key]))); if(in_array($file_ext,$extensions ) === false){ $errors[]="extension not allowed"; }
To limit the upload file size, $_FILES['epitome']['size']
is to be used to get the file size.
if($_FILES['image']['size'][$fundamental] > 2097152){ $errors[]='File size must be less tham two MB'; }
SQL
That is all with upload part, the files are prepare to be moved to a binder and store the file details in MySQL table'due south if necessary. In this tutorial nosotros use a MySQL table with 4 rows
CREATE Table `upload_data` ( `ID` int(5) Non NULL AUTO_INCREMENT, `USER_CODE` int(4) unsigned zerofill Non NULL, `FILE_NAME` varchar(200) Not NULL, `FILE_SIZE` varchar(200) NOT NULL, `FILE_TYPE` varchar(200) Not Cypher, PRIMARY Fundamental (`ID`) )
USER_CODE
is unique for each user if you lot have a user based application or any unique ID that can identify each items.
$query="INSERT into upload_data (`EMP_CODE`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$codes','$file_name','$file_size','$file_type'); "; # Make sure that the query is not executed unless the $errors array is empty. mysql_query($query);
Moving the Files
File uploaded volition be allocated space in the temporary location as mentioned in the php.ini. It is necessary to move the file from the temporary location to some other in lodge to utilize it once more. Y'all can become the file moved to another location using move_uploaded_file()
, in hither we will move information technology to an images
binder, make certain the directory exists, since move_uploaded_file()
cannot create a directory. And then it recommended to verify for existence of directory.
if(is_dir($dir)==false){ move_uploaded_file($file_tmp,images/".$file_name); }
If y'all program to create a directory for each user, y'all can employ mkdir(DIR Proper name,PERMISION)
if(is_dir($dir)==false){ mkdir("$dir", 0700); }
To Rename the file
Users can create a problem if there exists some other file with same name, since move_uploaded_file()
cannot motility the file if another file existed. You can get the file renamed or rename all the files uploaded with a series number which can foreclose this problem completely. With rename(OLD_FILE_NAME,NEW_FILE_NAME)
.
OLD_FILE_PATH file name with extension & location and NEW_FILE_NAME new proper noun.
#Cheque if a file of same proper noun exist if(is_dir("emp_data/$codes/".$file_name)==simulated){ move_uploaded_file($file_tmp,"$desired_dir/".$file_name); }else{ rename($file_tmp, "$desired_dir/".$file_name); # rename tin move the file along with renaming it }
Winding
You lot tin can utilise the following codes to upload files of whatever size & type, make sure y'all have enough space on the server. The consummate code for the project.
<?php if(isset($_FILES['files'])){ $errors= assortment(); foreach($_FILES['files']['tmp_name'] as $central => $tmp_name ){ $file_name = $cardinal.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$fundamental]; $file_tmp =$_FILES['files']['tmp_name'][$cardinal]; $file_type=$_FILES['files']['type'][$key]; if($file_size > 2097152){ $errors[]='File size must exist less than 2 MB'; } $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); "; $desired_dir="user_data"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if information technology does not exist } if(is_dir("$desired_dir/".$file_name)==fake){ move_uploaded_file($file_tmp,"user_data/".$file_name); }else{ //rename the file if some other one exist $new_dir="user_data/".$file_name.time(); rename($file_tmp,$new_dir) ; } mysql_query($query); }else{ print_r($errors); } } if(empty($error)){ echo "Success"; } } ?> <grade action="" method="POST" enctype="multipart/form-data"> <input type="file" proper noun="files[]" multiple="" /> <input type="submit"/> </form>
Download At present No Live Demo Source: https://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL
0 Response to "upload multiple file using for each loop php"
Post a Comment