473,775 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Image/$_FILES Handling please

I've a HTML form that allows a user to specify the location to upload
a file from:

<p><label for = 'file'>Upload Graphics : <input type='file'
name='imagefile ' /></label></p>

I then want to check if the user entered something or not as this
isn't mandatory. Here's an excerpt of the relavent code:

if (isset($_POST['submit']))
{
if (isset($_FILES['imagefile']))
{
if ($_FILES['imagefile']['type'] == "image/gif")
{
copy ($_FILES['imagefile']['tmp_name'],
"images/".$_FILES['imagefile']['name'])
or die ("Could not copy");

The problem is the line: if (isset($_FILES['imagefile'])) isn't
doing what I expect it to do, it is always returning true and
executing the code in the if construct.

How can I solve this issue so that if the user isn't submitting
anything to be uploaded that it doesn't execute that code and vice
versa?

Thanks.
Nov 15 '07 #1
3 2247
On 15 nov, 08:46, "skulkrinb...@g ooglemail.com"
<skulkrinb...@g ooglemail.comwr ote:
I've a HTML form that allows a user to specify the location to upload
a file from:

<p><label for = 'file'>Upload Graphics : <input type='file'
name='imagefile ' /></label></p>

I then want to check if the user entered something or not as this
isn't mandatory. Here's an excerpt of the relavent code:

if (isset($_POST['submit']))
{
if (isset($_FILES['imagefile']))
{
if ($_FILES['imagefile']['type'] == "image/gif")
{
copy ($_FILES['imagefile']['tmp_name'],
"images/".$_FILES['imagefile']['name'])
or die ("Could not copy");

The problem is the line: if (isset($_FILES['imagefile'])) isn't
doing what I expect it to do, it is always returning true and
executing the code in the if construct.

How can I solve this issue so that if the user isn't submitting
anything to be uploaded that it doesn't execute that code and vice
versa?

Thanks.
Hi, try this

if (isset($_FILES['imagefile']['name']))

hope it helps
Nov 15 '07 #2
On Thu, 15 Nov 2007 12:46:36 +0100, sk**********@go oglemail.com
<sk**********@g ooglemail.comwr ote:
I've a HTML form that allows a user to specify the location to upload
a file from:

<p><label for = 'file'>Upload Graphics : <input type='file'
name='imagefile ' /></label></p>

I then want to check if the user entered something or not as this
isn't mandatory. Here's an excerpt of the relavent code:

if (isset($_POST['submit']))
{
if (isset($_FILES['imagefile']))
{
if ($_FILES['imagefile']['type'] == "image/gif")

Never ever trust a user given mime-type. Use exif_imagetype( ) or
getimagesize() to check images.
{
copy ($_FILES['imagefile']['tmp_name'],
"images/".$_FILES['imagefile']['name'])
or die ("Could not copy");

The problem is the line: if (isset($_FILES['imagefile'])) isn't
doing what I expect it to do, it is always returning true and
executing the code in the if construct.

How can I solve this issue so that if the user isn't submitting
anything to be uploaded that it doesn't execute that code and vice
versa?
RTFM, UPLOAD_ERR_NO_F ILE

if($_FILES['imagefile']['error']!= UPLOAD_ERR_OK){
switch($_FILES['imagefile']['error']){
case UPLOAD_ERR_INI_ SIZE:
case UPLOAD_ERR_FORM _SIZE:
//file was bigger then allowed
break;
case UPLOAD_ERR_PART IAL:
//file was partially uploaded
break;
case UPLOAD_ERR_NO_F ILE:
//No file was uploaded.
break;
case UPLOAD_ERR_NO_T MP_DIR:
//Missing a temporary folder to store the upload
break;
case UPLOAD_ERR_CANT _WRITE:
//can't write upload to disk
case UPLOAD_ERR_EXTE NSION:
//extention was blocked
break;
default:
//screeching halt. unkown error. abort, abort,mayday (m'aidez), alarm
(a l'arme)
}
} else {
//everything fine and dandy
}

--
Rik Wasmus
Nov 15 '07 #3
Many thanks, that solved the problem.
Nov 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3930
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give them a caption, storing the caption and filename in a text file. It's a bit buggy when removing the photos and captions from the file, and also in displaying them on the delete page. you can see it in action at www.4am.com.au/gallery/upload.php...
4
2457
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save a thumbnail.jpg .... all at the same time. Links to a working example would be appreciated. Thanks.
9
2271
by: pescott | last post by:
Hello people, I could really use your help on this one... I have script to upload image data to a database, but all that is uploaded is the decription, not the data BLOB itself. There must be something wrong with the code that fread() and fopen() the file, but what? The inclusion file below, which diplays the form: <?php print ("<table>"); print ("<tr>");
7
1711
by: pescott | last post by:
I am still struggling to get some files uploaded to a database as BLOB data. I have 5 includes which allows the user to upload 5 files, numbered accordingly. However, there are script errors. Duhh, why else would I post? Well, here goes: CODE $adres = trim (htmlspecialchars($_POST)); $verkocht = trim (htmlspecialchars($_POST)); $form_description1 = trim (htmlspecialchars($_POST));
4
2715
by: yatin | last post by:
hi, friend. i have a problem in image validation plz check it. i have to include a image validation in foam 1. but i sending a details through form2. But when i include a image validation in foam1, then i can not change my image on calling a function onchange="return valid1" <my source code is here> <? session_start(); ob_start(); set_time_limit(0); include("includes/vars.inc.php");
10
7076
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for only small sized iamages.. for eg. resizing takes place for 70 kb sized images but fails for 600kb or more.. my code is below..
8
1274
by: adamjblakey | last post by:
Hi, I am trying to build a function and from a script but it does not seem to be working. Can anyone see anything wrong with this? Also i need it to convert the large image to a specific size also, like the thumbnail so i need to put something in there so i can enter a width of the large image and this will be resized also. Here is the function: function uploadimage($value){
22
4982
by: simon2x1 | last post by:
i have an image which width is 213 and height is 200 when i echo the image and i resize it echo "<img src='company/$present' width='70' height='68'/>"; the image was not as clear as when it was 213 * 200.how can i make the image clear after i have resize it to 70 * 200.
6
1841
by: mfaisalwarraich | last post by:
hi every body, i am using the following code to upload an image. it directly jumps to the error message please tell me what im doing wrong here. <form name="myform" enctype="multipart/form-data" method="post" action=""> Upload Album/Artist Image : <input type="file" name="imguploader"> <br /> <input type="submit" value="uploadnow" name="yes" /> </form>
0
9454
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
10268
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
10107
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...
1
7464
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
6718
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
5360
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...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.