Page 1 of 1

Array syntax question

Posted: Tue May 20, 2008 9:08 am
by TheBrandon
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello everyone,

I am fairly new to PHP (read a few tutorials, watched some videos on Lynda.com) and I am writing an admin script for a website trying to learn. Basically the client wants a form so he can update various news-type things, and also upload 4 pictures per article.

The 4 pictures is where I am getting stuck. I found a script to upload 1 image, but I'm not sure how to change the syntax to accept 4 forms.

Here is an example of what I am working with:

Code: Select all

<label for="file">Image 1:</label>
<input type="file" name="file" id="file" /> 
<label for="file2">Image 2:</label>
<input type="file" name="file2" id="file2" /> 
<label for="file3">Image 3:</label>
<input type="file" name="file3" id="file3" /> 
<label for="file4">Image 4:</label>
<input type="file" name="file4" id="file4" />

Code: Select all

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 300000))
There's obviously much more code than that, but it's all the same really. If someone could shed some light on how to change the "$_FILES["file"]" part to accept file, file2, file3 and file4; I would greatly appreciate it.

I've done some googling and everything and I've read that $_FILES is a global array built into PHP, but I'm still a bit confused about how to add more than one key(?) to it in the context that I need to.

Thanks in advance! This is the last little hurdle of what I've done so far on this project and so far I'm really having fun with PHP. I've used it before (for includes mainly) but actually scripting stuff out can be really fun and I think once I get the hang of it, I won't be able to pull myself away from it.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Array syntax question

Posted: Tue May 20, 2008 1:09 pm
by califdon
Welcome to the PHP party! There are several good short tutorials on uploading multiple files, using PHP. Here's one: http://www.plus2net.com/php_tutorial/ph ... upload.php

Re: Array syntax question

Posted: Tue May 20, 2008 1:28 pm
by TheBrandon
califdon wrote:Welcome to the PHP party! There are several good short tutorials on uploading multiple files, using PHP. Here's one: http://www.plus2net.com/php_tutorial/ph ... upload.php
Thanks!

And thanks for the link; although it looks much more complex than what I already have working.

Really, it's already functioning. I have it uploading the image, putting the image name, filesize, IP of the uploader, etc. into an SQL database and pulling it on another page. My only real hurdle left is this one spot (that I anticipate). Just the move from 1 file to 4.

I'll definitely bookmark it though in case I can't get the one I have working. I really look at re-doing the whole thing at this point as a last resort though.

It's a news section for a client, so it has news, the date, etc. and all of that is already done; already automatically formatted for the user, etc. Like when he logs in, it automatically detects the date for him so he doesn't have to enter it, small things like that.

If I had to integrate this new script into it, I would have pretty much redo all of that other stuff to work with it and might even run into a similar hurdle.

Thank you though. If its not finished by Friday, I'll more than likely be using the link you posted as the final script.

Re: Array syntax question

Posted: Tue May 20, 2008 4:48 pm
by TheBrandon
After doing some research on my own, I'm thinking if I could define a variable beforehand and then insert into the array, that would work pretty well.

Something like:

Code: Select all

 
   1. if ((($_FILES[($file)]["type"] == "image/gif")
   2. || ($_FILES[($file)]["type"] == "image/jpeg")
   3. || ($_FILES[($file)]["type"] == "image/pjpeg"))
   4. && ($_FILES[($file)]["size"] < 300000))
 
But I'm still a little lost as to the syntax of that and how to form the variable so it will output correctly. So if anyone has any advice, I would greatly appreciate it. I've been doing some reading online but I still can't find anything that addresses this specific type of code that I recognize; but then again, I am new to PHP.

Sorry for the first post, pickle. I was just in a hurry to get help. I did read the boards rules, particularly this line:
On our discussion boards, the focus is more on helping people to learn rather than simply handing out working scripts. Someone who is making a real effort to tackle a problem is much more likely to receive help than a "please can anyone give me a file upload script" type of post.
I will try not to post using the code brackets, but to post using the PHP code brackets from now on...

Re: Array syntax question

Posted: Wed May 21, 2008 5:35 pm
by TheBrandon
Third page already?

No one can offer any kind of advice on how to make this work without using a completely different script?

Re: Array syntax question

Posted: Wed May 21, 2008 9:24 pm
by nowaydown1
Just loop over the $_FILES superglobal. It's an array:

Code: Select all

 
<?php
 
if(is_array($_FILES)) {
    foreach($_FILES as $fileKey => $fileDetails) {
        if ((($_FILES[$fileKey]["type"] == "image/gif") || ($_FILES[$fileKey]["type"] == "image/jpeg") || ($_FILES[$fileKey]["type"] == "image/pjpeg")) && ($_FILES[$fileKey]["size"] < 300000)) {
            // Do stuff, it's valid--
        }
    }
}
 
?>
 

Re: Array syntax question

Posted: Thu May 22, 2008 2:56 pm
by TheBrandon
Thanks a ton. I will give that a shot as soon as possible and post how it works out.