0

Ok guys this might seem like such a newbie issue but I've got looping issues that I just can't seem to work around. I'm simply trying to upload multiple images on my first project site.

When I posted this test php page up, it uploads all the files that I requested of it fine ; with all images that I wish to upload being uploaded at the directory intended.

<?php
$files = $_FILES['fileField'];
for ($x = 0; $x < count($files['name']); $x++)
{
        $name     = $files['name'][$x];
        $tmp_name = $files['tmp_name'][$x]; 
        move_uploaded_file($tmp_name, "property_images/$property_name/" . $name);
        header("location: property_list.php"); 
        exit();
}   
?>

However when I tried including my parser, though it goes into the correct directory, only the first file gets uploaded

<?php 
if(isset($_POST['property_name'])){
$property_name = mysql_real_escape_string($_POST['property_name']);
$district = mysql_real_escape_string($_POST['district']);
$address = mysql_real_escape_string($_POST['address']);
$property_type = mysql_real_escape_string($_POST['property_type']);

$sql = mysql_query("SELECT id FROM mydb WHERE property_name='$property_name' LIMIT 1");
$propertyMatch = mysql_num_rows($sql); 
if($propertyMatch > 0)
{
    echo 'Sorry, you tried to place a duplicate "Property Name" into the system, <a href="property_list.php">click here</a>';
exit();
}   

$sql = mysql_query("INSERT INTO mydb (property_name, district, address, property_type) VALUES ('$property_name','$ district','$address','$property_type')")or die (mysql_error());

if (!file_exists("property_images/$property_name"))
        {
        mkdir("property_images/$property_name");
        }
$files = $_FILES['fileField'];
for ($x = 0; $x < count($files['name']); $x++)
{
        $name     = $files['name'][$x];
        $tmp_name = $files['tmp_name'][$x]; 
        move_uploaded_file($tmp_name, "property_images/$property_name/" . $name);
        header("location: property_list.php"); 
        exit();
}   
}
?>

The count code works fine so I think its either these {} buggers or I need to get my eyes fixed. Any help would be uber appreciated.

Jocke
  • 1
  • 2
  • Does your form take into account an `array`? Such as `file[]` for instance? What does your form look like? – Funk Forty Niner Jul 25 '13 at 02:52
  • Yeah it does Fred – Jocke Jul 25 '13 at 02:54
  • Ok, well I think you would need a `foreach` loop, which I don't see in your posted code. – Funk Forty Niner Jul 25 '13 at 02:55
  • Have a look at this on PHP.net http://php.net/manual/en/function.move-uploaded-file.php for the `foreach` loop. You might find something useful in there, am sure. See example **#1** on the page. – Funk Forty Niner Jul 25 '13 at 02:57
  • Try something to the affect of `foreach ($_FILES as $file) { $uploadfile = $uploaddir . basename($file['name']);`. – Funk Forty Niner Jul 25 '13 at 03:01
  • Just tried it, same results Fred. It still uploads only the first image :( – Jocke Jul 25 '13 at 03:19
  • Ok. Well, only thing that I can start questioning is your `if(isset($_POST['property_name'])){`, more specifically `property_name`. What is that in your form, is it a hidden field, or is it your Submit button, other? Also your `DB`. Is it set to a certain limit? Oh, and I see you're wanting to created a folder with `mkdir` with every file uploaded, why? – Funk Forty Niner Jul 25 '13 at 03:37
  • There's no hidden field in my form as far as i know. It's a basic CMS form where property name is just that eg The Cascadia, Condominium X, etc. – Jocke Jul 25 '13 at 03:47
  • As for my submit button looks just like this – Jocke Jul 25 '13 at 03:48
  • Ok but what is the syntax for `property_name` in your form, the code itself. Is it taken from a `select`, an `input` a `dropdown`, `checkbox`, other? – Funk Forty Niner Jul 25 '13 at 03:49
  • I'm intending to progress it to an image gallery once I get this resolved with each property having around 6 images thus the mkdir with each file uploaded – Jocke Jul 25 '13 at 03:51
  • Its an input Fred. Whoah does it actually relate? – Jocke Jul 25 '13 at 03:52
  • I would suspect it does. If there's a space, strange character, could be a number of things. – Funk Forty Niner Jul 25 '13 at 03:54
  • So, any new developments? – Funk Forty Niner Jul 25 '13 at 16:48

1 Answers1

0

you need to add to input name [] brackets and attribute "multiple"

<form id = "upload_form" method="post" enctype="multipart/form-data"  >
    <input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>

Now all uploaded file will be available via

$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]
and so on

More info at http://www.php.net/manual/en/features.file-upload.multiple.php

hope this will sure help you.

liyakat
  • 11,825
  • 2
  • 40
  • 46