473,602 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Single file upload in a multiple uploads system

58 New Member
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 2301
Atli
5,058 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
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:Duplicat e 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:Duplicat e entry '2' for key 1.
Jul 15 '07 #6
Jankie
58 New Member
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 Recognized Expert Expert
This is a whole other topic so I think its for the best.

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

<Email removed>
Jul 17 '07 #9
Atli
5,058 Recognized Expert Expert
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
2418
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. Everything works OK. My concern is how can I be sure the user is really uploading an image and not a file with some malicious code in it. Also, can someone tell me what my security concerns are here? Here's the html: <form id="Form1"...
13
4295
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 that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
3
1395
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 multiple uploads? I have a database record that will link from 2 to 4 files that have been uploaded. When entering a new record, I'd like for them to be able to upload the files as well. For now, My thinking is that I'd wrap each upload field in a...
4
5162
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. Message board, stored in mysql database (1 file per message, message in db, file in file system) I need a link (Download Project) which will download all the project
3
3588
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 "D:\Inetpub\wwwroot\images\uploads\uploadedFile.jpg' because it is being used by another process. If I wait 15 seconds, the error will dissappear. I did an extensive search on this error as it pertains to file uploads
7
1552
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
19855
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 found at www.srmiles.com/freestuff/ajax_file_uploader/ . you can do multiple file uploads. each upload will have it's own "form"-tag, so that each file is uploaded for its own. could be a good solution if there are "big" uploads.
12
2204
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" name="totalFormInputs" value="lawrence@krubner.com" /></p>
6
4401
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 design. The admin user needs to be able to assign both an image and a corresponding PowerPoint/Word/etc document to be downloaded once the end-user selects it. I am able to get the image in and out of the database no problem, but I am not able to...
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.