473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I have PHP identify $POST values.

31 New Member
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_uploade d_file($_FILES['uploaded']['tmp_name'], $target))
{
if($target=="wh erepixgo/"){
$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.
Oct 19 '07 #1
6 2055
steven
143 New Member
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.
Oct 20 '07 #2
Atli
5,058 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. foreach($_FILES as $file) {
  2.   if($file['error'] == 0) {
  3.     # ...
  4.   }
  5. }
  6.  
You may want to add some extra validation tho, in case there are files you don't want mixed in there.
Oct 20 '07 #3
recordlovelife
31 New Member
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='multip art/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>Im ages $i</td><td>
<input type=file name='uploaded[]' class='bginput' ></td></tr>";
}

echo "<tr><td colspan=2 align=center><i nput 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($valu e){
$target = "uploadedpictur es/";
$target = $target . $value ;
if(move_uploade d_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
Oct 22 '07 #4
Atli
5,058 Recognized Expert Expert
Hi.

When you POST it like that, the $_FILES['uploaded'] is an array of files. It looks more like this:
Expand|Select|Wrap|Line Numbers
  1. $_FILES['uploaded'] = array(
  2.   array(
  3.     "name" => "name1",
  4.      "tmp_name" => "tmp_path1",
  5.      // etc...
  6.   )
  7.   array(
  8.     "name" => "name2",
  9.      "tmp_name" => "tmp_path2",
  10.      // etc...
  11.   )
  12.   array(
  13.     "name" => "name3",
  14.      "tmp_name" => "tmp_path3",
  15.      // etc...
  16.   )
  17. )
  18.  

You need to do something more like this:
Expand|Select|Wrap|Line Numbers
  1. foreach($_FILES['uploaded'] as $_v) 
  2. {
  3.   $fileName = $_v['name'];
  4.   $tmpName = $_v['tmp_name'];
  5.   # etc...
  6. }
  7.  
Oct 22 '07 #5
steven
143 New Member
Just a final tip... When working with arrays, print_r and <pre> are your friends.

Expand|Select|Wrap|Line Numbers
  1. echo "<pre>".print_r($array, 1)."</pre>";
  2.  
Oct 23 '07 #6
recordlovelife
31 New Member
thanks to the both of you, this makes alot of sense now.
Oct 23 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3070
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 determine, through the Visual Basic interface, the permissions of each user that's using the application on his/her machine. For example, let's say I'm user "Michael" that's sitting down at my machine using the app. I've written. The security...
8
487
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 get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index 'UniqueValues'." What I'm looking for is a way to identify the exception: in case I get this
0
1554
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 array in it, kind of like the following: CREATE TABLE tbl_db_usuario_detalles (NOMBRE varchar(50), COD_USE varchar(6));
5
14897
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 geographical region. I have written a query which prompts the user for the start and end dates. It also filters for entries which pertain to the particular geographical region. I'm not sure where to go from here.
1
1791
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) are entered into txtUserName .... and that only numbers and decimals are entered into txtDimension ? Secondly, how could the program code be set up to inform the user that the wrong type of data was entered ? All constructive suggestions are...
6
17346
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? Str = activeControl.Name does not seem to work.
4
1947
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 a file that's named 'filename.xls', but it's actually a tab delimited or comma delimited file. The possible formats that I need to identify are: HTML, tab delimited, comma delimited or Excel.
12
2649
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 is: in the stringManipulator function, is there any way to identify if the char* is pointing to writable memory space to to a string literal? Sure, the example is trivial, but we run into such problems when the function call stack gets 20...
1
1494
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 is stored in a table. CREATE TABLE ( NOT NULL , NOT NULL, NULL , NULL ,
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.