473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Single file upload in a multiple uploads system

58
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0){
  3. foreach ($_FILES["userfile"]["error"] as $key => $error) {
  4. $name = $_FILES['userfile']['name'][$key];
  5. $tempname  = $_FILES['userfile']['tmp_name'][$key];
  6. …………
  7. ………
  8. $path = 'uploads/';
  9. include 'dbcon.php';
  10. $rep = strpos($_FILES["userfile"]["type"][$key], "image");
  11. if ($rep===FALSE){
  12. $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$id’,'$name','$type','$size','$file_data','$Caption)";
  13. mysql_query($query) or die('error, insert query failed:'.mysql_error());
  14. mysql_close($conn);
  15. }
  16. move_uploaded_file($_FILES["userfile"]["tmp_name"][$key],$path);
  17. }
  18. }
  19. ?>
  20. <form action="" method="post" enctype="multipart/form-data">
  21. <p>image upload:
  22. <input type="file" name="userfile[]" />
  23. <input type="file" name="userfile[]" />
  24. <input type="file" name="userfile[]" />
  25. <input type="file" name="userfile[]" />
  26. <input type="file" name="userfile[]" />
  27. </form>
  28. <?
  29. ?>
  30.  
When user uploads say 1 file,the other remaing four entries also upload in the db with 0 bytes.This is annoying as I need to move to find a way to get rid of the ids of even those legitimate entries(like one id for whatever number of files a user submits at once).For the time being,I only want help regarding restricting the remaining file inputs from posting into the database
My gratitude in advance.
Jul 15 '07 #1
9 2292
Atli
5,058 Expert 4TB
You need to check the '$_FILE['name']['error']' variable. If the file was uploaded successfully this value will be '0'. Any other value and the upload failed an should not be used.

If I remember correctly, not specifying a file will cause error number 4.
Jul 15 '07 #2
Jankie
58
You need to check the '$_FILE['name']['error']' variable. If the file was uploaded successfully this value will be '0'. Any other value and the upload failed an should not be used.

If I remember correctly, not specifying a file will cause error number 4.

How would I do that Atli?
Sorry i haven't done it before.
Jul 15 '07 #3
Atli
5,058 Expert 4TB
You could add these couple of lines right after you start your foreach loop.
They will check if the $error variable you create with your foreach loop is set to anything other than '0' and if it is it skips the loop and moves on to the next loop.

Expand|Select|Wrap|Line Numbers
  1. if($error != 0) {
  2.   continue;
  3. }
  4.  
Here is another way to do the same thing, except this time I check the $_FILES array instead of the $error variable the foreach loop created.
Expand|Select|Wrap|Line Numbers
  1. if($_FILES['userfiles']['error'][$key] != 0) {
  2.   continue;
  3. }
  4.  
Jul 15 '07 #4
Jankie
58
Thanks a lot Atli
I'll try it off and let you know of the outcome.I do appreciate coming up with the second alternative as it is really what my code based on.As you may have notice above,my error var in the foreach is not complete as I only used it to get the multiple uploads working.Now I understand what pro programmers refer to when they talk about "clean code"
Jul 15 '07 #5
Jankie
58
Perfect
Thank you very much Atli for it !
I still have to deal with another extension of the problem,though
It is one submission of multiple files,so I just need one id record for all those files.Now I get a corresponding number of ids instead of just one
I tried this,but for some reason it didnt work for me
Expand|Select|Wrap|Line Numbers
  1. $idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
  2. $idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
  3. if($idrow = @mysql_fetch_array($idresult)){
  4.     $idpost = $idrow["id"]++;
  5. }else{
  6.     $idpost = 0;
  7. }
  8.  
  9. 1.    $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$Capt  ion)";
  10. 2.    mysql_query($query) or die('error, insert query failed:'.mysql_error());
  11. 3.    mysql_close($conn);
  12. 4.    }
  13.  
But it only let all files be uploaded except the last one,then I got this error:
error, insert query failed:Duplicate entry '1' for key 1

Changing
Expand|Select|Wrap|Line Numbers
  1.  $idpost = $idrow["id"]++;
  2.  
to:
Expand|Select|Wrap|Line Numbers
  1. $idpost = $idrow["id"]+1;
  2.  
Would only give me:
error, insert query failed:Duplicate entry '2' for key 1.
Jul 15 '07 #6
Jankie
58
Sorry,sounds more like a Mysql issue,so I reposted it in Mysql section
My apologies if am not supposed to.
Jul 16 '07 #7
Atli
5,058 Expert 4TB
This is a whole other topic so I think its for the best.

The new topic is located here
Jul 17 '07 #8
see the link http://www.elmacindia.com/register.php its use ful for u

<Email removed>
Jul 17 '07 #9
Atli
5,058 Expert 4TB
see the link http://www.elmacindia.com/register.php its use ful for u

<Email removed>
I've removed the Email address from your post. Posting email addresses is not allowed, as they can (and most likely will) be picked up by spammers.

Please read the FAQ before posting.

Moderator
Jul 18 '07 #10

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

Similar topics

3
by: moondaddy | last post by:
I have a simple webform where a user can upload images by using an input element of type "file". In the a button's click event in the codebehind is this code which saves the file to the server. ...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
3
by: darrel | last post by:
My understanding is that using the FILE form element to allow a file upload is limited to one unique file per page. Is that correct? Any thoughts on how best to design an interface to allow...
4
by: yehaimanish | last post by:
I am developing the project management system. Each Project: 1. Title, description ... , stored in mysql database 2. Upto ten files (initial description), (name in db, file in file system) 3....
3
by: LisaBigJax | last post by:
I am utilizing the ASP.net FileUpload control to upload a file. After upload, if a user tries to delete the file, I get the dreaded The process cannot access the file...
7
by: karnati | last post by:
hi group, i need to upload all jpg images located in a folder is it possible using file option thanks in advance karnati
3
by: markus.rietzler | last post by:
i want to do (multiple) file upload(s) and display a progress bar. with firefox and safari it is no problem at all. only IE makes some problems. my script is based on ajax-uploader, which can be...
12
by: lawrence k | last post by:
I've a form that starts off like this: <form method="post" action="profile.php? id=5&formName=my_story_edit.htm" enctype="multipart/form-data"> <p>Email address:<br /> <input type="text"...
6
jenkinsloveschicken
by: jenkinsloveschicken | last post by:
I am developing a administrative backend for our sales dept website. They are using admin forms to manipulate what is displayed for the end user on the site. I am running into a problem with a form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.