Yet ANOTHER image upload question
Moderator: General Moderators
Yet ANOTHER image upload question
As most of you should know by most of my posts, I deal extensively with images.. and uploading them.
My question is, say I have 10 file boxes for selecting images for upload.
When the user submits the form, I want do do a foreach();
if I do foreach($_FILES['filetoupload'] AS $pic) would $pic then contain all of the array values that $_FILES had? for example, ['tmp_name'], ['size'], etc?
My question is, say I have 10 file boxes for selecting images for upload.
When the user submits the form, I want do do a foreach();
if I do foreach($_FILES['filetoupload'] AS $pic) would $pic then contain all of the array values that $_FILES had? for example, ['tmp_name'], ['size'], etc?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
so erm, what's a good way to loop file uploads?
Edit: Nevermind, http://us2.php.net/features.file-upload has a great example.
Edit: Nevermind, http://us2.php.net/features.file-upload has a great example.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
PHP CODE:
HTML FORM:
This code uploads the first picture fine, and then does nothing with the rest of the pictures. What am I missing that is causing this not to loop?
Code: Select all
if($_POST['action'] == "uploadsubpictures")
{
foreach($_FILES['filetoupload']['error'] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$upload_dir = "uploads/$u/";
$tmp_name = $_FILES['filetoupload']['tmp_name'][$key];
$size = $_FILES['filetoupload']['size'][$key];
$size_bytes = 1048576;
if($size > $size_bytes)
{
echo "<p class=\"main\">File Too Large. Please try again.</p>"; exit();
}
$filename2 = mysql_real_escape_string(strip_tags($_FILES['filetoupload']['name'][$key]));
$extension = strtolower(strrchr($filename2,"."));
$extensions = array('.jpg','.jpeg','.png');
if(!in_array($extension, $extensions))
{
echo "<p class=\"main\"><font color=red>File type not allowed, only JPG or PNG files are allowed.</font></p>"; require 'footer.php'; die();
}
$time = time();
$filename = $time.$extension;
$filesize = getimagesize($tmp_name);
include 'createthumb.inc.php';
if(($filesize['0'] > 450) || ($filesize['1'] > 450))
{
move_uploaded_file($tmp_name,$upload_dir.$filename);
chmod("uploads/$u/$filename", 0777);
createthumb("uploads/$u/$filename","uploads/$u/$filename",450,450);
} ELSE
{
move_uploaded_file($tmp_name,$upload_dir.$filename);
}
createthumb("uploads/$u/$filename","thumbs/$u/$filename",100,100);
$size2 = filesize("uploads/$u/$filename");
mysql_query("INSERT INTO subpics (username, img, size) VALUES('$u','$filename','$size2')") or die(mysql_error());
}
}
}Code: Select all
<form action="editprofile.php?u=<? echo $u; ?>" method="post" enctype="multipart/form-data">
Sub Pics<BR>
<input type="hidden" name="action" value="uploadsubpictures">
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="file" name="filetoupload[]" class="form"><BR>
<input type="Submit" value="Upload">
</form>Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
anyone?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Very helpful 

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
For me, using a three field form, for uploading one test file, this: foreach($_FILES['file'] AS $pic){ var_dump($pic);} returned this:
Instead of all that nonsense maybe you should just access the files array directly. It would return this:
Code: Select all
array(3) {
[0]=>
string(8) "news.php"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
array(3) {
[0]=>
string(24) "application/octet-stream"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
array(3) {
[0]=>
string(26) "C:\WINDOWS\TEMP\php801.tmp"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
array(3) {
[0]=>
int(0)
[1]=>
int(4)
[2]=>
int(4)
}
array(3) {
[0]=>
int(317)
[1]=>
int(0)
[2]=>
int(0)
}Code: Select all
array(5) {
["name"]=>
array(3) {
[0]=>
string(9) "forum.php"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["type"]=>
array(3) {
[0]=>
string(24) "application/octet-stream"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["tmp_name"]=>
array(3) {
[0]=>
string(26) "C:\WINDOWS\TEMP\php80A.tmp"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["error"]=>
array(3) {
[0]=>
int(0)
[1]=>
int(4)
[2]=>
int(4)
}
["size"]=>
array(3) {
[0]=>
int(319)
[1]=>
int(0)
[2]=>
int(0)
}
}Personally I think what you are looking for is something like the following which converts the FILES array into a more manageable form. This:
produces an array like this:
I hope that is of some help to you!
Code: Select all
for($i = 0, $count = count($_FILES['filetoupload']['name']); $i < $count; $i++){
foreach($_FILES['filetoupload'] as $key => $value){
if(!empty($_FILES['file']['name'][$i])){
$pic[$i][$key] = str_replace('\\', '/', $value[$i]);
}
}
}Code: Select all
Array
(
[0] => Array
(
[name] => template.php
[type] => application/octet-stream
[tmp_name] => C:/WINDOWS/TEMP/php8A7.tmp
[error] => 0
[size] => 5956
)
[1] => Array
(
[name] => forum.php
[type] => application/octet-stream
[tmp_name] => C:/WINDOWS/TEMP/php8A8.tmp
[error] => 0
[size] => 319
)
[2] => Array
(
[name] => index.php
[type] => application/octet-stream
[tmp_name] => C:/WINDOWS/TEMP/php8A9.tmp
[error] => 0
[size] => 318
)
)