I have a photo uploader for a client. It's a simple input file button.
But they wanted to be able to add more than one picture.
So, when the client clicks the "add another picture?" button, I used javascript to clone the initial input button, and then used a counter to increase a digit in the name. I.E:
First input buttons name => name="uploaded1"
When Client Wants Another pick => name="uploaded2"
...Another One => name="uploaded3"
(and so on)
now on my php side, im using this to move the file into a folder on the server and save the path in a MySQL table.
[PHP]
$target = "wherepixgo/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$pictureurl = $target;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
if($target=="wherepixgo/"){
$target="";
echo '<center><br /><br /><br />';
echo "No file Uploaded.";
echo '</center>';
echo '<br><br><br>';
}
else {
echo '<center>';
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
echo '<br><br><br></center>';
}
}[/PHP]
That's great for when I only had one picture to upload, and it would just look for one input file button name "uploaded". Now there are going to be input files to the Nth degree. How can I tell php to look at all of the posted variables named "uploaded(#)" and loop through an inputting code til no more.
Psuedocode:
[PHP]
while(there are posted variables with the name "uploaded#"){
find it's # at the end of its name;
upload the current photo;
copy the path name into a variable so i can save it in MySQL;
}
[/PHP]
now i figure the only tricky thing about this would be searching through the post data and selecting everytime it sees the world "uploaded".
A little help, ps...if i am missing a very obvious and common feature of php, please tell me as im still learning (for life).
thanks in advance.
6 2014
I believe, instead of changing the name by incrementing a counter each time, you could simply make the name `uploaded[]` and it will be an array. You would not need to change the name at all and instead, could simply pull the entire array.
So, for example <input type="file" name="images[]" /> and $_FILES[images] would be an array of multiple files.
I'm pretty certain it works that way.
Atli 5,058
Expert 4TB
Hi.
What Steven suggested should work. It does work on normal POST elements so I see no reason why it shouldn't work here.
In case it doesn't, however, you could loop through the $_FILES array with a foreach loop: -
foreach($_FILES as $file) {
-
if($file['error'] == 0) {
-
# ...
-
}
-
}
-
You may want to add some extra validation tho, in case there are files you don't want mixed in there.
ok, so i looked into what you two were both saying, but i cant seem to get it to work. I understand that if i simply call the name of the file input "uploaded[]", it will enter the file names into and array.
so this is what is printing the form for me: [PHP]
$max_no_img=5;
echo "<form method=post action=postnews.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='uploaded[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Submit'></td></tr>";
echo "</form> </table>";
[/PHP]
so that will make me 5 input files.
Now my posting side wont work. And I want to be able to understand why it isn't.
[PHP]
{
foreach($_FILES['uploaded']['name'] as $value)
{
if(!empty($value){
$target = "uploadedpictures/";
$target = $target . $value ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
echo '<br>';
}
}
}
}[/PHP]
It doesn't work, i get this error
Notice: Array to string conversion in postnews.php on line 13
The only array to string i can see is maybe using foreach to turn an array element into "$value"? Please help, this has been like a 2 day struggle for me and the client.
Thanks
Atli 5,058
Expert 4TB
Hi.
When you POST it like that, the $_FILES['uploaded'] is an array of files. It looks more like this: -
$_FILES['uploaded'] = array(
-
array(
-
"name" => "name1",
-
"tmp_name" => "tmp_path1",
-
// etc...
-
)
-
array(
-
"name" => "name2",
-
"tmp_name" => "tmp_path2",
-
// etc...
-
)
-
array(
-
"name" => "name3",
-
"tmp_name" => "tmp_path3",
-
// etc...
-
)
-
)
-
You need to do something more like this: -
foreach($_FILES['uploaded'] as $_v)
-
{
-
$fileName = $_v['name'];
-
$tmpName = $_v['tmp_name'];
-
# etc...
-
}
-
Just a final tip... When working with arrays, print_r and <pre> are your friends. -
echo "<pre>".print_r($array, 1)."</pre>";
-
thanks to the both of you, this makes alot of sense now.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brad H McCollum |
last post by:
I'm writing an application using VB 6.0 as the front-end GUI, and the
MSDE version of SQL Server as the back-end (it's a program for a
really small # of users --- less then 3-4).
I'm trying to...
|
by: DraguVaso |
last post by:
Hi,
I want my application do different actions depending on the exception it
gets.
For exemple: I have an SQL-table with a unique index. In case I try to
Insert a record that's alreaddy in it I...
|
by: Mike Leahy |
last post by:
Okay.I'm following the documentation that came with the PostgreSQL source
code (located in /usr/doc/postgresql-7.3.4-2/html/arrays.hmtl in my cygwin
root).
I created have a table with a varchar...
|
by: BlackFireNova |
last post by:
I need to write a report in which one part shows a count of how many
total records fall within the working days (Monday - Friday) inside of a
(prompted) given date range, in a particular...
|
by: Dennis |
last post by:
If a Window's form contains two textboxes, txtUserName for names and the
other txtDimension for mathematical values, how could the program code
be set up to identify that only letter character(s)...
|
by: Woody Splawn |
last post by:
I know that I can do the following to identify the parent name of an active
control
Dim sParentName As String = ActiveControl.Parent.Name
But how do I identify the name of the active control?...
|
by: Eric |
last post by:
Hi,
I need to find a way to identify between a few different file formats
WITHOUT looking at the file extension. Very often our customers will name
file incorrectly. For example, they'll send us...
|
by: weaselboy1976 |
last post by:
Hello,
If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question...
|
by: rm |
last post by:
I have seen several examples explaining the fact that a table
containing a field for each day of the week is for the most part an
array. An specific example is where data representing worked hours...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |