-
<?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 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.
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
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. -
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:Duplicate entry '1' for key 1
Changing -
$idpost = $idrow["id"]++;
-
to: -
$idpost = $idrow["id"]+1;
-
Would only give me:
error, insert query failed:Duplicate 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
Expert 4TB
This is a whole other topic so I think its for the best.
The new topic is located here 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 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. ...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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"...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |