Jump to content

How do i do multi uploads?.


ChrisMartino

Recommended Posts

Hey i would like to do this : http://hosting.mrkrabz.net/

 

I've got the base down you can upload single files, But how would i make it so when you click a button it adds a new field for a file to be uploaded and when you click upload it uploads the files in the box's and not just 1 of them all the ones you have put in the field's. ??

Link to comment
https://forums.phpfreaks.com/topic/179394-how-do-i-do-multi-uploads/
Share on other sites

you would want to make an html array, like

<input type="file" name="files[]" />
<input type="file" name="files[]" />

 

and in PHP, you would upload them in a loop, like so

for ($i = 0; $i < count($_FILES['files']['name']; $i++){
//i access each individual files stuff like so
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
//upload it now

 

for the button click, you will need javascript. You could surround the input file field with a div, and use javascript, and the innerHTML attribute of the div (gotten by document.getElementById()) to add another file field

Try this:

<head>
<title>FotoSnap.net</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="description" />
<meta name="keywords" content="keywords" /> 
<meta name="author" content="author" /> 
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>

<div id="container">
<div id="top">
	<div id="header">
	</div>
	<div id="menu">
		<a href="#">Upload</a><a href="#">*News</a><a href="#">About</a><a href="#">Contact</a><a href="#">Register</a>
	</div>
</div>
<div id="body">
<center>
	<div class="post">
		<h1>Upload a photo!.</h1>
		<p>
			<?php
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. 
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit'])) 
{
//reads the name of the file the user submitted for uploading
for ($i = 0; $i < count($_FILES['image']['name']; $i++){
$image=$_FILES['image']['name'][$i];
//if it is not empty
if ($image) 
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name'][$i]);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif") && ($extension != "bmp") && ($extension != "tif") && ($extension != "tiff"))	
{
//print error message
echo '<h1>Wrong file format!,  !</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name'][$i]);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'][$i], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{
if ($image) {
echo "<h1>Image Uploaded Successfully!</h1>";
echo "<textarea rows=\"2\" cols=\"40\">http://fotosnap.net/images/$image_name</textarea></form>";
}
else
{
echo "<h1>Error: You did not select a image to upload!</h1>";
}
}
}


?>

<!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" --> 
<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td><input type="file" name="image[]"></td></tr>
<tr><td><input type="file" name="image[]"></td></tr>
<tr><td><input type="file" name="image[]"></td></tr>
<tr><td><input type="file" name="image[]"></td></tr>
</table>
<br />
<br />
<table>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>	
</form>

		</p>
	</div>
</center>
	<div class="post">
		<h1>FotoSnap.net</h1>
		<p>Welcome to FotoSnap!, FotoSnap is a free any easy way to host photo's for everybody, With no limit on trafic!.</p>
		<p>FotoSnap is here to provide a easy way to upload and share photos across the web.</p>
	</div>
	<div class="post">
		<h1>What images do we support?</h1>
		<p>
			<ul>
				<li>.jpg</li>
				<li>.bmp</li>
				<li>.gif</li>
				<li>.tif</li>
				<p>And more soon!</p>
			</ul>
		</p>
	</div>
	<div class="post">
		<h1>Rules:</h1>
		<p>
			You cannot upload the following type of images:
			<ul>
				<li>Pornography</li>
				<li>Images that do not belong to you</li>
				<li>Images showing harm being inflicted on humans/animals.</li>
				<p>Breaking these rules will result in the image being deleted by a administrator!.</p>
			</ul>
		</p>
	</div>
</div>
<div id="footer">
	<p class="right"><a href="http://templates.lanmust.org">Custom design by</a> <a href="http://webdesign.lanmust.org">Mapers</a></p><p>Copyright © 2009 <a href="#">FotoSnap.net</a></p>
	<center><p><a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/uk/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/2.0/uk/80x15.png" /></a><br /><a xmlns:cc="http://creativecommons.org/ns#" href="FotoSnap.net" property="cc:attributionName" rel="cc:attributionURL"></a><a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/uk/"></a></p></center>
</div>
</div>

</body>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.