473,804 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if a file was sent by a form?

Markus
6,050 Recognized Expert Expert
I'm developing a small image hosting project (not commercial just a project to test my php abilities)

and i want to check that the user has entered a file

the code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lengthCheck = $_POST['file'];
  3. if(strlen($lengthCheck) < 1){
  4. setcookie("shortLength", "No File Innput", time()+3600);
  5. header('Location: http://mahcuz.com/upload.php');
  6. }
  7. ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  9.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head>
  12.  
i want to check that the user has input a file and if not redirect back to the previous page. There's a cookie so i can display an error on the previous page.

Even if there is a file entered the page is still redirected..

Any ideas?

Much thanks.
Sep 9 '07 #1
5 2689
Atli
5,058 Recognized Expert Expert
Hi Markus.

Try using the isset() function.

Also, files are not passed as text variables, via the $_POST array. They are sent as a part of the $_FILES array.

So try something like this:
Expand|Select|Wrap|Line Numbers
  1. # Check if the file element is set
  2. if(isset($_FILES['myFile'])) {
  3.   # Check if the upload was successful.
  4.   if($_FILES['myFile']['error'] == 0) {
  5.     # File was uploaded successfully
  6.   }
  7.   else {
  8.     # File was sent but failed to be uploaded!
  9.   }
  10. }
  11. else {
  12.   # File was not sent!
  13. }
  14.  
Sep 9 '07 #2
Atli
5,058 Recognized Expert Expert
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

Also, please use [code] tags when posting code. It is barely readable without them!

Moderator
Sep 9 '07 #3
Markus
6,050 Recognized Expert Expert
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

Also, please use [code] tags when posting code. It is barely readable without them!

Moderator

Sorry! I am a noob >.<

Noted though, and thanks alot!
Sep 9 '07 #4
Markus
6,050 Recognized Expert Expert
Hi Markus.

Try using the isset() function.

Also, files are not passed as text variables, via the $_POST array. They are sent as a part of the $_FILES array.

So try something like this:
Expand|Select|Wrap|Line Numbers
  1. # Check if the file element is set
  2. if(isset($_FILES['myFile'])) {
  3.   # Check if the upload was successful.
  4.   if($_FILES['myFile']['error'] == 0) {
  5.     # File was uploaded successfully
  6.   }
  7.   else {
  8.     # File was sent but failed to be uploaded!
  9.   }
  10. }
  11. else {
  12.   # File was not sent!
  13. }
  14.  

Ahh but ofcourse! Thanks alot! :D

Ahh thanks so much! :D:D
Sep 9 '07 #5
Atli
5,058 Recognized Expert Expert
Glad I could help :)
Please don't hesitate to post again if you have any more questions or problems we can help with!
Sep 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
7135
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate a user from information you got from the session. Each secure app on a site must challenge the user for name and password, each and every time the user accesses it (not just once and then store it in the session). If a secure app is multi-page,...
2
5354
by: Dknight | last post by:
Hi, all! I have a problem, how can I check a form data before sending it to server. I have the example below, but first data is sent to server, but after JavaScript checks the data :( I need that JavaSctipt check data and after send it to the server, how can do that? Example: ....
5
737
by: Neo | last post by:
Hello, I am receiving a file as a binary stream from a C++ Client Application. >From the application, the file is being sent as a "Multipart/form-data". I want to receive this file on the server, for which i am using Request.Inputstream and reading it into a byte array. This is not working as it is coupling the binary content with some Boundary and text such as "Content-Disposition:
1
4269
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will require checking a check box to tell which category something will pertain to. Well after each record is entered i want the check to remain with that record and auto clear when going to the next record so i can input something else if i have a...
25
29766
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden" name="ClickedElement" id="Messenger" /> </form> </body>
0
1427
by: karen987 | last post by:
Please could someone help me with this code? The problem appears to be a simple one, but i'm not html or ASP literate and need to modify the below code to add 2 things. First a bit about the website: It's a news website, which has talkback, where the public register to post comments. One of the options in the comments is for the commentator to leave an email icon, if they want private emails from other readers. The initial email is sent...
2
7670
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the PasswordRecovery with a Password reset required; a temporary password is sent to the account on file. I want an extra layer of security to accommodate the very unlikely contingency that someone's e-mail account is compromised. Challenging with the...
7
2577
by: Vishal | last post by:
Hi, I am writing a CGI to serve files to the caller. I was wondering if there is any way to tell in my CGI if the client browser is still connected. If it is not, i want to execute some special code before exiting. Is there any way to do this? Any help on this is appreciated :) Regards,
0
9705
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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
10564
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
10320
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
10073
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...
1
7609
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
5513
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
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.