hi there... i have a form where a user may optionally upload a maximum of 2 files along with other textual data (there are 2 file fields built in the form). i'm having trouble writing a php script that will allow 2 files to be uploaded optionally.
i have successfully written a small php script that will allow one file upload (with one file field in the form), as well as uploading 2 files (with 2 file fields in the form). but i don't know how to make it optional. it seems that if the file field is there the user MUST upload something or else there's an error...
here is my HTML form code: -
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-
<title>Untitled Document</title>
-
</head>
-
-
<body>
-
-
<form enctype="multipart/form-data" action="uploadtest.php" method="POST">
-
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
-
Choose a file to upload:<br /> <input name="datafileA" type="file" /><br />
-
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
-
Choose a file to upload:<br /> <input name="datafileB" type="file" />
-
-
<br /><br />
-
<input type="submit" value="SUBMIT" />
-
</form>
-
-
</body>
-
</html>
-
here is my PHP script: -
<?php
-
-
function UploadFiles()
-
{
-
$FileA = $_FILES['datafileA'];
-
$FileB = $_FILES['datafileB'];
-
-
if (($FileA != "") && ($FileB == ""))
-
{
-
UploadA();
-
}
-
else if (($FileA == "") && ($FileB != ""))
-
{
-
UploadB();
-
}
-
else if (($FileA != "") && ($FileB != ""))
-
{
-
UploadAandB();
-
}
-
else
-
{
-
echo 'No Files Uploaded';
-
}
-
}
-
-
function UploadA()
-
{
-
$newfolder = "uploads/";
-
$uploadA = $newfolder . basename( $_FILES['datafileA']['name']);
-
-
if (move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA))
-
{
-
echo 'File A Uploaded';
-
}
-
else
-
{
-
echo 'Error';
-
}
-
}
-
-
function UploadB()
-
{
-
$newfolder = "uploads/";
-
$uploadB = $newfolder . basename( $_FILES['datafileB']['name']);
-
-
if (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadA))
-
{
-
echo 'File B Uploaded';
-
}
-
else
-
{
-
echo 'Error';
-
}
-
}
-
-
function UploadAandB()
-
{
-
$newfolder = "uploads/";
-
$uploadA = $newfolder . basename( $_FILES['datafileA']['name']);
-
$uploadB = $newfolder . basename( $_FILES['datafileB']['name']);
-
-
if ((move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA)) && (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadB)))
-
{
-
echo 'File A and B Uploaded';
-
}
-
else
-
{
-
echo 'Error';
-
}
-
}
-
-
?>
-
3 1511
your check is not ok.
>$FileA = $_FILES['datafileA'] ;
>if ($FileA != "")
it must be :
if (! $_FILES ['datafileA'] ['error'])
if there is an input type file named datafileA not used, empty, $_FILES ['datafileA'] will be an array, it wont be empty, but its field error will contain a value of 4.
Check php.net, all the errors are explained.
An alternative, name both inputs as "yourname[]" then in php you will have an array ($_INPUT ['yourname']['name'][0], $_INPUT ['yourname']['name'][1], $_INPUT ['yourname']['error][0], etc) and you will can process it recursively (a loop maybe). 3 functions are not needed.
Good luck
your check is not ok.
>$FileA = $_FILES['datafileA'] ;
>if ($FileA != "")
it must be :
if (! $_FILES ['datafileA'] ['error'])
if there is an input type file named datafileA not used, empty, $_FILES ['datafileA'] will be an array, it wont be empty, but its field error will contain a value of 4.
Check php.net, all the errors are explained.
An alternative, name both inputs as "yourname[]" then in php you will have an array ($_INPUT ['yourname']['name'][0], $_INPUT ['yourname']['name'][1], $_INPUT ['yourname']['error][0], etc) and you will can process it recursively (a loop maybe). 3 functions are not needed.
Good luck
hi vituko. thanks for the response.
i can't use an array... i have to figure out how to address each file by it's field name because the form's javascript is dependent on each file field having a separate name.
i know this should only be a super easy script to write, but it's so complicated and nothing works. i've been on this almost all day and i still have no idea how to write this small code...
for my form, a user isn't just uploading files... file uploading from the form is optional. but it seems that every file uploading tutorial i've read over the past few days is written so that an error occurs if a file or files aren't uploaded... so i don't know how:
1) to make the uploading optional
2) to address the 2 files as separate names without making an array
3) to continue processing text fields within the same php script after files are uploaded / or not uploaded.
i really hope someone can point me in the right direction...
so this is the closest to success i've come so far... it works, but something very strange is happening with the uploaded files. after the file is uploaded, the file size is smaller.
for example: i uploaded a .JPG image that is 170kb from my desktop, but after is was uploaded, the file was only 115kb on the server. so to compare the differences, i downloaded the file off the server onto my computer... they both looked the same but had different file sizes. i'm assuming my PHP code is doing something to the file when it's being uploaded...
here is my current working PHP code... if anyone sees something wrong with it please educate me... -
<?php
-
-
$uploadfolder = "uploads/";
-
$uploadfileA = $uploadfolder . basename($_FILES['datafileA']['name']);
-
$uploadfileB = $uploadfolder . basename($_FILES['datafileB']['name']);
-
-
if (move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadfileA))
-
{
-
echo "Successful upload File A.\n\n";
-
}
-
else
-
{
-
echo "File A Empty.\n\n";
-
}
-
-
if (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadfileB))
-
{
-
echo "Successful upload File B.\n\n";
-
}
-
else
-
{
-
echo "File B Empty.\n\n";
-
}
-
-
?>
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dickiedyce |
last post by:
Hi there.
I've spent the weekend getting ever more frustrated, trying to get an
upload file function working on a website.
The site is hosted by a company called oneandone. They're using PHP...
|
by: Chamomile |
last post by:
can anyone point me to some straightforward information on
file uploading without using an html form? That is, direcly from
within a php script.
if I know the local path etc. to a particular...
|
by: Ron Brennan |
last post by:
Good afternoon.
The entire task that I'm trying to achieve is to allow a user to browse and
upload multiple files simultaneously, hiding the Browse button of <input>
tags of type="file" and...
|
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: tshad |
last post by:
I have a page that I am using for multiple uses that includes reposting of
the page as well as going to another page where the "form" tag would be
something like:
<form id="something"...
|
by: Chris |
last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL.
Meeting info and Meeting docs reside on 2 related tables in the db. Users
may want to upload anywhere from 1 to 10 or...
|
by: ajaykataram |
last post by:
Hi all i am getting error in the following code to upload files csv and txt files
i have to insert the following values only into database if firstname,lastname and email id is null it should not...
|
by: Atli |
last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t.
In situations where your PHP application...
|
by: LoriFranklin |
last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form.
I am using some code that I found from Jacob at...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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: 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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
| |