| | |
unlimited file upload fields
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
I wand to give the user the ability to upload as many pictures as he wants to. at present I hava a fexed amount of browse fields wich the user can use to select pictures... the code looks something like this :
On the other page I have code that builds a unick name for each uploaded picture, then makes a directory and copies the selected pictures in that directory.
This part works properly.
Then I have a few lines of code that build the link to each of those files in part and uploads it in to a DB... the website then accesses those pics via the links wich it takes from the DB.
Now on the DB, the images have their own table... it has two main columns... "largepic" and "smallpic". smmallpic is the thumbnail and largepic is the image that is displayed when users click on the thumbnail to view the full pic.
Now I need to figure out a way in wich to submit these pictures in groups of two. and process them in solid groups of two too.
SO I was thinking maybe on the upload page only two browse buttons would be shown... when the user fills bolth of them, another two apear beneath them (I'm guessing something like this can be done with ajax scripts somehow)... maybe with building the name of each field as a string first with a numeric value as part of the name that one can increment each time a new set of fields is needed.
But how do I go about the saving of the parh to the DB? the solid groups of two links need to be identifyable somehow so that you can target them when putting the links in the db since the first field needs to point to the first column and the second needs to point to the second.
If it was a set number of fields it would be easy to just write the upload proecess for each field... but for a limitless amount of fields ( namelly a new set of fields pops up as soon as the last set is filled... and, sheesh... lots of complications, if the user delets the pics in a field in the middlle of two other sets only that set of fields has to disapear and the others realocated... I am lost... )
How do you supose I could go about this unlimited image upload procedure?
PHP Syntax (Toggle Plain Text)
<?PHP $max_file_size = 3000000; ECHO "<FORM NAME=name ID=name ACTION=submit.php enctype='multipart/form-data' METHOD=post >"; ... code ... ECHO "<input type='hidden' name='MAX_FILE_SIZE' value = $max_file_size > "; ECHO "<input id='file1' type='file' name='file[]'><BR>"; ECHO "<input id='file2' type='file' name='file[]'><BR>"; ECHO "<input id='file3' type='file' name='file[]'><BR>"; ... code... ECHO "<BR><INPUT TYPE = submit NAME = submit VALUE = 'blah '>"; ECHO "</FORM>"; ?>
On the other page I have code that builds a unick name for each uploaded picture, then makes a directory and copies the selected pictures in that directory.
This part works properly.
Then I have a few lines of code that build the link to each of those files in part and uploads it in to a DB... the website then accesses those pics via the links wich it takes from the DB.
Now on the DB, the images have their own table... it has two main columns... "largepic" and "smallpic". smmallpic is the thumbnail and largepic is the image that is displayed when users click on the thumbnail to view the full pic.
Now I need to figure out a way in wich to submit these pictures in groups of two. and process them in solid groups of two too.
SO I was thinking maybe on the upload page only two browse buttons would be shown... when the user fills bolth of them, another two apear beneath them (I'm guessing something like this can be done with ajax scripts somehow)... maybe with building the name of each field as a string first with a numeric value as part of the name that one can increment each time a new set of fields is needed.
But how do I go about the saving of the parh to the DB? the solid groups of two links need to be identifyable somehow so that you can target them when putting the links in the db since the first field needs to point to the first column and the second needs to point to the second.
If it was a set number of fields it would be easy to just write the upload proecess for each field... but for a limitless amount of fields ( namelly a new set of fields pops up as soon as the last set is filled... and, sheesh... lots of complications, if the user delets the pics in a field in the middlle of two other sets only that set of fields has to disapear and the others realocated... I am lost... )
How do you supose I could go about this unlimited image upload procedure?
you set up a numbering structure so that the thumbnail and full picture share a common filename,
the database only needs one picture name
the thumbnail is thumb100004.jpg
and the fullsize picture is 100004.jpg
<img src='thumb<php echo'/*sql to get $number.jpg*/'; ?>' onclick='bigimage.src=<?php echo $number.jpg ?>'>
down the bottom of the page is
<img id='bigimage' src='blank.jpg'>
waiting to be filled with the contents of the clicked thumbnail
the thumbnail can be generated on the fly by php so only one file need be stored
dirty unchecked code, thought process only
the database only needs one picture name
the thumbnail is thumb100004.jpg
and the fullsize picture is 100004.jpg
the only thing needed is 100004.jpgthe thumbnail is
<img src='thumb<php echo'/*sql to get $number.jpg*/'; ?>' onclick='bigimage.src=<?php echo $number.jpg ?>'>
down the bottom of the page is
<img id='bigimage' src='blank.jpg'>
waiting to be filled with the contents of the clicked thumbnail
the thumbnail can be generated on the fly by php so only one file need be stored
dirty unchecked code, thought process only
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
For starters, if you name every field just like you have been, on the php side you will be able to access each individual file as a numerical array.
Now for adding and removing the file fields dynamically i'll refer you to my favorite prototype tutorial, which i have posted before: http://www.phpriot.com/articles Read the 8 Weeks of Prototype tutorials in order, even just look them over and at the examples and it should be easy to figure that out.
As for naming conventions, use a timestamp from time() or mktime() (although i believe mktime() throws an E_STRICT violation now). and add something unique, maybe the username to it etc.
Generally how i handle thumbnails is such:
First I create two directories, one that holds the large version and one that holds the thumbnails. 800 and 150 or full and thumb or anything that works with your conventions. I would caution against generating the thumbnails on the fly. Image processing can be very resource intensive and if you're displaying a lot of thumbnails on one page that will not scale well. disk space however is cheap and a lot easier to expand as necessary then more memory/processing power.
Then when i process the images i resize the uploaded file to the 800 and then resize the 800 to the 150, and save them both as the SAME filename but in different folders. This is where you will see the performance gain. You do two resizing operations and that is it for ever, unless of course you decide to change your thumbnail sizes.
In your case i'd say your best bet would be to create an "images" table in mysql and then use a many to many relationship to join a user to any number of images. In the images table simply storing some information about a particular image. filename, size? type? the date is was created?
//images
pk_imageid
filename
date_created
//users_images_join
fk_userid
fk_imageid
Or something like that.
When you pull all your images out, if you're displaying a thumbnail you use domain.com/images/150/filename.jpg and when you are displaying the full size domain.com/images/800/filename.jpg
Using a timestamp and a unique identifier, like a username (
1232375747_username.jpg) or userid (
1232375747_137.jpg) ensures that there is no way for two users to generate the same image, even if both of them would submit it at exactly the same time.
Now for adding and removing the file fields dynamically i'll refer you to my favorite prototype tutorial, which i have posted before: http://www.phpriot.com/articles Read the 8 Weeks of Prototype tutorials in order, even just look them over and at the examples and it should be easy to figure that out.
As for naming conventions, use a timestamp from time() or mktime() (although i believe mktime() throws an E_STRICT violation now). and add something unique, maybe the username to it etc.
Generally how i handle thumbnails is such:
First I create two directories, one that holds the large version and one that holds the thumbnails. 800 and 150 or full and thumb or anything that works with your conventions. I would caution against generating the thumbnails on the fly. Image processing can be very resource intensive and if you're displaying a lot of thumbnails on one page that will not scale well. disk space however is cheap and a lot easier to expand as necessary then more memory/processing power.
Then when i process the images i resize the uploaded file to the 800 and then resize the 800 to the 150, and save them both as the SAME filename but in different folders. This is where you will see the performance gain. You do two resizing operations and that is it for ever, unless of course you decide to change your thumbnail sizes.
In your case i'd say your best bet would be to create an "images" table in mysql and then use a many to many relationship to join a user to any number of images. In the images table simply storing some information about a particular image. filename, size? type? the date is was created?
//images
pk_imageid
filename
date_created
//users_images_join
fk_userid
fk_imageid
Or something like that.
When you pull all your images out, if you're displaying a thumbnail you use domain.com/images/150/filename.jpg and when you are displaying the full size domain.com/images/800/filename.jpg
Using a timestamp and a unique identifier, like a username (
1232375747_username.jpg) or userid (
1232375747_137.jpg) ensures that there is no way for two users to generate the same image, even if both of them would submit it at exactly the same time.
•
•
Join Date: Jan 2009
Posts: 34
Reputation:
Solved Threads: 3
Here is a little something I use, It's rough and could be greatly improved but it works. One problem I notice is that the new div is created inside the previous so it keeps getting bigger and pushes everything below it down. That div is needed because or else the previous information in the input will be delete. But here it is work on it... let me know how it goes.
PHP Syntax (Toggle Plain Text)
<script> //n = name (upload) //t = this input //c = counter function createNew(n,t,c){ //counter NEXT c2 = c+1; //Start: Make input newer = "<input name='"+n+"_"+c2+"' type='file' onclick='javascript:createNew(\""+n+"\",this,"+c2+");' />"; //End: New div for next newer += "<div id='Extras"+n+c2+"'> </div>"; //Outputs whats created document.getElementById('Extras'+n+c).innerHTML += newer+"<br />"; //Updates hidden counter document.getElementById(n+"count").value = c; //remove onclick t.onclick = ""; } </script> <form action="" method="post" /> &n<input name='upload_1' type='file' onclick='javascript:createNew("upload",this,1);' > <div id="Extrasupload1"> </div> <input type='hidden' id='uploadcount' name='uploadcount' value='0' /> <input type="submit" value="Submit" /> </form>
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
html Syntax (Toggle Plain Text)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="jquery-1.3.min.js"></script> <script type="text/javascript"> //Make sure the document is loaded $(document).ready(function(){ //Watch clicks on the add button $(":input[name='add']").click(function () { $("div[id='file']:last").clone(true).insertAfter("div[id='file']:last"); }); //Watch clicks on the remove button $(":input[name='sub']").click(function () { $("div[id='file']:last").remove(); }); }); </script> </head> <body> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <div id="file"><input type="file" name="file[]" id="file"/></div> <div id="file"><input type="file" name="file[]" id="file"/></div> </form> <div><input name="add" type="button" value="+ 1" /><input name="sub" type="button" value="- 1" /></div> </body> </html>
I know i posted a link to a prototype tutorial earlier, but this is a jQuery example, that is very basic but achieves the same thing posted above, and adds the ability to remove the last field too.
It could use some error checking, like checking if the only file field has been removed etc. Or creating a new file field if all of them have been deleted.
Just some food for thought but this is really javascript related not php.
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
thanks for the suggestion guys... I managed to skip a lot of headakes simply by doing the entire picture upload on a separate page. and I've got the upload working, the saving of files, placing in directory, naming, saving paths to DB, and accessing them from the website.
But now one more problem remains. for the admin there needs to be a checkbox next to each image detailing weather the image will display on the website or not. basicly there's a field in the DB that is either 1 or 0 and when the images are displayed on the website they check weather that field is 1 or 0... if it's 0 the pic is not displayed whille if the value is 1 the image is displayed.
I incorporated that checkbox in the file upload area so the user can choose weather to display the image on the website or not. but naturally he needs to have an option to edit that on the same page for each individual image.
so on the same page as the image upload, I made a for loop that will display all images that have a path in the DB.
and for each of these pics I inclouded a checkbox that is either checked or unchecked based on what the vizible value is in the DB for each coresponding pic :
Now what I want to do is to implement a way in wich if the user checks or unchecks that checkbox, a function would be executed that will change the vizible field in the DB to 1 or 0 acording to weather the the checkbox is checked or not... and the results must be automaticly reloaded on the page... to that end I was thinking of using ajax scripts so that I wouldn't have to reload the entire page ( actually reloading the entire page isn't even an option )... so I was thinking of doing something like this (the $VALUE thing is necesary to identify wichset of pics the edited pic is from so that value must be transmitted through the ajax scripts):
the check.js contains:
and the vizible.php contains:
But now one more problem remains. for the admin there needs to be a checkbox next to each image detailing weather the image will display on the website or not. basicly there's a field in the DB that is either 1 or 0 and when the images are displayed on the website they check weather that field is 1 or 0... if it's 0 the pic is not displayed whille if the value is 1 the image is displayed.
I incorporated that checkbox in the file upload area so the user can choose weather to display the image on the website or not. but naturally he needs to have an option to edit that on the same page for each individual image.
so on the same page as the image upload, I made a for loop that will display all images that have a path in the DB.
PHP Syntax (Toggle Plain Text)
INCLUDE('conect.php'); $sql_pics = "SELECT pics.smallpic, pics.largepic, pics.vizible, pics.ID FROM pics WHERE (pics.ID = '$VALUEl' )"; $rez_pics = mysql_query($sql_pics) OR DIE (mysql_error()); $lines_pics = mysql_num_rows($rez_pics); FOR ($i=0; $i<$lines_pics; $i++) { $smallpic = mysql_result ($rez_pics, $i, 'smallpic'); $largepic = mysql_result ($rez_pics, $i, 'largepic'); ECHO "<A HREF = '$largepic' target='_blank'> <IMG SRC = '$smallpic' BORDER='0'></IMG> </A><BR>"; }
and for each of these pics I inclouded a checkbox that is either checked or unchecked based on what the vizible value is in the DB for each coresponding pic :
PHP Syntax (Toggle Plain Text)
$vizible = mysql_result ($rez_pics, $i, 'vizible'); IF ($vizible == 1) { ECHO "<INPUT TYPE='checkbox' NAME=viz ID=viz CHECKED='checked' > Vizible"; } ELSE { ECHO "<INPUT TYPE='checkbox' NAME=viz ID=viz> Vizible"; }
Now what I want to do is to implement a way in wich if the user checks or unchecks that checkbox, a function would be executed that will change the vizible field in the DB to 1 or 0 acording to weather the the checkbox is checked or not... and the results must be automaticly reloaded on the page... to that end I was thinking of using ajax scripts so that I wouldn't have to reload the entire page ( actually reloading the entire page isn't even an option )... so I was thinking of doing something like this (the $VALUE thing is necesary to identify wichset of pics the edited pic is from so that value must be transmitted through the ajax scripts):
PHP Syntax (Toggle Plain Text)
<HTML> <HEAD> <script src="check.js"></script> </HEAD> <BODY> <?PHP ECHO "<span id='vize'>"; ... entire code with displaying the images listed here ... IF ($vizible == 1) { ECHO "<INPUT TYPE='checkbox' NAME=viz ID=viz CHECKED='checked' onMouseUp=\"vizib(this.value,'$IVALUE')\"> Vizible"; } ELSE { ECHO "<INPUT TYPE='checkbox' NAME=viz ID=viz onMouseUp=\"vizib(this.value,'$VALUE')\"> Vizible"; } ECHO "</span>"; ?> </BODY> </HTML>
the check.js contains:
PHP Syntax (Toggle Plain Text)
var xmlHttp //------------------------------- function vizib(str1,str2) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="vizible.php" url=url+"?q="+str1+"&p="+str2 url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChangedviz xmlHttp.open("GET",url,true) xmlHttp.send(null) function stateChangedviz() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("vize").innerHTML=xmlHttp.responseText } } //---------------------------------- function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
and the vizible.php contains:
PHP Syntax (Toggle Plain Text)
<HTML> <HEAD> <script src="check.js"></script> </HEAD> <BODY> <?PHP INCLUDE('conect.php'); $q=$_GET["q"]; $p=$_GET["p"]; ... code for updating the DB field vizible from the set marked by $p wich is $VALUE from the first file and put either 1 or 0 in it weather the checkbox si either checked or not ( reflected in the $q variable wich should hoald the checkbox's state. ) ... code with displaying the images listed here allong with checkboxes and function links ... ?> however... I get a number of problems with this aproach... first of all the escaped \" symbols seem to be interfearing with the function somehow as it won't execute... also... this.value for a checkbox always seems to return "on" reguardless if the checkbox is checked or not... and I'm not sure that transfering 2 variables through the ajax scripts works the way I typed above... any suggestions for this? </BODY> </HTML>
Last edited by marcmm; Jan 27th, 2009 at 1:03 pm.
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
Starting from scratch with AJAX is completely unnecessary.
There are many fine Javascript libraries that exist for this reason.
Again I recommend you look at jQuery or Prototype.
This is not a solution for your issue but is an example that you should be able to manipulate to get your result:
And now for php file:
There are many fine Javascript libraries that exist for this reason.
Again I recommend you look at jQuery or Prototype.
This is not a solution for your issue but is an example that you should be able to manipulate to get your result:
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script type="text/javascript"> //Wait for the document to be ready $(document).ready(function(){ //Listen for a click on every checkbox $("input:checkbox").click(function () { var val = $(this).val(); var name = $(this).attr('name'); //This is so IE does not cache the results var noCache = new Date(); //Make a JSON request $.getJSON('ajax.php', {'item':name, 'val':val, '_':noCache.getTime()}, function(json){ $("[name='" + json.item + "']").attr('value', json.check ) }); }); //Change the value of the checkbox to 1 or 0 function updateElement(json) { //Update the checkbox and change the value status $("[name='" + json.item + "']").attr('value', json.check ) } }); </script> </head> <body> Item 1 <input type="checkbox" name="1" id="viz" value="0"/> <br /> Item 2 <input type="checkbox" name="2" id="viz" value="0"/> <br /> Item 3 <input type="checkbox" name="3" id="viz" value="0"/> <br /> Item 4 <input type="checkbox" name="4" id="viz" value="0"/> <br /> </body> </html>
And now for php file:
php Syntax (Toggle Plain Text)
<?php //Make sure we aren't caching this file header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); //Do some sql statements etc, and make the necessary changes using the get item value etc. //return some json for demonstration $val = 0; if( $_GET['val'] == 0) { $val = 1; } $arr = array ( 'item'=>$_GET['item'], 'check'=>$val ); echo json_encode($arr);
Last edited by mschroeder; Jan 27th, 2009 at 4:37 pm.
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
I'm having a hard time figuring out what is going on in there... but anyways... can somebody please tell me how I could get the value of a checkbox when the user clicks it?
I tryed onclick function(this.value) or on mouseup function ( this.value)
and tryed echoing it on the file the function would open...
but this.value for the checkbox ALWAYS returned "on" reguardless if it was checked or not...
wich leads me to either think that it was only executing the function ONCE when the user first clicked the checkbox and then for some reason it didn't fire no matter how many times the user would re-click it...
or that this.value always returns on for the checkbox reguardless what state it is in...
so, can anybody give me a clue about this?
I tryed onclick function(this.value) or on mouseup function ( this.value)
and tryed echoing it on the file the function would open...
but this.value for the checkbox ALWAYS returned "on" reguardless if it was checked or not...
wich leads me to either think that it was only executing the function ONCE when the user first clicked the checkbox and then for some reason it didn't fire no matter how many times the user would re-click it...
or that this.value always returns on for the checkbox reguardless what state it is in...
so, can anybody give me a clue about this?
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
I'm not seeing whats so difficult with the code i posted.
I tend to believe the problem is, you don't want to look at, and apply the concepts to your code, you simply want the exact solution to your problem.
The code I posted not only handles the click events on EVERY checkbox on the page but also provides the mechanisms for capturing the value of the checkbox and the name of the field.
It also handles the AJAX request. The only difference is I used a function designed for returning JSON as an example of the flexibility you gain by working with a javascript library. You could just as simply make an ajax call with $.ajax and return xml or anything else you would like.
If my solution doesn't appease you, then I would suggest having a look here.
I tend to believe the problem is, you don't want to look at, and apply the concepts to your code, you simply want the exact solution to your problem.
The code I posted not only handles the click events on EVERY checkbox on the page but also provides the mechanisms for capturing the value of the checkbox and the name of the field.
It also handles the AJAX request. The only difference is I used a function designed for returning JSON as an example of the flexibility you gain by working with a javascript library. You could just as simply make an ajax call with $.ajax and return xml or anything else you would like.
If my solution doesn't appease you, then I would suggest having a look here.
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: header probs..
- Next Thread: Image Resizing with PHP
| Thread Tools | Search this Thread |
ajax apache api array beginner binary broadband broken cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date display dynamic echo email error fcc file files folder form forms freelancing function functions google href htaccess html image include incode insert integration ip java javascript joomla limit link login loop mail match menu method mlm mod_rewrite multiple mysql oop pageing pagerank paypal pdf php problem query radio random recursion recursiveloop regex remote script search server sessions sms smtp soap source space sql strip_tags subversion support! syntax system table template tutorial update upload url validator variable video web window.onbeforeunload=closeme; xml youtube






