-
<?php
-
if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0){
-
foreach ($_FILES["userfile"]["error"] as $key => $error) {
-
$name = $_FILES['userfile']['name'][$key];
-
$tempname = $_FILES['userfile']['tmp_name'][$key];
-
…………
-
………
-
$path = 'uploads/';
-
include 'dbcon.php';
-
$rep = strpos($_FILES["userfile"]["type"][$key], "image");
-
if ($rep===FALSE){
-
$query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$id’,'$name','$type','$size','$file_data','$Caption)";
-
mysql_query($query) or die('error, insert query failed:'.mysql_error());
-
mysql_close($conn);
-
}
-
move_uploaded_file($_FILES["userfile"]["tmp_name"][$key],$path);
-
}
-
}
-
?>
-
<form action="" method="post" enctype="multipart/form-data">
-
<p>image upload:
-
<input type="file" name="userfile[]" />
-
<input type="file" name="userfile[]" />
-
<input type="file" name="userfile[]" />
-
<input type="file" name="userfile[]" />
-
<input type="file" name="userfile[]" />
-
</form>
-
<?
-
?>
-
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.
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.
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.
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. -
if($error != 0) {
-
continue;
-
}
-
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. -
if($_FILES['userfiles']['error'][$key] != 0) {
-
continue;
-
}
-
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"
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 -
$idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
-
$idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
-
if($idrow = @mysql_fetch_array($idresult)){
-
$idpost = $idrow["id"]++;
-
}else{
-
$idpost = 0;
-
}
-
-
1. $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$Capt ion)";
-
2. mysql_query($query) or die('error, insert query failed:'.mysql_error());
-
3. mysql_close($conn);
-
4. }
-
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 -
$idpost = $idrow["id"]++;
-
to: -
$idpost = $idrow["id"]+1;
-
Would only give me:
error, insert query failed:Duplicat e entry '2' for key 1.
Sorry,sounds more like a Mysql issue,so I reposted it in Mysql section
My apologies if am not supposed to.
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 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 Sign in to post your reply or Sign up for a free account.
Similar topics |
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"...
|
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...
|
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...
|
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
|
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
| |
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
|
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.
|
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>
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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
| |