Solved

PHP Image Upload Loop

Posted on 2014-11-18
4
136 Views
Last Modified: 2014-11-18
Hello,

I have 2 file uploads, I tired this and it worked perfectly out of the box.
https://gist.github.com/zvineyard/3530917

My question is where do I put the loop if I have 2 file inputs.

<input type="file" id="file" name="file" type="file" />
<input type="file" id="file2" name="file2" type="file" />

I should probably make it like:

<input type="file" id="file" name="file[]" type="file" />
0
Comment
Question by:movieprodw
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:Greg Alexander
Greg Alexander earned 250 total points
ID: 40450352
Perhaps something like this... you can keep the static html however you want:

Note: Added a foreach loop for the incoming files on line 4 and renamed the variables to be dynamic.

if (isset($_POST['submit']))
{
	
	foreach($_FILES as $file){
		$filename = $file["name"];
		$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
		$file_ext = substr($filename, strripos($filename, '.')); // get file name
		$filesize = $file["size"];
		$allowed_file_types = array('.doc','.docx','.rtf','.pdf');	
	 
		if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
		{	
			// Rename file
			$newfilename = md5($file_basename) . $file_ext;
			if (file_exists("upload/" . $newfilename))
			{
				// file already exists error
				echo "You have already uploaded this file.";
			}
			else
			{		
				move_uploaded_file($file["tmp_name"], "upload/" . $newfilename);
				echo "File uploaded successfully.";		
			}
		}
		elseif (empty($file_basename))
		{	
			// file selection error
			echo "Please select a file to upload.";
		} 
		elseif ($filesize > 200000)
		{	
			// file size error
			echo "The file you are trying to upload is too large.";
		}
		else
		{
			// file type error
			echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
			unlink($file["tmp_name"]);
		}
	}
}

Open in new window

0
 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 250 total points
ID: 40450393
Yes you should make it like that. And to handle it with PHP, your code will be modified like this:
<?php
 
// Upload and Rename File
 
if (isset($_POST['submit']))
{
	$file_count = count($_FILES["file"]["name"]);
	for($i=0; $i < $file_count; $i++){
		$filename = $_FILES["file"]["name"][$i];
		$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
		$file_ext = substr($filename, strripos($filename, '.')); // get file name
		$filesize = $_FILES["file"]["size"][$i];
		$allowed_file_types = array('.doc','.docx','.rtf','.pdf');
		 
		if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
		{
			// Rename file
			$newfilename = md5($file_basename) . $file_ext;
			if (file_exists("upload/" . $newfilename))
			{
				// file already exists error
				echo "You have already uploaded this file.";
			}
			else
			{
				move_uploaded_file($_FILES["file"]["tmp_name"][$i], "upload/" . $newfilename);
				echo "File uploaded successfully.";
			}
		}
		elseif (empty($file_basename))
		{
			// file selection error
			echo "Please select a file to upload.";
		}
		elseif ($filesize > 200000)
		{
			// file size error
			echo "The file you are trying to upload is too large.";
		}
		else
		{
			// file type error
			echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
			unlink($_FILES["file"]["tmp_name"][$i]);
		}
	} // for end
}
 
?>

Open in new window

You can do it in different ways, but I did it this way to make minimum changes in the original copy.
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40450413
Thank you!
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40450438
You are welcome.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php construct 5 27
Insert won't work due to space in column name 5 21
tutorial for ebay api 3 34
send post values 7 26
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question