Solved

PHP Image Upload Loop

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

11 Experts available now in Live!

Get 1:1 Help Now