473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to handle an uploaded file without using separate script file

14 New Member
I'm new to PHP, but I usually program Windows Apps in .NET(c#, VB). So please excuse my unfamiliarity with code and HTML combined.

It seems pretty simple to upload a file to a website using the following html:

Expand|Select|Wrap|Line Numbers
  1. <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
  2. <p><input type="file" name="image_file" size="20"></p>
  3. <p><input type="submit" value="Upload Image" name="action"></p>
  4. </form>
I'm dealing with a website that the standard is not to use separate script files, but to code in the same file as the html. Most examples on the web for this use separate script files. I found a few examples that do not work. What am I missing in my code to somehow connect the uploaded file in the html to my code on top?

My code with html:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" -->
  4.  
  5. <head>
  6. <script type="text/JavaScript">
  7.  
  8. <? php
  9.  
  10. define ('MAX_FILE_SIZE', 1024 * 50);
  11.  
  12. if (array_key_exists('upload', $_POST)) {
  13.   // replace any spaces in original filename with underscores
  14.   $file = str_replace(' ', '_', $_FILES['image']['name']);
  15.   // create an array of permitted MIME types
  16.   $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
  17. 'image/png');
  18.  
  19.   // upload if file is OK
  20.   if (in_array($_FILES['image']['type'], $permitted)
  21.       && $_FILES['image']['size'] > 0 
  22.       && $_FILES['image']['size'] <= MAX_FILE_SIZE) 
  23.     {
  24.       $result= "$file file uploaded!";
  25.     }
  26.   else {
  27.     $result = "$file is either too big or not an image.";
  28.   }
  29.  
  30.  
  31. }
  32. ?>
  33.  
  34.  
  35.  
  36. </script>
  37. </head>
  38. <body>
  39.  
  40. <?php
  41. // if the form has been submitted, display result
  42. if (isset($result)) {
  43.   echo "<p><strong>$result</strong></p>";
  44. }
  45. ?>
  46.  
  47. <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
  48. <p><input type="file" name="image_file" size="20"></p>
  49. <p><input type="submit" value="Upload Image" name="action"></p>
  50. </form>
  51.  
  52. </body>
  53. </html> 

Thanks!
Oct 20 '10 #1
5 1991
code green
1,726 Recognized Expert Top Contributor
I think what you are trying to do is get the same page to submit itself.
You do this with
Expand|Select|Wrap|Line Numbers
  1. action="$_SERVER['PHP_SELF']"
Oct 20 '10 #2
LELE7
14 New Member
I'm getting the following error:
Firefox can't find the file at
/C:/WebSites/STUNNI~1/$_SERVER['PHP_SELF'], the root directory being the root of my page. Why does it think it's a separate file? Am I missing something important in my code on top?
Oct 21 '10 #3
code green
1,726 Recognized Expert Top Contributor
I think your HTML is wrong. I would need to see it.
But $_SERVER['PHP_SELF'] returns
The filename of the currently executing script, relative to the document root.
For instance, $_SERVER['PHP_SELF'] in a script at the address
http://example.com/test.php/foo.bar would be /test.php/foo.bar.
Oct 21 '10 #4
HaLo2FrEeEk
404 Contributor
If it's in HTML, then he needs to do:

action="<?php echo $_SERVER['PHP_SELF']; ?>"

That should work.
Oct 21 '10 #5
LELE7
14 New Member
In the end, I think the problem was that I wasn't running it from the proper place- from my localhost (or my WAMP/www directory. I was just running it from my regular file directory. Not sure if this makes sense, but I'm not getting the error anymore. Thanks for your help!
Oct 22 '10 #6

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

Similar topics

7
1641
by: balgach | last post by:
Greetings all, I have a group of rather large files (by group i mean close to 2x10^7 files, each 12-15megs) now i need information which is stored in just the last 512 bytes of each file. i was wondering if there is a way to strip out this information without loading the entire file into memory. Right now im doing it with fopen() and fread() and it takes several hours to process all the files. obviously i know exactly where the...
11
2509
by: ulyses | last post by:
Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer which size is 50MB and the second 30MB. Now we come to my problem. Is there possibility to swap this rows without using system memory (preferably in Unix/Linux)? Is there any function in C to do this?
4
3626
by: gsuns82 | last post by:
hi friends,can any one give the coding for finding file size using java script???
5
6330
by: sat | last post by:
Hi, I'm working as a DB2 UDB DBA. I have some questions regarding SCP command... Actually every week redirect restore takes place from one of my production servers to test servers.. So, but before i can restore it on the test server i need to transfer the backup file from the production server... So i want use the SCP command to do that... since it takes place on every wednesday early morning i would like run the scp command using...
2
2174
by: rinkudhimar | last post by:
I want to write using fwrite and upload the file without using File Field or Browse option. Actually I want to upload static file after writing it useing fwrite and as soon as it gets uploaded it should be savedt o the database.I want code for this: 1) Write a file 2) Upload the same file(static one) 3) Save it to Database
5
3348
by: ganesh.kundapur | last post by:
Hi, I want to know how to compile the C source file without using the compiler directly. I mean to say by using compiler componets such as ( cpp, cc, as, ld ). I tried to compile the fallowing code as fallows file name: hello.c # include <stdio.h> int main ( void )
6
11767
by: Gorksha | last post by:
How to upload a file without using browse button provided by <input type=file > tag in html..
6
2514
by: Kuldeep | last post by:
Framework: Visual Studio 2005 Technology: ASP.NET 2.0 Language: C#.NET 2.0 Hi All, How can we generate an Excel File through C#.NET without using Interops or XML ? Any leads on this would be highly appreciated.
1
1855
by: mihir0288 | last post by:
I want to save HTML file that i open in web browser on my hard disk without using save file dialog box. I want to save it as html page through only code and without user interactions.
3
2009
by: nguyenthuy | last post by:
How to disable Mousewhell without using MouseWheel.dll file and copy it to each computer need to prevent scrolling. If there are many computer use the program, I can't setup one bye one. So, please support me! Thanks
0
9643
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
10319
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
10147
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
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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...
0
6737
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.