Solved

PHP Image Upload Loop

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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