General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 13th, 2003, 12:15 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
best method for multiple user files

I want to allow users to upload pictures to my site. I am using php and mysql. I have it set up well for one picture, but what about, say, six? So far it is done this way:
the user uploads the file, it is put into a folder, and the pictures name is inserted into the picture1 row, along with userid. This is used to retrieve the picture.
But with mutiple pictures, won't I have to check to see if(picture1 != '')?
and then check
if(picture2 != '') and so on and so forth. This seems like a lot of work. There must be a better way!
Oh and one other thing that may make it difficult is that there is a pending row, which means the pic is set pending ="yes" before an admin okays it and it is made "no". For this method it seems I would need maybe a seperate row for each pic, ie pic1pending, pic2pending? there is no way to differentiate between pending for pic1 and pic2 and so on.
Any help, gladly accepted!
__________________
hey it's the CHARKING

Reply With Quote
  #2  
Old December 13th, 2003, 12:18 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
There was an article posted right here on Devarticles:

Creating a Multi-File Upload Script in PHP
http://www.devarticles.com/c/a/PHP/..._Script_in_PHP/

This should cover everything you need...

Reply With Quote
  #3  
Old December 13th, 2003, 12:34 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
sorry I guess I didn't explain myself well enough. I have the file uploading part fine... I want to let users LATER add files, and i jsut wondered what the best database table would be. here this is what I have so far:
PHP Code:
 CREATE TABLE `pics` (
  `
picidint(14NOT NULL auto_increment,
  `
useridint(14NOT NULL default '0',
  `
currentvarchar(255NOT NULL default '',
  `
descriptionvarchar(255NOT NULL default '',
  `
pic1varchar(255NOT NULL default '',
  `
pic2varchar(255NOT NULL default '',
  `
pic3varchar(255NOT NULL default '',
  `
pic4varchar(255NOT NULL default '',
  `
pic5varchar(255NOT NULL default '',
  `
pendingvarchar(14NOT NULL default 'yes',
  
PRIMARY KEY  (`picid`)
TYPE=MyISAM

is this a good setup? How can users add pics to this table without the script having to check for the possibility that a filename has already been entered?

[edit]current is the current filename in the database[/edit]

Reply With Quote
  #4  
Old December 15th, 2003, 08:18 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
It'd probably be better to use something like the following:

Code:

CREATE TABLE pics(
    picid int(10) unsigned zerofill not null primary key auto_increment,
    userid int(14) not null default 0,
    current... #what is this?
    description...
    file varchar(30) not null default '',
    pending enum('yes','no') default 'yes'
    PRIMARY KEY (picid)
) TYPE=MyISAM;


Such a structure affords you maximum flexibility; if you later decide to allow 8 pictures, you don't have to make any table changes but would have only to modify your upload code so that it checks to determine whether or not the current user has already uploaded eight pictures (select count(*) from pics where userid=4) and allow or disallow the upload accordingly.

This also stands to make your code a little more concise. Rather than having to copy and paste code for pic1 through pic5 or build a function that runs X times, you can do something like the following (which also scales without changes if you decide to allow more pictures in the future):

PHP Code:
//Assume you've got a mysql result set in $rows.

for($i=0$i<sizeof($rows); $i++){
    if(
$rows[$i]["file"] != '' && $rows[$i]["pending"]=='no'){
        print 
"<img src=\"" $rows[$i]["file"] . "\">";
    }



Pretty much any time you find yourself adding numbered fields to a table like this, you should consider the benefits of splitting that data out into a separate table with a row per repeated piece of data rather than multiple columns for repeated data within rows.

Reply With Quote
  #5  
Old January 4th, 2004, 07:59 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
alright I decided to try this again, after I couldn't do it a while back. this is the form page I made; I think I udnerstand it all pretty well (I mean of what you wrote earlier).
PHP Code:
if($_POST)    { 
$filename $_FILES['imagefile']['name'];
echo 
$filename '<br />';

/*I commented out the code to actually use the pic since all I wanted to tackle first was the picname...*/
         
$picQuery "INSERT INTO pics2 (";  
$picQuery .= "`userid`, `FILE`, `pending`) VALUES (";  
$picQuery .= "'1', '$filename', 'no')"


if(
mysql_query($picQuery)or die('<br />pic didnt go...' mysql_error()))    { 
    echo 
'good it worked <br />
    <a href="index.php?page=picUpload&userid=1">continue</a>'

    } 
} else { 

echo 
'<p>
<form action="index.php?page=picUpload&userid=1" method="post" enctype="multipart/form-data"> 
<font class="asterix">*</font>please select a picture 
<br /> 
<input type="file" name="imagefile" class="select" /> 
<input type="submit" name="submit" class="submit" /> 
</form> 
</p>'


$rows mysql_result(mysql_query("SELECT * FROM pics2 WHERE userid = " $_GET['userid']),0,0) or die('could not get result because ' mysql_error()); 

//Assume you've got a mysql result set in $rows. 

for($i=0$i<sizeof($rows); $i++){  
    if(
$rows[$i]["file"] != '' && $rows[$i]["pending"] == 'no')    {  
        print 
"<img src=\"" $rows[$i]["file"] . "\"> image name = " $rows[$i]["file"];  
    }  
} } 


So this is inserting the picname fine into the db table pics2, under FILE, but is not giving me any results from it in the print of the for loop. I don't know much about for, but what I think it's saying is:

starts $i at 0 and then adds one to $i until it reaches the sizeof $rows;

checks to see that the file row of the number it is at ($i's) is not blank and pending is set to 'no';

then it prints out some thigns (i added a few things to see if it were going at all), including the row file where it matches current #.

this is giving me no results though; I at least expected a 'image name = ' but am getting nothing. am I doing something wrong? thanks for your help so far.

Reply With Quote
  #6  
Old January 6th, 2004, 05:27 PM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
am I understanding this correctly? Because I just can't figure this out. Anyone?

Reply With Quote
  #7  
Old January 7th, 2004, 08:39 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Your understanding of the for loop is correct. One thing that I notice is that your die statement in the mysql_result line uses single quotes but has an apostrophe in it. This should cause a syntax error and could be keeping your script from even getting to the loop. I would think the error would print to the screen, but that could depend on your log level and on whether die statements are even evaluated for syntax prior to running (I would assume they are).

Also, to upload files, you use POST. In your query, you're using the $_GET array to get the userid. Maybe that's preventing your query from returning any results. Try printing out your query and running it manually to see if you get any results. If not, you've found your problem. If so, we'll think on this some more.

Reply With Quote
  #8  
Old January 7th, 2004, 04:43 PM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
ok I don't now why, but I tried to escape the apostraphe (and had originally) but it didn't show on devarticles, so I simply wrote out the contraction.
As for the $_GET, I am typing &userid=1 in the url, it's not from the post. I just know that when I finish the script it will be based on searching for the userid, and figured I would just use it. I could easily jsut say where userid = 1. But, it's done before the psot, so it shouldn't matter. The script should be writing out the information right from the beginning...

i tried commenting out the if statement, and it gave me the blank image sign, and then 'image name = '
i tried using [$i]['userid'], instead of [$i]['FILE'], and still am getting nothing. So it seems to not be getting the info I need. I tried doing the wuery with and without count(*), because I thought maybe if it were doing count it doesn't make the array or something top choose some of the items from, but neither work. It jsut seems to not get any results! And I have two results where userid = 1 and pending ='no'.

Reply With Quote
  #9  
Old January 7th, 2004, 05:01 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Actually, take a closer look at what you're using to get your results. Mysql_result() gets one value from one cell, so you in fact don't even have an array to iterate over in the for loop. Try using mysql_fetch_array() or a similar function. In that case, you could also replace the for loop with something like:

PHP Code:
while($row=mysql_fetch_array(<params>)){
    
//do stuff with $row["file"] and $row["pending"]



You'll need to check the function reference for specifics, but that's the general idea.

Reply With Quote
  #10  
Old January 8th, 2004, 12:03 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
maaan Im sorry to make you tell me how to use mysql_fetch_array haha. Ok I got it now. I jsut am so unfamiliar with for, it's something I need more practice with. Ok so now... I think I'm running a pretty good picture table. I made the FILE row where the filename goes, and now current will just be an enum for yes or no, and I jsut check if the user has say six pictures or something, and then let them ad another if they would like. Alright thanks man I'll let you know if I have more problems, you have helped a lot.

Reply With Quote
  #11  
Old January 8th, 2004, 03:04 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
I'm back again! Alright I'm making the file upload page, and wanted to let the user upload up to six at a time. It checks to see how many they have and then displays the upload file boxes for each allowed. This works great. this is the code for that:
PHP Code:
 $rowCountof coursehas been set..
if(
$rowCount 6)    {
for(
$i=$rowCount$i<=6$i++)    {
input type="file" name="imagefile[]" class="select" /><br />';
        }
    } 

Notice the imagefile I have set as an array. I think this is the best way...?

But when I get to the actual processing of the files, I am using a foreach loop,

PHP Code:
foreach($_FILES['imagefile']['name'] as $filename)    {

$i++;

echo 
$_FILES['imagefile']['tmp_name'][$i];
if(
$filename != '')    {

copy ($_FILES['imagefile']['tmp_name'][$i],  
$_SERVER['DOCUMENT_ROOT'] . '/pics/profiles/' $_GET['userid'] . '/' $filename) or die('Could not copy,
<a href="backbalhblah">Go Back</a>'
); 


and then it jsut finished up the brackets and stuff. But see I realized it was not working with $_FILES['imagefile']['tmp_name'] because it creates an array I guess, but the foreach only goes through the name, not the tmp_name. So the tmp_name is still an array, right? So I thought, I will just treat it as an array (which I am really bad with anyway, so I may not be treating it correctly, I jsut tried to do what I saw in the manual). So every time the foreach happens, it adds one to $i, which is used as the array number after tmp_file. But it isn't echoing it, and it jsut says 'could not copy, go back'. And I know if I use a foreach again, it'll do ALL of the foreach before returning to the beginning foreach...
thanks for your time so far, this has been a great lesson in a few areas I'm struggling with.

Reply With Quote
  #12  
Old January 8th, 2004, 03:08 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
I JUST fixed it and thought I would leave the previous post anyway. I realized that arrays start at 0, so I moved the $i++ to the end of the foreach, and so it still ebgins the array at 0. Ok man! that was easy! Doesn't it always happen that way? you make the worlds longest post, and then you realize what it is you did wrong...
thanks though. This is so great

Reply With Quote
  #13  
Old January 8th, 2004, 08:21 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Well there you go. Glad you got it all resolved.

Reply With Quote