Solved

PHP Image Upload Loop

Posted on 2014-11-18
4
126 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now