|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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... |
|
#3
|
|||
|
|||
|
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:
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] |
|
#4
|
||||
|
||||
|
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:
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. |
|
#5
|
|||
|
|||
|
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:
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. |
|
#6
|
|||
|
|||
|
am I understanding this correctly? Because I just can't figure this out. Anyone?
|
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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'. |
|
#9
|
||||
|
||||
|
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:
You'll need to check the function reference for specifics, but that's the general idea. |
|
#10
|
|||
|
|||
|
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.
|
|
#11
|
|||
|
|||
|
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:
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:
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. |
|
#12
|
|||
|
|||
|
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 ![]() |
|
#13
|
||||
|
||||
|
Well there you go. Glad you got it all resolved.
|