Solved

PHP Image Upload Loop

Posted on 2014-11-18
4
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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.

733 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